How to compress pictures in Java

problem background

When the picture is too large, it will cause the page to freeze or even report an error, and now pages, interfaces, and many places have maximum limit requirements for message transmission. In addition, I don’t know if you have ever encountered a page rendering a relatively large base64 picture. , it will be very stuck. Therefore, we must compress the original pictures uploaded by users.


Why does the image file become larger after base64 encoding conversion?

After the image is converted by base64 encoding, the file will become larger because base64 encoding will convert each 3-byte data into 4-byte data, and some extra characters will be added during the conversion process. These extra characters include "=", "+", "/", etc., which do not exist in the original image data.

So when we base64 encode the image, it makes the data bigger because it needs more characters to represent the same original data.

In addition, using base64 encoding will also slow down the network transmission speed, because the same data needs to transmit more characters. Therefore, it is recommended to use raw binary data instead of base64 encoding when large amounts of data need to be transferred.


 

solution

1. Read the source image first

        new ImgCompress(srcFilePath);

2. Perform image compression

        resize(int w, int h, String toPic)

3. The source code tools are as follows:

package com.example.util;

import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
/**
 * 图片压缩
 * @author 86183
 *
 */
@SuppressWarnings("restriction")
public class ImgCompress {
	static BufferedImage img = null;
	public static void main(String[] args) throws IOException {
		
		String fromPic = "C:\\Users\\86183\\Pictures\\儿童节插画手绘人物.png";
		String toPic = "C:\\Users\\86183\\Pictures\\儿童节插画手绘人物_min.png";
		ImgCompress imgCom = new ImgCompress(fromPic );
		imgCom.resize(400, 400, toPic);

		
	}
	/**
	* 构造函数
	*/
	public ImgCompress(String fileName) throws IOException {

	     File file = new File(fileName);// 读入文件
	     img = ImageIO.read(file);      // 构造Image对象
	}

	/**
	 * 强制压缩/放大图片到固定的大小
	 *
	 * @param w int 新宽度
	 * @param h int 新高度
	 */
	public void resize(int w, int h, String toPic) throws IOException {
	// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
	    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
	    image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
	    File destFile = new File(toPic);

	    FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
	    // 可以正常实现bmp、png、gif转jpg
	    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
	    encoder.encode(image); // JPEG编码
	    out.close();
	}
}

Remark

Here we use the dependency package under jdk

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

Why can't the package under jdk be packaged when maven is packaged?

If your maven project relies on packages under the JDK, but these packages are not packaged when packaging, it may be because maven only packages the jar packages that the project depends on by default, and packages under the JDK are considered system Level dependencies will not be automatically added to the packaged jar.

To solve this problem, there are two common methods:

1. Introduce maven dependencies of JDK packages
You can add dependencies similar to the following to pom.xml, and import packages under JDK into maven projects, so that they can be packaged:

<dependency>
    <groupId>jdk.tools</groupId>
    <artifactId>jdk.tools</artifactId>
    <version>${java.version}</version>
    <scope>system</scope>
    <systemPath>${java.home}/lib/tools.jar</systemPath>
</dependency>

2. Manually add JDK packages
If you do not want to introduce dependencies, you can also manually add packages under the JDK to the packaged jar file, and you can add the following parameters to the maven package command:

mvn package -Dmaven.compiler.includeJavaxPackages=true

In this way, the packages under the JDK will be included when packaging.


When maven is packaging, it will prompt that the package cannot be found. Here we need to add a packaging dependency setting item in the maven POM file.

<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
					<encoding>UTF-8</encoding>
					<compilerArguments>
                       <verbose />
                       <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
                   </compilerArguments>
				</configuration>
			</plugin>

 The added locations are as follows: 

 Additional content: the way of judging the file size by front-end Jquery and background Java.

front end:
var fileSize = $("#"+fileId)[0].files[0].size/(1024*1024);
console.log("上传文件大小:"+fileSize+"M");

The above variable fileId  is the id value of the file input box.

rear end:
/**
     * 判断文件大小处于限制内
     *
     * @param fileLen 文件长度
     * @param fileSize 限制大小
     * @param fileUnit 限制的单位(B,K,M,G)
     * @return
     */
	public static boolean checkFileSizeIsLimit(Long fileLen, int fileSize, String fileUnit) {
      double fileSizeCom = 0;
      if ("B".equals(fileUnit.toUpperCase())) {
          fileSizeCom = (double) fileLen;
      } else if ("K".equals(fileUnit.toUpperCase())) {
          fileSizeCom = (double) fileLen / 1024;
      } else if ("M".equals(fileUnit.toUpperCase())) {
          fileSizeCom = (double) fileLen / (1024*1024);
      } else if ("G".equals(fileUnit.toUpperCase())) {
          fileSizeCom = (double) fileLen / (1024*1024*1024);
      }
      if (fileSizeCom > fileSize) {
          return false;
      }
      return true;
  }

Just use the tool class directly, the code is simple and clear, and there is not much to explain and note.

Guess you like

Origin blog.csdn.net/weixin_36754290/article/details/131381415