Saturday, June 1, 2013

JPEG Converter

The JPEGConverter program allows you to convert different image formats:
-jpeg to png, bmp, and gif
-png to jpeg, bmp, and gif
-bmp to jpeg, png, and gif
-gif to jpeg, png, and bmp.

The main class used in the image conversion is ImageIO. To convert a from an image format to another, firstly you need to transform the original image to an buffered image. Then use the write method of the ImageIO to write the buffered image to destination image format.

Jpeg converter in Java

JPEGConverter 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.geom.*;
import javax.imageio.*;
import javax.imageio.stream.*;

class DrawImage extends Canvas implements ImageObserver{
   
Dimension ds;
String filename;
BufferedImage bi;

DrawImage(){
ds=getToolkit().getScreenSize(); //get the screen size-width and height
filename=null; //initialize the image file name

}

public void paint(Graphics g){

if(filename!=null){
try{
bi=ImageIO.read(new File(filename)); //read the image file
}catch(IOException ie){}
drawIt(g,bi); //invoke the drawIt method to display the image
}

    }

//The drawIt method has code to draw the image on the Canvas drawing object
public void drawIt(Graphics g, BufferedImage img){

Graphics2D g2d=(Graphics2D)g; //create graphics2d object
int w=img.getWidth(this); //get the image width
int h=img.getHeight(this);//get the image height
int mX=(int)ds.getWidth()/2-img.getWidth(this)/2; //x-axis to move the origin x-axis from
int mY=(int)ds.getHeight()/2-img.getHeight(this)/2;//y-axis to move the origin y-axis from
g2d.translate(mX,mY); //move the origin coordinate to the coordinate (mX,mY)
g2d.drawImage(img,0,0,w,h,this);//display the image

}

//The selecteFile method displays the file chooser dialog for image file selection
public void selectFile(){
JFileChooser chooser = new JFileChooser(); //create file chooser object
//create FileNameExtensionFilter object to filter the image files
    FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG, BMP, GIF, PNG Images", "jpg", "bmp","gif","png");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(false); //disable multi-selection
    int returnVal = chooser.showOpenDialog(this); //show the file chooser dialog
    if(returnVal == JFileChooser.APPROVE_OPTION) {
filename=chooser.getSelectedFile().toString(); //ge the selected file path
           
        }
}

//get the image file name
public String getFileName(){
return(filename);
}

//convert from an input image format to the selected image format
public void convert(String toFormat){
try{
File outf=new File(filename.substring(0,filename.lastIndexOf('.')+1)+toFormat);
ImageIO.write(bi,toFormat,outf);
}catch(Exception e){System.out.println("Error...");}


}



}

//The ShowProgram class represents the main interface of the JPEGConverter program
//The program has a menu bar that contains two menus: File and Convert to
//In File menu, you can open the image file and exit the program
//In the Convert to menu, you can convert the image from an input format to another
class  ShowProgram extends JFrame implements ActionListener{

JButton btconvert;
JPanel panel;
DrawImage di;
JMenuBar mainmenu;
JMenu menufile;
JMenu menuconvert;

ShowProgram(){
setTitle("Image Converter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);

mainmenu=new JMenuBar();
menufile=new JMenu("File");
JMenuItem mopen=new JMenuItem("Open...");
mopen.setMnemonic(KeyEvent.VK_O);
mopen.addActionListener(this);
JMenuItem mexit=new JMenuItem("Exit");
mexit.setMnemonic(KeyEvent.VK_X);
mexit.addActionListener(this);
menufile.add(mopen);
menufile.add(mexit);

menuconvert=new JMenu("Convert to");
JMenuItem mjpg=new JMenuItem("JPG");
mjpg.setMnemonic(KeyEvent.VK_G);
mjpg.addActionListener(this);

JMenuItem mbmp=new JMenuItem("BMP");
mbmp.setMnemonic(KeyEvent.VK_B);
mbmp.addActionListener(this);

JMenuItem mgif=new JMenuItem("GIF");
mgif.setMnemonic(KeyEvent.VK_F);
mgif.addActionListener(this);

JMenuItem mpng=new JMenuItem("PNG");
mpng.setMnemonic(KeyEvent.VK_P);
mpng.addActionListener(this);

menuconvert.add(mjpg);
menuconvert.add(mbmp);
menuconvert.add(mgif);
menuconvert.add(mpng);

mainmenu.add(menufile);
mainmenu.add(menuconvert);

setJMenuBar(mainmenu);

btconvert=new JButton("Convert");
btconvert.setBackground(Color.YELLOW);
btconvert.addActionListener(this);
btconvert.setEnabled(true);

di=new DrawImage();
add(di,BorderLayout.CENTER );
setVisible(true);
menuconvert.setEnabled(false);
}

//define actions for menu selections
public void actionPerformed(ActionEvent e) {

JMenuItem source = (JMenuItem)(e.getSource());
if(source.getText().compareTo("Open...")==0)
{
di.selectFile(); //display file chooser dialog box
di.repaint(); //repaint the drawing area
if(!di.getFileName().equals(""))
menuconvert.setEnabled(true); //enable image conversion sub-menu items
}
else if(source.getText().compareTo("Exit")==0)
System.exit(0);
else
di.convert(source.getText()); //convert the image to different format


}



}

public class JPEGConverter{

public static void main(String args[]){
     new ShowProgram(); //display the program interface
 
}


}

No comments:

Post a Comment