`
gaojingsong
  • 浏览: 1153228 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

【JAVA之缩略图】

阅读更多

缩略图介绍

缩略图:代表网页上或计算机中图片经压缩方式处理后的小图 ,其中通常会包含指向完整大小的图片的超链接。缩略图用于在 Web 浏览器中更加迅速地装入图形或图片较多的网页。因其小巧,加载速度非常快,故用于快速浏览。相当于图片文件预览及目录的作用。 计算机中,Windows在第一次显示缩略图时,先读取当前目录中的“Thumbs.db"这个文件(隐藏文件),其实这是一个缩略图数据库,从而来判断是否有大图片的缩略图。

 

关闭Windows缩略图缓存

在Windows系统中,为了提高被频繁浏览的缩略图的显示速度,系统会将这些被显示过的图片进行缓存,以便下次打开时直接读取缓存中的信息。可以通过编辑组策略来关闭缩略图缓存的功能,以Windows XP系统为例介绍方法:

在“组策略编辑器”窗口中依次展开“用户配置”→“管理模板”→“Windows组件”目录,并选中“Windows资源管理器”选项。在右窗格中将“关闭缩略图的缓存”策略置于“已启用”状态。

JAVA之缩略图

 

package pic;

 

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Arrays;

 

import javax.imageio.ImageIO;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

public class ImageUtil {

 

    private Logger log = LoggerFactory.getLogger(getClass());

 

    private static String DEFAULT_PREVFIX = "thumb_";

    private static Boolean DEFAULT_FORCE = false;//建议该值为false

 

    /**

     * <p>Title: thumbnailImage</p>

     * <p>Description: 依据图片路径生成缩略图 </p>

     * @param imagePath    原图片路径

     * @param w            缩略图宽

     * @param h            缩略图高

     * @param prevfix    生成缩略图的前缀

     * @param force        是否强制依照宽高生成缩略图(假设为false,则生成最佳比例缩略图)

     */

    public void thumbnailImage(String imagePath, int w, int h, String prevfix, boolean force){

        File imgFile = new File(imagePath);

        if(imgFile.exists()){

            try {

                // ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]

                String types = Arrays.toString(ImageIO.getReaderFormatNames());

                String suffix = null;

                // 获取图片后缀

                if(imgFile.getName().indexOf(".") > -1) {

                    suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1);

                }// 类型和图片后缀所有小写,然后推断后缀是否合法

                if(suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()) < 0){

                    log.error("Sorry, the image suffix is illegal. the standard image suffix is {}." + types);

                    return ;

                }

                log.debug("target image's size, width:{}, height:{}.",w,h);

                Image img = ImageIO.read(imgFile);

                if(!force){

                    // 依据原图与要求的缩略图比例,找到最合适的缩略图比例

                    int width = img.getWidth(null);

                    int height = img.getHeight(null);

                    if((width*1.0)/w < (height*1.0)/h){

                        if(width > w){

                            h = Integer.parseInt(new java.text.DecimalFormat("0").format(height * w/(width*1.0)));

                            log.debug("change image's height, width:{}, height:{}.",w,h);

                        }

                    } else {

                        if(height > h){

                            w = Integer.parseInt(new java.text.DecimalFormat("0").format(width * h/(height*1.0)));

                            log.debug("change image's width, width:{}, height:{}.",w,h);

                        }

                    }

                }

                BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

                Graphics g = bi.getGraphics();

                g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);

                g.dispose();

                String p = imgFile.getPath();

                // 将图片保存在原文件夹并加上前缀

                ImageIO.write(bi, suffix, new File(p.substring(0,p.lastIndexOf(File.separator)) + File.separator + prevfix +imgFile.getName()));

                log.debug("缩略图在原路径下生成成功");

            } catch (IOException e) {

               log.error("generate thumbnail image failed.",e);

            }

        }else{

            log.warn("the image is not exist.");

        }

    }

 

    public static void main(String[] args) {

        new ImageUtil().thumbnailImage("C:/china-drm/20150314_124204.jpg", 200, 300,DEFAULT_PREVFIX,DEFAULT_FORCE);

    }

}

 

结果验证:

1)生成尺寸为300*400的图片



 

2)图片大小28KB,比原图小很多



 

3)图片预览



 

 

  • 大小: 36.9 KB
  • 大小: 40.9 KB
  • 大小: 41.2 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics