使用zxing生成与解析二维码

本文介绍使用zxing生成与解析二维码,大家可以考虑在二维码中如何加入颜色图片等。

package com.test.zxing;

import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Scanner;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCode {
	
	String format = "png";
	
	//生成二维码
	public void creatQRCode(int width,int height,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(new String(content.getBytes("UTF-8"),"ISO-8859-1"),BarcodeFormat.QR_CODE,width,height);
			//定义图片保存路径
			Path file = new File("D:/image.png").toPath();
			//写入
			MatrixToImageWriter.writeToPath(bitMatrix, format, file);
			
			System.out.println("二维码已保存至D:/image.png");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//解析二维码
	public void ReadQRCode(String path){
		try {
			MultiFormatReader formatReader = new MultiFormatReader();
			//定义二维码参数
			HashMap hints = new HashMap();
			//定义字符集编码
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			//获取文件
			File file = new File(path);	
			//读取图片
			BufferedImage image = ImageIO.read(file);
			
			BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
			
			Result result = formatReader.decode(binaryBitmap,hints);
			
			System.out.println("解析结果:"+result.toString());
			System.out.println("格式类型:"+result.getBarcodeFormat());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		QRCode qRCode = new QRCode();
		System.out.println("请输入二维码尺寸,以像素为单位:");
		int size = new Scanner(System.in).nextInt();
		System.out.println("请输入要生成二维码的内容:");
		String text = new Scanner(System.in).next();
		qRCode.creatQRCode(size, size, text);
		qRCode.ReadQRCode("D:/image.png");
	}

}
运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值