压缩图片小工具

 /**
     * 将图片压缩到指定大小以内
     *
     * @param inputStream 源图片数据
     * @param maxSize 目的图片大小
     * @return 压缩后的图片数据
     */
    public static byte[] compress2UnderSize(InputStream inputStream, long maxSize) {
        byte[] bytes = null;
        try {
            bytes = IOUtils.toByteArray(inputStream);
        }catch (IOException e){
            throw new ServiceException("输入流转字节数组失败!");
        }
        return compress2UnderSize(bytes,maxSize);
    }

    /**
     * 将图片压缩到指定大小以内
     *
     * @param inputStream 源图片数据
     * @param specLong 长边压缩规格
     * @param specShort 短边压缩规格
     * @return
     */
    public static byte[] compress2UnderSize(InputStream inputStream, int specLong, int specShort) {
        byte[] bytes = null;
        try {
            bytes = IOUtils.toByteArray(inputStream);
        } catch (IOException e) {
            throw new ServiceException("输入流转字节数组失败!");
        }
        try {
            return compress(bytes, specLong, specShort);
        } catch (IOException e) {
            throw new ServiceException("压缩图片失败!");
        }
    }

    /**
     * 将图片压缩到指定大小以内
     *
     * @param srcImgData 源图片数据
     * @param maxSize 目的图片大小
     * @return 压缩后的图片数据
     */
    public static byte[] compress2UnderSize(byte[] srcImgData, long maxSize) {
        double scale = 0.9;
        byte[] imgData = Arrays.copyOf(srcImgData, srcImgData.length);
        int count=0;
        if (imgData.length > maxSize) {
            do {
                try {
                    imgData = compress(imgData, scale);
                    count++;
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new IllegalStateException("压缩图片过程中出错!");
                }

            } while (imgData.length > maxSize && count < 20);
        }

        return imgData;
    }

    /**
     * 按照 宽高 比例压缩
     *
     * @param scale 压缩刻度
     * @return 压缩后图片数据
     * @throws IOException 压缩图片过程中出错
     */
    public static byte[] compress(byte[] srcImgData, double scale) throws IOException {
        BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));
        int width = (int) (bi.getWidth() * scale); // 源图宽度
        int height = (int) (bi.getHeight() * scale); // 源图高度

        Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = tag.getGraphics();
        g.setColor(Color.RED);
        g.drawImage(image, 0, 0, null); // 绘制处理后的图
        g.dispose();

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        ImageIO.write(tag, "JPEG", bOut);

        return bOut.toByteArray();
    }

    /**
     *
     * @param srcImgData 源图片数据
     * @param specLong 长边压缩规格
     * @param specShort 短边压缩规格
     * @return
     * @throws IOException
     */
    public static byte[] compress(byte[] srcImgData, int specLong, int specShort) throws IOException {
        BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));
        int width = bi.getWidth(); // 源图宽度
        int height = bi.getHeight(); // 源图高度
        int targetWidth;
        int targetHeight;
        if(bi.getWidth()>=bi.getHeight()){
            targetWidth=specLong;
            targetHeight=specShort;
        }else {
            targetWidth=specShort;
            targetHeight=specLong;
        }
        float widthScale = targetWidth / (float) width;// 源图宽度
        float heightScale = targetHeight / (float) height;// 源图高度
        float targetScale = Math.min(widthScale, heightScale);//最终缩小比例
        targetWidth = (int) ((float) width * targetScale);
        targetHeight = (int) ((float) height * targetScale);

        Image image = bi.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
        BufferedImage tag = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);

        Graphics g = tag.getGraphics();
        g.setColor(Color.RED);
        g.drawImage(image, 0, 0, null); // 绘制处理后的图
        g.dispose();

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        ImageIO.write(tag, "JPEG", bOut);

        return bOut.toByteArray();
    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值