java实现二维码生成

导入zxing.jar去百度盘下
链接:https://pan.baidu.com/s/1gmzzpmoKHvrMlx2F43zitw
提取码:u1sb

上代码看注释

package com.moon;

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.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

//生成二维码
public class CreatQRCode {

	public static void main(String[] args) {
			int width = 300;
			int height = 300;
			String format = "png";
			String content="http://www.baidu.com";
			
			//定义二维码的参数
			HashMap hints = new HashMap();
			
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码问题
			
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//纠错等级
			hints.put(EncodeHintType.MARGIN, 2);//调整空白区
			
			try {
				BitMatrix bitMatrix=new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE , width, height,hints);
				
				Path file=new File("D:/code/img"+i+".png").toPath();
				MatrixToImageWriter.writeToPath(bitMatrix, format, file);
				System.out.println("二维码已生成");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
}

你可能感兴趣的:(java)