Showing posts with label converter. Show all posts
Showing posts with label converter. Show all posts

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
 
}


}

Tuesday, April 2, 2013

Measurement Converter program

The MeasurementConvert program is able to convert from an input number in inch to foot, and centimeter measurements. In this program, you will learn to use the nextFloat() method of Scanner class to get user input, to do some calculations with floating-point numbers, and to format the output by using the format(Format, Text) method.

MeasurementConvert source code:

1. import java.util.Scanner;
2.  class MeasurementConverter
   {

3.    public static void main(String[] args)
    {

4.       float inch, foot, cent;
5.       Scanner sc=new Scanner(System.in);
6.       System.out.print("Input a number in inches:");
7.       inch=sc.nextFloat();
8.       foot = inch/12.0f;//1 foot=12 inches
9.     cent = inch*2.54f;//1 inch=2.54 centimeters
10.     System.out.format("%-10s%-10s%-10s\n", "INCH","FOOT","CENTIMETER");
11.     System.out.format("%-10.3f%-10.3f%-10.3f\n",inch,foot,cent);


12.  }

13.}

Program Output
Input a number in inch: 12
INCH            FOOT            CENTIMETER
12.000            1.000            30.480

Code Explanation
1. Include the Scanner class. By including the Scanner class in the program, you will have nextFloat() method
to read a floating-pointer number from the keyboard.
2. Open MeasurementConverter class block.
3. Open the main method block. The Java program begins from the main method.
4. Declare local variable inch, foot, and cent that are used to store measurements in inch, foot, and centimeter.
5. Create Scanner object. The constructor of the Scanner class can wrap the System.in class to allow number input from keyboard.
6. Tell the user to input a number in inch.
7. Receive the number input.
8. Convert the number from inch to foot.
9. Convert the number from inch to centimeter.
10. Display heading text. Each heading column text is displayed ten characters wide and left aligned.
11. Display the result in inch, foot, and centimeter. Each floating-point number is ten characters wide, left aligned, and shows three decimal places.

Note: \n is a special character that is used to output a new line on the screen.