java achieve picture compression

Recent business there is a scene: the need to achieve base64 string picture by passing the interface image upload feature, but we all know, even the post request, also have restrictions on the size of the message, and how long the image base64 string then, if not very clear, you can print out look.
Therefore, it is necessary to compress the image, so that the size of the control to the extent feasible, then into base64 string, transmitted over the interface.
Specific codes are as follows:

@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));
    }

}

Reproduced in: https: //www.jianshu.com/p/a542f38c842b

Guess you like

Origin blog.csdn.net/weixin_33728268/article/details/91054897