skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, September 19, 2012

Image Resizing using JAI

Posted by Admin at 6:47 AM – 0 comments
 

Images of different formats like jpeg,png,tiff,bmp,fpx,pnm etc can be resized to a different size using Java Advanced Imaging


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 ImageResize{

   String validFormats[] = {"BMP","GIF","FPX","JPG","PNG","PNM","TIFF","TIF","JPEG"};

   static String destFolder = null;
   static String thumbNailFolder = null;

   static int imgWidth = 0;
   static int imgHeight = 0;

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


                Properties prop = new Properties();
                File file = new File("JAIProps.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 + "/";
                }

    imgWidth = Integer.parseInt(prop.getProperty("IMG_WIDTH"));

    imgHeight = Integer.parseInt(prop.getProperty("IMG_HEIGHT"));

           ImageResize imgResize = new ImageResize();

      imgResize.imageProcess(srcFolder);



        }

private void imageProcess(String src)
{
   File file = new File(src);
   //File photos[] = file.listFiles();
   File photos[] = 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();
         resizePhoto(src,fileName);
      }
   }

}


private void resizePhoto( String imgFolder,String fileName){


         try{


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

                float width = objImage.getWidth();
                float height = objImage.getHeight();


                float photoFactor = Math.min(imgWidth/width, imgHeight/height);
                ParameterBlock pbPhoto = getParamBlock(objImage, photoFactor);
                RenderedImage objImage1 = JAI.create( "scale", pbPhoto );

    String photoPath = destFolder+fileName;
    JAI.create("filestore", objImage1 , photoPath,null,null );

                //float thumbnailFactor = Math.min(27/width, 36/height);
                //ParameterBlock pbThumb = getParamBlock(objImage, thumbnailFactor);
                //RenderedImage objImage2 = JAI.create( "scale", pbThumb );
     //String thumbNailPath = thumbNailFolder +fileName;
     // JAI.create("filestore", objImage2 , thumbNailPath ,null,null );



           }
     catch(FileNotFoundException fe)
      {
                  System.out.println(fe.getMessage());
           }
    catch(Exception ioe)
     {
            System.out.println("Exception ..."+ioe.getMessage());


          }


}

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