Brightness control with java Buffered image class..

10 07 2011

Hi, today I’m going to tell you how to change brightness of an image with Java’s Buffered image class. .. And consider this will be a series of posts regarding image processing/enhancement with Java 🙂 .

First we need to create an String variable to store the path of image file.

String path=”filepath_of_ your_image”;

for a example I used : String path=”/home/gihan/Pictures/nfs_bmw.jpg”;

Then create a file type object. And give our path as parameter.

File file = new File(path);

Then create a  buffered image.

BufferedImage bufferedImage = ImageIO.read(file);

If you want to, now you can preview your image using :

ImageIcon icon;
JLabel picLabel=new JLabel();

 icon = new ImageIcon(bufferedImage);          
 picLabel.setIcon(icon);

An Image before change the Brightness...

But we are going to process that image so let’s move to next point. There are two methods I’ve written in my programm.

1). public void rescale() {}

2). private void brighten() {}

Let’s consider them, one by one
     public void rescale() {

        rescale = new RescaleOp(1.0f,offset, null);
        bufferedImage=rescale.filter(bufferedImage,null);//(sourse,destination)

       }//end rescale
In here, I’ve created RescaleOp object, and should pass parameters as in order sacleFactors, offsetshints .

By changing offsets value we can adjust the brightness of an image, and by changing sacleFactors value we can adjust contrast of an image and hints for the specified RenderingHints or can be kept as null. In my program offset is set to 20 in float for default image and for enhanced image it is set to 170 in float.

An Image after change the Brightness...

Here you can look at the related java docs.

In the filter method filter(sourse_bufferedImage, destination_bufferedImage); should be given as like this, and it returns filtered buffered image.

Then in the private void brighten()  method I think there is nothing to describe.😀😀. I called the rescale() method and as I mentioned earlier, the buffered image is printed on a JLabel.😀
private void brighten(){

         rescale();
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);

    }//end  brighten()

Here is the source code of it and if you want you can DOWNLOAD my Netbeans project. And it has two classes Bright.java and Main.java which is included the main method.

Main.java
/*
 * Main.java
 */
package bright;

/**
 *
 * @author gihan
 */
public class Main {

    public static void main(String[] args) {
        Bright obj=new Bright();
    }//end main()

}//end class Main

Bright.java

/*
 * Bright.java
 */
package bright;

import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author gihan
 */
public class Bright extends JFrame{
       //globel variables
        BufferedImage bufferedImage;
        String path=”/home/gihan/Pictures/nfs_bmw.jpg”;// image file path         
        float offset = 170.0f;//change offset to brighten
        /*keep the value offset = 170.0f; as for a normal image*/
        RescaleOp rescale;
        ImageIcon icon;
        JLabel picLabel=new JLabel();

     public Bright() {

         JFrame jf=new JFrame();
         JPanel jp=new JPanel();        

         jf.add(jp);
         jp.add(picLabel);

            jf.setVisible(true);
            jf.setSize(550, 550);
            jf.setLocation(200,100);
            jf.setTitle(“Gihan’s Image Processing Test Area.. “);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            File file = new File(path);

        try {           
            bufferedImage = ImageIO.read(file); // create a buffered image
        } catch (IOException ex) {
            Logger.getLogger(Bright.class.getName()).log(Level.SEVERE, null, ex);
        }

         brighten();   
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);       

    }

     private void brighten(){

         rescale();
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);
         System.out.println(“offset : “+offset  );

    }//end  brighten()

     public void rescale() {

        rescale = new RescaleOp(1.0f,offset, null);
        bufferedImage=rescale.filter(bufferedImage,null);//(sourse,destination)

       }//end rescale

}//end class Bright

If you have any doubt, feel free to ask.😀
Thank you

Gihan Malan De Silva @ gihansblog.wordpress.com


Actions

Information

13 responses

12 07 2011
Irwin Rocca

haha pretty cool site here

13 07 2011
Chris Hejny

I am very pleased to see that you are putting so much of effort for encouraging the visitors with valueable posts like this, I have sent this post to my myspaceaccount profile.

21 07 2011
janakan

continue u r works helps me also………………

6 08 2011
zombieville usa apk

I agree with your Brightness control with java Buffered image class.. Gihan's Blog.., fantastic post.

9 08 2011
robo defense

I agree with your Brightness control with java Buffered image class.. Gihan's Blog.., good post.

21 10 2011
selvakarthi

How to do the same brightness and contrast for .png image ind .gif image.
for gif image its throwing exception “Rescaled image cannot be performed”
and for png it becomes dark…

21 10 2011
selvakarthi

* for gif image its throwing exception “Indexed image cann’t be performed”

11 11 2011
shan

when run your project come exception “An unexpected error has been detected by Java Runtime Environment:”

12 11 2011
Gihan

Hello Shan, I always check the projects before I blog and no errors were encountered. Can you tell me what version of NetBeans you are using? I used NetBeans 7.0 . If there is an error try to open new new project with your NetBeans version and replace the it’s main java file with java files of mine. If you are still having problem with it please feel free to ask. I’ll help you.

26 06 2012
tran my

Thanks bro, I have a execise for them and your share is useful for me😀

31 10 2012
rameeha

how can we adjust the brightness of an image using slider in a java frame.
we are waiting for your favourable answer

31 10 2012
aishah

can we insert an image to a JFrame swing???

3 11 2012
aishah

i need a reply……

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: