用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

在java中创建缩略图

2014-03-31 作者: tiansong举报

[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();


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...