java 创建缩略图_在java中创建缩略图

[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

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

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值