[Java] 在java中创建缩略图 →→→→→进入此内容的聊天室

来自 , 2019-05-12, 写在 Java, 查看 121 次.
URL http://www.code666.cn/view/1f50893f
  1. private void createThumbnail(String filename, int thumbWidth,intthumbborder="1" Height, int
  2.   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
  3.   (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,
  4.   BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D=thumbImage.createGraphics();graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  5.   (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();
  6.   private void createThumbnail(String filename, int thumbWidth,intthumbborder="1" Height, int
  7.   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
  8.   (mediaTracker.isErrorAny());
  9.   // 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);
  10.   // draw original image to thumbnail image t and// scale it tothenew size on-the-flyBufferedImage thumbImage =newBufferedImage(thumbWidth,
  11.   thumbborder="1" Height, BufferedImage.TYPE_INT_RGB);Graphics2Dgraphics2D=thumbImage.createGraphics();graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  12.   // save thumbnail image to outFilenameBufferedOutputStream out=new BufferedOutputStream(new
  13.   FileOutputStream(outFilename));JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);JPEGEncodem m=encoder.getDefaultJPEGEncodem
  14.   (thumbImage);quality = Math.max(0,Math.min(quality,100));m.setQuality((float)quality /100.0f,false);encoder.setJPEGEncodem(m);encoder.encode(thumbImage);out.close();

回复 "在java中创建缩略图"

这儿你可以回复上面这条便签

captcha