usage:java ImageClass e:\Share\Bluehills.jpg e:\Share\ABC 120 80 75 public class ImageClass { public static void main(String[] args) throws Exception { // load image from INFILE Image image = Toolkit.getDefaultToolkit().getImage(args[0]); MediaTracker mediaTracker = new MediaTracker(new Container()); mediaTracker.addImage(image, 0); mediaTracker.waitForID(0); // determine thumbnail size from WIDTH and HEIGHT int thumbWidth = 120; int thumbHeight =80; System.out.println(" thumb Width = "+ thumbWidth + " thumb Height = "+thumbHeight); int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); String outputFile = args[1]; String quality = 75; System.out.println("imageWidth " +imageHeight); System.out.println("imageHeight " +imageWidth); byte[] bytes = imageSizer(imageWidth,imageHeight,thumbWidth,thumbHeight,image,outputFile,quality); Image image1= Toolkit.getDefaultToolkit().createImage(bytes); BufferedImage bi = convert(image1); File file1 = new File("c:
byteImage.jpg"); BufferedOutputStream out; try { out = new BufferedOutputStream(new FileOutputStream(file1)); out.write(bytes); out.flush(); out.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println("Done."); System.exit(0); } private static byte[] imageSizer(int imageWidth,int imageHeight,int thumbWidth,int thumbHeight ,Image image,String outputFile,String qualityOfImage){ byte[] imageAsBytes = null; for(int i = 0 ; i < 3 ; i++){ double thumbRatio = (double)thumbWidth / (double)thumbHeight; System.out.println(" Image Width = "+ imageWidth + " Height = "+imageHeight); if (i == 0){ double imageRatio = (double)imageWidth / (double)imageHeight; System.out.println(" Image Ratio"+imageRatio); if (thumbRatio < imageRatio) { thumbHeight = (int)(thumbWidth / imageRatio); } else { thumbWidth = (int)(thumbHeight * imageRatio); } }else if(i==2){ thumbWidth = imageWidth ; thumbHeight = imageHeight ; }else{ thumbWidth = thumbWidth * 2 ; thumbHeight = thumbHeight * 2 ; } System.out.println(" After Checking Ratio ::: thumb Width = "+ thumbWidth + " thumb Height = "+thumbHeight); // draw original image to thumbnail image object and // scale it to the new size on-the-fly 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(image, 0, 0, thumbWidth, thumbHeight, null); // save thumbnail image to OUTFILE BufferedOutputStream out; try { out = new BufferedOutputStream(new FileOutputStream(outputFile+"_"+i+".jpg")); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder. getDefaultJPEGEncodeParam(thumbImage); int quality = Integer.parseInt(qualityOfImage); quality = Math.max(0, Math.min(quality, 100)); param.setQuality((float)quality / 100.0f, false); encoder.setJPEGEncodeParam(param); encoder.encode(thumbImage); //Encoding it into a byte stream to store it in the database File imageFile = new File(outputFile+"_"+i+".jpg"); imageAsBytes = getBytesFromFile(imageFile); }catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ImageFormatException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } }//end of for loop return imageAsBytes; } /** * * @param im * @return */ public static BufferedImage convert(Image im) { int imgHeight=im.getHeight(null); int imgWidth= im.getWidth(null) ; BufferedImage bi = new BufferedImage( 1024 , 768 , BufferedImage.TYPE_INT_RGB); Graphics bg = bi.getGraphics(); bg.drawImage(im, 0, 0, null); bg.dispose(); return bi; } /** * returns the byte array to the corresponding file * @param file * @return byte[] * @throws IOException */ private static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); final int DEFAULT_BUFFER_SIZE = 1024 * 4; byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int count = 0; int num = 0; while ((num = is.read(buffer)) != -1) { bytestream.write(buffer, 0, num); count += num; } return bytestream.toByteArray(); } }