ZXing生成二维码

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

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

import jp.sourceforge.qrcode.util.Color;

public class ZXingTest {
	
	public void encodeImg(String content,String imagePath,int width,int height) throws Exception {
		MultiFormatWriter mf = new MultiFormatWriter();
		Map<EncodeHintType,Object> map = new HashMap<>();
		map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);	//排错率
		map.put(EncodeHintType.CHARACTER_SET,"utf-8");
		map.put(EncodeHintType.MARGIN,1);
		BitMatrix bitMatrix = mf.encode(content, BarcodeFormat.QR_CODE, width, height, map);
		BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		for(int x = 0;x < width; x++) {
			for(int y = 0;y < height; y++) {
				image.setRGB(x, y, (bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE));
			}
		}
		image.flush();
		ImageIO.write(image,"gif",new File(imagePath));
	}
	
	public static void main(String[] args) throws Exception {
		new ZXingTest().encodeImg("https://www.hgjun.cn","D:/图片/zing.gif",430,430);
	}
}
ZXing生成二维码

原创文章,作者:LeoRiver,如若转载,请注明出处:https://www.hgjun.cn/?p=1018

发表评论

电子邮件地址不会被公开。 必填项已用*标注

联系我们

QQ-501439094

撩一撩:点击这里给我发消息

邮件:501439094@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

隐藏