[java]代码库
private void createThumbnail(String filename, int thumbWidth,intthumbborder="1" Height, int
quality, String outFilename) throwsInterruptedException,FileNotFoundException, IOException // loadimage from filenameImage image =Toolkit.getDefaultToolkit().getImage(filename);MediaTrackermediaTracker = new MediaTracker(newContainer());mediaTracker.addImage(image, 0);mediaTracker.waitForID(0); // usethis to test for errors at thispoint: System.out.println
(mediaTracker.isErrorAny()); // determine thumbnail sizefromWIDTH and border="1" Height double thumbRatio =(double)thumbWidth /(double)thumbborder="1" Height; int image Width= image.getWidth(null); intimageborder="1" Height =image.getborder="1" Height(null); double imageRatio=(double)imageWidth / (double)imageborder="1" Height; if(thumbRatio <imageRatio) thumbborder="1" Height =(int)(thumbWidth / imageRatio); elsethumbWidth =(int)(thumbborder="1" Height * imageRatio); // draw originalimageto thumbnail image t and // scale it to the new sizeon-the-flyBufferedImage thumbImage = newBufferedImage(thumbWidth,thumbborder="1" Height,
BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D=thumbImage.createGraphics();graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
(outFilename)); JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); JPEGEncodem m=encoder.getDefaultJPEGEncodem(thumbImage); quality =Math.max(0,Math.min(quality, 100)); m.setQuality((float)quality /100.0f,false); encoder.setJPEGEncodem(m);encoder.encode(thumbImage);out.close();
private void createThumbnail(String filename, int thumbWidth,intthumbborder="1" Height, int
quality, String outFilename)throwsInterruptedException,FileNotFoundException, IOException// loadimage from filenameImageimage =Toolkit.getDefaultToolkit().getImage(filename);MediaTrackermediaTracker= newMediaTracker(newContainer());mediaTracker.addImage(image,0);mediaTracker.waitForID(0);//use this to test for errors at thispoint: System.out.println
(mediaTracker.isErrorAny());
// determine thumbnail size from WIDTH and border="1"HeightdoublethumbRatio = (double)thumbWidth /(double)thumbborder="1" Height;intimageWidth =image.getWidth(null);int imageborder="1" Height=image.getborder="1" Height(null);double imageRatio =(double)imageWidth /(double)imageborder="1" Height;if (thumbRatio< imageRatio) thumbborder="1" Height =(int)(thumbWidth /imageRatio); else thumbWidth = (int)(thumbborder="1" Height*imageRatio);
// draw original image to thumbnail image t and// scale it tothenew size on-the-flyBufferedImage thumbImage =newBufferedImage(thumbWidth,
thumbborder="1" Height, BufferedImage.TYPE_INT_RGB);Graphics2Dgraphics2D=thumbImage.createGraphics();graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
// save thumbnail image to outFilenameBufferedOutputStream out=new BufferedOutputStream(new
FileOutputStream(outFilename));JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);JPEGEncodem m=encoder.getDefaultJPEGEncodem
(thumbImage);quality = Math.max(0,Math.min(quality,100));m.setQuality((float)quality /100.0f,false);encoder.setJPEGEncodem(m);encoder.encode(thumbImage);out.close();