Posts Tagged: brigthness control

Brightness control with java Buffered image class..

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. 😀 :D. 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