It is the segment of code which is developed for reducing the size of PDF (which is getting generated through application), which contains images.
It is achieved by decreasing the quality of images.
Code can be used for:
1. Converting any type of image to JPEG.
2. Changing the Height / width of image.
3. Changing the quality, effectively size (number of bytes of image)
It is achieved by decreasing the quality of images.
Code can be used for:
1. Converting any type of image to JPEG.
2. Changing the Height / width of image.
3. Changing the quality, effectively size (number of bytes of image)
// Imports required: import java.awt.Color; import java.awt.Container; import java.awt.MediaTracker; import java.awt.RenderingHints; import java.awt.Toolkit; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import com.lowagie.text.Image; import com.lowagie.text.Element; import java.awt.Graphics2D; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGCodec; import java.awt.image.BufferedImage; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.Phrase; import com.lowagie.text.Rectangle; import com.lowagie.text.Table; // Code Block 1: private Image getImage(String StrfileName){ Image imgImage = null; try{ String fullFilePath = SVRSystemConstants.IMAGE_UPLOAD_DIR + "/"+StrfileName; java.awt.Image imgAWT = null; imgAWT = Toolkit.getDefaultToolkit().getImage(fullFilePath); MediaTracker mediaTracker = new MediaTracker(new Container()); mediaTracker.addImage(imgAWT, 0); mediaTracker.waitForID(0); int thumbWidth = Integer.parseInt("360"); int thumbHeight = Integer.parseInt("144"); double thumbRatio = (double)thumbWidth / (double)thumbHeight; int imageWidth = imgAWT.getWidth(null); int imageHeight = imgAWT.getHeight(null); double imageRatio = (double)imageWidth / (double)imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int)(thumbWidth / imageRatio); } else { thumbWidth = (int)(thumbHeight * imageRatio); } BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(imgAWT, 0, 0, thumbWidth, thumbHeight, null); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(SVRSystemConstants.IMAGE_UPLOAD_DIR + "/"+"Q"+StrfileName)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder. getDefaultJPEGEncodeParam(thumbImage); param.setQuality(0.5f, false); encoder.setJPEGEncodeParam(param); encoder.encode(thumbImage); out.close(); imgImage = Image.getInstance(SVRSystemConstants.IMAGE_UPLOAD_DIR + "/Q"+StrfileName); return imgImage; }catch( Exception e ){ SVRLogger.getInstance().log("Exception : "+e.getMessage()); e.printStackTrace(); return null; } } // Code Block 2: for(int i=0;arrImgNamTemp!= null && i<arrImgNamTemp.size();i++) { File myFile = new File(SVRSystemConstants.IMAGE_UPLOAD_DIR + "/"+"e"+arrImgNamTemp.get(i).toString().trim()); myFile.delete(); }