skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, September 19, 2012

JPEG Converter - Convert Image Format

Posted by Admin at 7:13 AM – 0 comments
 

Sample code to convert image formats like bmp,gif,tiff,png,fpx etc to JPEG format. The images stored in jpeg format will be of less size compared to other formats


import java.io.*;
import java.util.*;
import java.awt.image.renderable.*;
import javax.media.jai.*;
import com.sun.media.jai.codec.*;
import java.awt.image.RenderedImage; 




public class ImageConverter{

 private RenderedOp image = null;
 private RenderedOp result = null;
 
 String validFormats[] = {"BMP","GIF","FPX","JPG","PNG","PNM","TIFF","TIF","JPEG"}; 
 
 static String destFolder = null;
 static String thumbNailFolder = null;


public static void main(String args[])throws Exception{ 


                Properties prop = new Properties(); 
                File file = new File("JAI.properties"); 
                FileInputStream fis = new FileInputStream(file); 
                InputStream ins = fis; 
                prop.load(ins); 
                 
               String srcFolder =  prop.getProperty("SRC_FOLDER_PATH");                 
     destFolder = prop.getProperty("DEST_FOLDER_PATH");  
     thumbNailFolder = prop.getProperty("THUMBNAIL_FOLDER_PATH"); 
                               
                if(srcFolder .charAt(srcFolder .length()-1) != '/') 
                { 
                        srcFolder = srcFolder + "/"; 
                } 

         ImageConverter jaiUtils = new ImageConverter();
  
  jaiUtils.imageProcess(srcFolder);
  
                
        } 

private void imageProcess(String src)
{
 File file = new File(src);
 File photos[] = file.listFiles();
 //File listFiles[] = getValidImages(src,validFormats); 
 if(photos!= null && photos.length>0)
 { 
  for(int i=0;i<photos.length; i++)
  {
   File imgFile = photos[i];
   String fileName = imgFile.getName();
   convertToJPEG(src,fileName);
  }
 }

}



private void convertToJPEG(String imgFolder,String fileName){ 
         RenderedOp op = null; 
         byte byteArr[] = null;                                         
         try{ 


  InputStream ins = new FileInputStream(imgFolder+fileName); 
         SeekableStream s = SeekableStream.wrapInputStream(ins, true);         
            RenderedImage objImage = JAI.create( "stream", s); 

                           
  String jpegFileName = fileName.substring(0,fileName.indexOf('.'))+".jpg";
  FileOutputStream os = new FileOutputStream(destFolder+jpegFileName);
            
  JPEGEncodeParam encodeParam = new JPEGEncodeParam(); 
       //FileOutputStream os = new FileOutputStream("D:/thumbnails/copy1.jpg");             
            ImageEncoder encoderImage = ImageCodec.createImageEncoder("JPEG", os, encodeParam);                                                     
            encoderImage.encode(objImage); 
                                                                                                                
             
           } 
   catch(FileNotFoundException fe)
    { 
                  System.out.println(fe.getMessage()); 
           }
  catch(Exception ioe)
  { 
                          System.out.println("Exception ..."+ioe.getMessage()); 
                        //  System.out.println("The file which cannot be processed....."+fileName); 
                         // String imgNames[] = {fileName}; 
                        //  writeNamesToFile(imgNames); 

               } 
                        
     //return byteArr; 
} 

private static ParameterBlock getParamBlock(RenderedImage objImage, float imgFactor){ 
                                        Interpolation interp = Interpolation.getInstance( Interpolation.INTERP_BICUBIC ); 
                                        ParameterBlock pb = new ParameterBlock(); 
                                        pb.addSource(objImage); // The source image 
                                        pb.add(imgFactor);         // The xScale 
                                        pb.add(imgFactor);         // The yScale 
                                        pb.add(0.0F);           // The x translation 
                                        pb.add(0.0F);           // The y translation 
                                        pb.add(interp); // The interpolation 

                                return pb; 

        } 


/* 
         *   Method for fetching the image files with valid name and extension only. 
         * Method: getValidImage 
         * @param validFormats 
         * @param strPersonId 
         * @return File[] of valid files 
         */ 
        private static File[] getValidImages(String srcFolder,String[] validFormats){ 

                File file = new File(srcFolder); 
                FilenameFilter onlyValidImg = new ImageFilter(validFormats); 
                return file.listFiles(onlyValidImg); 
        } 

}



//..................................Class ImageFilter................................................................ 
/*        Class to filter the files which are of valid formats and corresponding to the personID 
* This class implements FilenameFilter 
* 
*/ 
class ImageFilter implements FilenameFilter{ 

        boolean validFileFlag; 
        String validFormats[]; 
       
        ImageFilter(String validFormats[]){ 
                        this.validFormats = validFormats;                         
        } 

        /* 
        * Method which returns true if  the files fit into the criteria given, like valid extension and format 
        * Method: accept 
        * @param dir 
        * @param name 
        * @return boolean true or false 
        */ 
        public boolean accept(File dir, String name){ 

                        String extension = null; 
                        if(name.indexOf(".") == -1) 
                                return validFileFlag; 

                        if(name.indexOf(".")!= -1) 
                                extension = name.substring(name.indexOf(".")+1); 
                         
                        //System.out.println("ImageFilter class........"+personIdInName +" - "+extension); 
                        if((null != extension && "" != extension) ) { 
                                for(int i=0; i<validFormats.length; i++){ 

                                                if(extension.equalsIgnoreCase(validFormats[i])){ 

                                                        validFileFlag = true; 
                                                        break; 
                                                } 
                                                else{ 

                                                        validFileFlag = false; 
                                                } 
                                } 
                        } 
                return validFileFlag; 
        } 
}


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger