图片压缩

/**
 * 图片压缩
 */
@Slf4j
public class CompressImageUtil {
    /**
     * 根据指定大小和指定精度压缩图片
     *
     * @param desFileSize
     *            指定图片大小,单位kb
     * @param accuracy
     *            精度,递归压缩的比率,建议小于0.9
     *  @param desMaxWidth
     *             目标最大宽度
     *  @param desMaxHeight
     *             目标最大高度
     * @return 目标文件路径
     */
    public static  byte[] commpressPicForScale(byte[] src,long desFileSize, double accuracy,int desMaxWidth,int desMaxHeight) {
        byte[] des=null;
        if (src==null) {
            return null;
        }
        try {
            long srcFileSize = src.length;
            log.info("源图片:大小:" + srcFileSize / 1024 + "kb");
            //获取图片信息
            ByteArrayInputStream in = new ByteArrayInputStream(src);    //将b作为输入流;
            BufferedImage bim = ImageIO.read(in);
            int srcWidth = bim.getWidth();
            int srcHeight = bim.getHeight();

            //先转换成jpg
            Thumbnails.Builder builder = Thumbnails.of(bim).outputFormat("jpg");

            // 指定大小(宽或高超出会才会被缩放)
            if(srcWidth > desMaxWidth || srcHeight > desMaxHeight) {
                builder.size(desMaxWidth, desMaxHeight);
            }else{
                //宽高均小,指定原大小
                builder.size(srcWidth,srcHeight);
            }

            // 写入到内存
            ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节输出流(写入到内存)
            builder.toOutputStream(baos);

            // 递归压缩,直到目标文件大小小于desFileSize
            des = commpressPicCycle(baos.toByteArray(), desFileSize, accuracy);

            // 输出到文件
//            File desFile = new File("C:\\222222.png");
//            FileOutputStream fos = new FileOutputStream(desFile);
//            fos.write(des);
//            fos.close();
            log.info("目标图片:大小" + des.length / 1024 + "kb");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return des;
    }

    private static byte[] commpressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException {
        // File srcFileJPG = new File(desPath);
        long srcFileSizeJPG = bytes.length;
        // 2、判断大小,如果小于500kb,不压缩;如果大于等于500kb,压缩
        if (srcFileSizeJPG <= desFileSize * 1024) {
            return bytes;
        }
        // 计算宽高
        BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes));
        int srcWdith = bim.getWidth();
        int srcHeigth = bim.getHeight();
        int desWidth = new BigDecimal(srcWdith).multiply(
                new BigDecimal(accuracy)).intValue();
        int desHeight = new BigDecimal(srcHeigth).multiply(
                new BigDecimal(accuracy)).intValue();

        ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节输出流(写入到内存)
        Thumbnails.of(new ByteArrayInputStream(bytes)).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos);
        return commpressPicCycle(baos.toByteArray(), desFileSize, accuracy);
    }

    public static void main(String[] arg) throws IOException {
        /**
         * asBufferedImage() 返回BufferedImage
         */
//        BufferedImage thumbnail = Thumbnails.of("C:\\2.png").size(1280, 1024).asBufferedImage();
//        ImageIO.write(thumbnail, "png", new File("C:\\1.png"));
        byte[] imgBytes = ImageUtil.image2byte("https://a.jpg");
        commpressPicForScale(imgBytes,1000,(long)0.8,700,1000);
    }
  /**
     * 从html中获取中文
     * @return
     */
    public static String getChinese(String content){
        String txtcontent = content.replaceAll("</?[^>]+>", ""); //剔出<html>的标签
        txtcontent = txtcontent.replaceAll("<a>\\s*|\t|\r|\n</a>", "");//去除字符串中的空格,回车,换行符,制表符
        txtcontent = txtcontent.replaceAll("&nbsp;", "");
        txtcontent = txtcontent.replaceAll("&amp", "");
        return txtcontent;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值