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
--------------------------------------------------------------------------------------------------------------------------
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 the source from Mediafire
DOWNLOAD the source from 4shared
--------------------------------------------------------------------------------------------------------------------------
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 4shared
--------------------------------------------------------------------------------------------------------------------------
RELATED POSTS
--------------------------------------------------------------------------------------------------------------------------
Labels:Graphics2D-Images
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
Thanks a lott :)
ReplyDelete