Java实现创建缩略图(压缩图片,可自定义压缩or按百分比压缩)

/***
 * 创建缩略图(压缩图片)
 * @throws InterruptedException
 * @throws IOException
 */
public static String reduceImg(String imgsrc) {
    String imgdist = FileUtil.rootPath()+ File.separator +"tc-client"+File.separator+ "pdfFile";
    File file1 = new File(imgdist);
    if (!file1.exists()){
        file1.mkdirs();
    }
    String imagePath = imgdist+ File.separator + DateUtils.getDateStr() + ".png";
    int widthdist = 400;          //自定义压缩
    int heightdist = 250;
    Float rate = 0.0f;          //按原图片比例压缩(默认)
    try {
        File srcfile = new File(imgsrc);
        // 检查图片文件是否存在
        if (!srcfile.exists()) {
            System.out.println("文件不存在");
        }
        // 如果比例不为空则说明是按比例压缩
        if (rate != null && rate > 0) {
            //获得源图片的宽高存入数组中
            int[] results = getImgWidthHeight(srcfile);
            if (results == null || results[0] == 0 || results[1] == 0) {
                return null;
            } else {
                //按比例缩放或扩大图片大小,将浮点型转为整型
                widthdist = (int) (results[0] * rate);
                heightdist = (int) (results[1] * rate);
            }
        }
        // 开始读取文件并进行压缩
        java.awt.Image src = ImageIO.read(srcfile);
        // 构造一个类型为预定义图像类型之一的 BufferedImage
        BufferedImage tag = new BufferedImage((int) widthdist, (int) heightdist, BufferedImage.TYPE_INT_RGB);
        //绘制图像  getScaledInstance表示创建此图像的缩放版本,返回一个新的缩放版本Image,按指定的width,height呈现图像
        //Image.SCALE_SMOOTH,选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。
        tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        //创建文件输出流
        FileOutputStream out = new FileOutputStream(imagePath);
        //将图片按JPEG压缩,保存到out中
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(tag);
        //关闭文件输出流
        out.close();
        return imagePath;
    } catch (Exception ef) {
        ef.printStackTrace();
    }
    return null;
}


/**
 * 获取图片宽度和高度
 * @param
 * @return 返回图片的宽度
 */
public static int[] getImgWidthHeight(File file) {
    InputStream is = null;
    BufferedImage src = null;
    int result[] = { 0, 0 };
    try {
        // 获得文件输入流
        is = new FileInputStream(file);
        // 从流里将图片写入缓冲图片区
        src = ImageIO.read(is);
        result[0] =src.getWidth(null); // 得到源图片宽
        result[1] =src.getHeight(null);// 得到源图片高
        is.close();  //关闭输入流
    } catch (Exception ef) {
        ef.printStackTrace();
    }

    return result;
}

猜你喜欢

转载自blog.csdn.net/qq_38114563/article/details/81408650
今日推荐