java实现图片压缩功能

最近的业务里有一个场景:需要通过接口传递图片的base64字符串实现图片的上传功能,但众所周知,即使是post请求,也对报文的大小有所限制,而图片的base64字符串有多长呢,如果不是很清楚的话,可以打印出来看一下。
因此,需要将图片进行压缩,使大小控制在可行的范围内,再转成base64字符串,通过接口进行传输。
具体实现代码如下:

@Slf4j
public class ImageUtilJnb {
    private static int mb = 1048576;// 1MB

    public static void main(String[] args) {
        String fullPath="D:\\123.jpg";
        String newPath="D:\\321322198511205873F.jpg";

        long length = new File(fullPath).length();
        if (length / 1048576 >= JnbConstants.JNB_IMAGE_RATE) {// 如果图片大于等于0.5MB 则按80%缩小
            try {
                ImageUtilJnb.zoom(fullPath, newPath, length, JnbConstants.JNB_IMAGE_RATE);
            } catch (IOException e) {
                log.error("出错啦:" + e);
            }
        }

        String base64Str = Base64Util.getBase64Str(newPath);
        System.out.println("base64Str===" + base64Str);

    }

    // 缩图
    public static void zoom(String oldFile, String zoomFile,long length,double newRate) throws IOException {
        DecimalFormat df = new DecimalFormat("0.00");// 设置保留位数
        double rate=0.8;
        log.info("原始图片大小:" + df.format((float) length / mb) + "MB");
        long newfile = new File(oldFile).length();
        int i = 1;
        // 如果首次压缩还大于2MB则继续处理
        while ((float) newfile / mb >= newRate) {
            log.info("压缩后图片大小:" + newfile);
            rate = rate - 0.05;// 暂定按照0.03频率压缩
            log.info(i + " rate=" + rate);
            BufferedImage srcImage = ImageIO.read(new File(oldFile));
            int WIDTH = (int) (srcImage.getWidth() * rate);
            int HEIGHT = (int) (srcImage.getHeight() * rate);
            BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics g = image.getGraphics();
            g.drawImage(srcImage, 0, 0, WIDTH, HEIGHT, null);
            // 缩小
            ImageIO.write(image, "jpg", new File(zoomFile));
            i++;
            newfile = new File(zoomFile).length();
            log.info("压缩次数:" + i);
        }
        // 调整方向
         BufferedImage newImage = ImageIO.read(new File(zoomFile));
         BufferedImage image1 = Rotate(newImage, 90);// 顺时针旋转90度
         ImageIO.write(image1, "jpg", new File(zoomFile));
        log.info("处理后图片路径:" + zoomFile + ";缩小之后大小:"
                + df.format((float) new File(zoomFile).length() / mb) + "MB");
    }

    /**
     * 对图片进行旋转
     *
     * @param src
     *            被旋转图片
     * @param angel
     *            旋转角度
     * @return 旋转后的图片
     */
    public static BufferedImage Rotate(Image src, int angel) {
        int src_width = src.getWidth(null);
        int src_height = src.getHeight(null);
        // 计算旋转后图片的尺寸
        Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel);
        BufferedImage res = null;
        res = new BufferedImage(rect_des.width, rect_des.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = res.createGraphics();
        // 进行转换
        g2.translate((rect_des.width - src_width) / 2, (rect_des.height - src_height) / 2);
        g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);

        g2.drawImage(src, null, null);
        return res;
    }

    /**
     * 计算旋转后的图片
     *
     * @param src
     *            被旋转的图片
     * @param angel
     *            旋转角度
     * @return 旋转后的图片
     */
    public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
        // 如果旋转的角度大于90度做相应的转换
        if (angel >= 90) {
            if (angel / 90 % 2 == 1) {
                int temp = src.height;
                src.height = src.width;
                src.width = temp;
            }
            angel = angel % 90;
        }

        double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
        double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
        double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
        double angel_dalta_width = Math.atan((double) src.height / src.width);
        double angel_dalta_height = Math.atan((double) src.width / src.height);

        int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_width));
        int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height));
        int des_width = src.width + len_dalta_width * 2;
        int des_height = src.height + len_dalta_height * 2;
        return new Rectangle(new Dimension(des_width, des_height));
    }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,136评论 1 32
  • 1、截取字符串”20 | http://www.baidu.com”中,”|”字符前面和后面的数据,分别输出它们 ...
    强子ly阅读 3,013评论 8 46
  • 简书已停更,欢迎转到个人博客查看对应教程:https://www.cnblogs.com/superhin/p/1...
    韩志超阅读 47,025评论 6 214
  • Web 页面的实现 Web 基于 HTTP 协议通信 客户端(Client)的 Web 浏览器从 Web 服务器端...
    毛圈阅读 1,123评论 0 2
  • 参考文献链表翻转的图文讲解(递归与迭代两种实现)
    四喜汤圆阅读 247评论 0 0