Java 编写生成二维码的代码,Zxing jar包地址分享

项目中需要引入Zxing jar包地址 密码: 密码:76q2。

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class CreateCode {

	public static void main(String[] args) {
		int width = 400; //二维码的宽度
		int height = 400; //二维码的高度
		String format = "png"; //二维码的图片格式
		String content ="二维码的内容";
		
		//定义二维码参数
		
		HashMap hints = new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
		hints.put(EncodeHintType.MARGIN,2);
		
		try {
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
			Path file = new File("D:/image.png").toPath();//二维码生成的位置
			
			MatrixToImageWriter.writeToPath(bitMatrix, format, file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}

你可能感兴趣的:(Java语言)