The ImageBrightness program allows you to selection an image file. When the image displays on the program window you can slide the knob left or right to change the brightness of the image. After you make change to the image, you can save the updated image in a separated file or override the original image file.
ImageBrightness source code:
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
import
javax.swing.event.*;
import
java.awt.image.*;
import
javax.swing.filechooser.*;
import
java.io.*;
import
java.awt.*;
import
java.awt.geom.*;
import
javax.imageio.*;
import
javax.imageio.stream.*;
class ImgArea
extends Canvas{
Image orImg;
BufferedImage
orBufferedImage;
BufferedImage bimg;
float e=0.0f;
Dimension ds;
int mX;
int mY;
int x;
int y;
boolean imageLoaded;
boolean actionPerf;
public ImgArea(){
imageLoaded=false;
actionPerf=false;
}
1 public void paint(Graphics
g){
Graphics2D
g2d=(Graphics2D)g;
g2d.translate(mX,mY);
if(imageLoaded){
//the image loaded
if(actionPerf)
//the image have been changed
g2d.drawImage(bimg,x,y,this);
else
g2d.drawImage(orBufferedImage,x,y,this);
}
g2d.dispose();
//clear graphics object
}
2 public void
prepareImage(String filename){
try{
MediaTracker
mt=new MediaTracker(this);//Create MediaTracker object
//to track
the image load
orImg=Toolkit.getDefaultToolkit().getImage(filename); //read the image file
mt.addImage(orImg,0);//add
image to the tracker
mt.waitForID(0); //wait for the image to fully load
if(orImg==null)
System.exit(-1); //if no valid image selected exit the program
ds=getToolkit().getScreenSize();
//get the size of screen
//new
coordinate to move the original coordinate to
mX=(int)ds.getWidth()/2;
mY=(int)ds.getHeight()/2;
//coordinate
to show the image on the screen
x=-orImg.getWidth(this)/2;
y=-orImg.getHeight(this)/2;
//get the
size of the orignal image
int
width=orImg.getWidth(null);
int
height=orImg.getHeight(null);
//Create
BufferedImage object from the original image
orBufferedImage=createBufferedImageFromImage(orImg,width,height);
//Create the
blank BufferedImage object
bimg = new
BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
imageLoaded=true;
//now the image loaded
}catch(Exception
e){System.exit(-1);}
}
3 public void filterImage(){
//Define
elements array used for Kernel object
float[]
elements = {0.0f, 1.0f, 0.0f, -1.0f,e,1.0f,0.0f,0.0f,0.0f};
//create
Kernel object kernel from the elements array
Kernel
kernel = new Kernel(3, 3, elements);
//Create
ConvolveOp object cop to wrap the kernel object
ConvolveOp
cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
//filtering
the image
//destination
image is placed in BufferedImage object bimg for later showing and saving
cop.filter(orBufferedImage,bimg);
}
4 public void setValue(float
value){
e=value;
}
5 public void
setActionPerf(boolean value ){
actionPerf=value;
}
6 public BufferedImage
createBufferedImageFromImage(Image image, int width, int height)
{
BufferedImage
dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D
g2 = dest.createGraphics();
g2.drawImage(image,
0, 0, null);
g2.dispose();
return
dest;
}
7 public void saveToFile(String
filename){
String
ftype=filename.substring(filename.lastIndexOf('.')+1); //get file extension
try{
if(actionPerf)
//there is a change to the image so save it
ImageIO.write(bimg,ftype,new
File(filename));
}catch(IOException
e){System.out.println("Error in saving the file");}
}
}
class Main extends JFrame implements
ChangeListener, ActionListener{
JSlider jsd;
ImgArea ia;
JFileChooser chooser;
JMenuBar mainmenu;
JMenu menu;
JMenuItem mopen;
JMenuItem msaveas;
JMenuItem msave;
JMenuItem mexit;
String filename;
boolean actionPerf;
Main(){
ia=new ImgArea();
jsd=new
JSlider(-10,10,0);
jsd.setEnabled(false);
Container
cont=getContentPane();
jsd.addChangeListener(this);
cont.add(ia,BorderLayout.CENTER
);
cont.add(jsd,BorderLayout.SOUTH);
mainmenu=new JMenuBar();
menu=new
JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
mopen=new
JMenuItem("Open...");
mopen.setMnemonic(KeyEvent.VK_O);
mopen.addActionListener(this);
msaveas=new
JMenuItem("Save as...");
msaveas.setMnemonic(KeyEvent.VK_S);
msaveas.addActionListener(this);
msave=new
JMenuItem("Save");
msave.setMnemonic(KeyEvent.VK_V);
msave.addActionListener(this);
mexit=new
JMenuItem("Exit");
mexit.setMnemonic(KeyEvent.VK_X);
mexit.addActionListener(this);
menu.add(mopen);
menu.add(msaveas);
menu.add(msave);
menu.add(mexit);
mainmenu.add(menu);
setJMenuBar(mainmenu);
setTitle("Image
Brightness");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(this.getExtendedState()
| this.MAXIMIZED_BOTH);
setVisible(true);
chooser = new
JFileChooser();
FileNameExtensionFilter
filter = new FileNameExtensionFilter("Image files", "jpg",
"gif","bmp","png");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(false);
enableSaving(false);
}
8 public void
actionPerformed(ActionEvent e){
JMenuItem source =
(JMenuItem)(e.getSource());
if(source.getText().compareTo("Open...")==0)
//The Open sub-menu item is selected
{
setImage();
ia.repaint();
validate();
}
else
if(source.getText().compareTo("Save as...")==0) //The Save as
sub-menu item is selected
{
showSaveFileDialog();
}
else
if(source.getText().compareTo("Save")==0) //The Save sub-menu item is
selected
{
ia.saveToFile(filename);
}
else
if(source.getText().compareTo("Exit")==0) //The Exit sub-menu item is
selected
System.exit(0);
}
9 public void setImage(){
int returnVal =
chooser.showOpenDialog(this);
if(returnVal
== JFileChooser.APPROVE_OPTION) {
filename=chooser.getSelectedFile().toString();
ia.prepareImage(filename);
//prepare image for show
jsd.setEnabled(true);
//enable the slider
jsd.requestFocus();
//move focus to the slider
}
}
10 public void showSaveFileDialog(){
int returnVal = chooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String
filen=chooser.getSelectedFile().toString();
ia.saveToFile(filen);
}
}
11 public void stateChanged(ChangeEvent e){
ia.setValue(jsd.getValue()/10.0f);
ia.filterImage();
ia.repaint();
ia.setActionPerf(actionPerf=true);//tells
the ImgShow object that there is any change to the image
enableSaving(true);
//enable Save as... and Save sub-menu items
}
12 public void enableSaving(boolean f){
msaveas.setEnabled(f);
msave.setEnabled(f);
}
}
public class
ImageBrightness{
public static void main(String
args[]){
new Main();
}
}
Code Explanation:
1 The paint method of the Canvas class has code to display the image.
2 The prepareImage method has code to read the image file, track its loading, define coordinates to
move the original coordinate to and to show the image on the screen, create BufferedImage object from
the original image, and to create a blank BufferedImage object to be the result image of making change to the
original image.
3 The filterImage method is implemented to transfer the result image or updated image to the BufferedImage object bimg for later showing and saving.
4 The setValue method has code to assign the value of the slider to the e variable of the ImgArea class. This value is used in the elements array that is used to construct the kernel object for image filtering process.
5 The setActionPerf method has code to assign the boolean value true or false to the actionPer variable of ImgArea class to indicate any change been made to the image.
6 The createBufferedImageFromImage has code to create an BufferedImage object from the original image.
7 The saveToFile method is invoked when the user select Save as... or Save sub-menu item to save the updated image to you current working folder.
8 The actionPerform method of the ActionListener interface is rewritten to enable menu items selection and do the action accordingly.
9 The setImage method displays the open file dialog for image file selection. The selected image file will be sent to ia object(created from ImgArea class) for showing on the screen.
10 The showSaveDialog is invoked when the user selects Save as.. sub-menu item. Its displays the save file
dialog so the user can save the updated image in a separed file.
11 The stateChanged method of the ChangeListener interface is rewritten to track any change made to the original image.
12 The enableSaving method is invoked to enable or disable the sub-menu items: Save as... or Save. When
the program firstly loads and there is no change to the image, they are disabled. They are reenabled after
any change to the image.
For more detain code explanation, please read the comments in code of the program.
No comments:
Post a Comment