java创建缩略图

摘要:
voidcreateThumbnail(Stringfilename,intthumbWidth,intthumHeight,intquality,StringoutFilename)throwsInterruptedException,FileNotFoundException,IOException{//loadimagefromfilenameImageimage=Toolkit.getD
voidcreateThumbnail(String filename, intthumbWidth, intthumbHeight,
             intquality, String outFilename)
        throwsInterruptedException, FileNotFoundException, IOException
{
        // load image from filename
        Image image = Toolkit.getDefaultToolkit().getImage(filename);
        MediaTracker mediaTracker = newMediaTracker(newContainer());
        mediaTracker.addImage(image, 0);
        mediaTracker.waitForID(0);
        // use this to test for errors at this point:
    // System.out.println(mediaTracker.isErrorAny());
        // determine thumbnail size from WIDTH and HEIGHT
        doublethumbRatio = (double)thumbWidth / (double)thumbHeight;
        intimageWidth = image.getWidth(null);
        intimageHeight = image.getHeight(null);
        doubleimageRatio = (double)imageWidth / (double)imageHeight;
        if(thumbRatio < imageRatio) {
            thumbHeight = (int)(thumbWidth / imageRatio);
        } else{
            thumbWidth = (int)(thumbHeight * imageRatio);
        }
    
        // draw original image to thumbnail image object and
        // scale it to the new size on-the-fly
        BufferedImage thumbImage = newBufferedImage(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 outFilename
        BufferedOutputStream out = newBufferedOutputStream(newFileOutputStream(outFilename));
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
        quality = Math.max(0, Math.min(quality, 100));
        param.setQuality((float)quality / 100.0f, false);
        encoder.setJPEGEncodeParam(param);
        encoder.encode(thumbImage);
        out.close();
}

免责声明:文章转载自《java创建缩略图》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇微软脱机实验五十微软应用程序虚拟化之五APPV 5.1脱机使用应用程序游戏攻略 Re:LieF ~親愛なるあなたへ~ (relief给挚爱的你)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

android 桌面文件平夹

引用:http://blog.csdn.net/hmg25/article/details/6574575 哈哈,好久没有写博客拉,blog里边好凄凉阿~~人也变懒了……     android原生自带的桌面文件夹样式及其简单,没有iphone那种可以显示文件夹内文件图标缩略图的功能,今天我们来简单的实现一个。 效果如下: 从launcher源码中很容易变...

【技术贴】解决相册thumbnails文件过大 小米缩略图缓存根除办法

命令:cd.> I:\DCIM\.thumbnails I为你的小米插到电脑上的usb的盘符,进到你的usb里面的此文件夹中,删除.thumbnails文件夹,然后把上面的命令右键复制,然后再cmd里面右键粘贴,然后打个回车即可。 此时,你的usb里面就会生成 ok 开始观察你的内存卡剩余容量吧。看看还会不会继续沾满你的USB。如果此方法不错,请...

微信分享网页的缩略图

微信公众平台很多时候都需要跳转到网页展示和实现一些功能,而这些网页也是可以被用户分享到朋友圈或发送给朋友的。分享和发送的时候一般都是一个缩略图,一个标题和一个介绍。可是有时候分享的网页缩略图部分是空白的,虽然网页里是包含了图片的,所以就需要研究一下这个缩略图到底怎么。...

照片上传(缩略图实现)

1.获取所有的提交到服务器的文件集合 HttpFileCollection fileColl= Request.Files; 2.取得一个文件(这里是一张照片)     HttpPostedFile pic = fileColl[0]; 3.判断文件是否为空     1.获取服务器存放图片的物理路径(Server.MapPath)        strin...

iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)

本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文《iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)》,主要是干货环节,列举了如何基于 PhotoKit 与 AlAssetLibrary 封装出通用的方法...

iOS 开发之照片框架详解(3)

http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 三. 常用方法的封装 虽然 PhotoKit 的功能强大很多,但基于兼容 iOS 8.0 以下版本的考虑,暂时可能仍无法抛弃 ALAssetLibrary,这时候一个比较好的方案是基于 ALAs...