Monday, October 15, 2012
Today I am going to post a program that will be able to change i.e control the brightness of any image in Java. First the image is read from a file using Java's ImageIO class (in javax.imageio package) as a BufferedImage. A function is written that takes in the image and brightness value as parameter. Inside it an object of RescaleOp class (in java.awt.image package) is created using the brightness value. Then the object's filter(src,dest) method is called and source image is passed (destination is kept null) and the final image developed is returned. This final image is drawn on a JPanel and shown in frame. Also this image is saved in a new file named dest.jpg in jpeg format using ImageIO class. Screenshots of the original and the brightened image is shown below

Original image
Brightened by 50%








--------------------------------------------------------------------------------------------------------------------------
 SOURCE CODE
--------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import javax.imageio.ImageIO;

@SuppressWarnings("serial")
public class Brighten extends JPanel{
   @Override
   public void paintComponent(Graphics g){
      Graphics2D g2d=(Graphics2D)g;
      try{

          //reading image data from file
          BufferedImage src=ImageIO.read(new File("src.jpg"));
          /* passing source image and brightening by 50%-value of 1.0f means original brightness */
          BufferedImage dest=changeBrightness(src,1.5f);

          //drawing new image on panel
          g2d.drawImage(dest,0,0,this);

          //writing new image to a file in jpeg format
          ImageIO.write(dest,"jpeg",new File("dest.jpg"));
      }catch(Exception e){
            e.printStackTrace();
      }
   }


   public BufferedImage changeBrightness(BufferedImage src,float val){
       RescaleOp brighterOp = new RescaleOp(val, 0, null);
       return brighterOp.filter(src,null);
//filtering
   }
   
   public static void main (String[] args) {
       JFrame jf=new JFrame("BRIGHTEN");
       Brighten obj=new Brighten();
       jf.getContentPane().add(obj);
       jf.setVisible(true);
       jf.setSize(325,270);
       jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
    }
}

--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD LINKS
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire 
DOWNLOAD the source from 4shared
--------------------------------------------------------------------------------------------------------------------------
RELATED POSTS
--------------------------------------------------------------------------------------------------------------------------

1 comment:

Total Pageviews

Followers


Labels

Popular Posts

free counters