创建和读取简单二维码的案例:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37928350/article/details/85119428

创建二维码我们可以通过zxing QrCode的方式:

一 .  zxing 的方式创建:

1.首先我们通过zxing 的方式创建二维码: 

     需要下载zxing jar包:https://download.csdn.net/download/qq_37928350/10864817

package com.mk.zxing;

import java.io.File;
import java.io.IOException;
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 CreateQRCode {
	public static void main(String[] args) {
		int width = 300;
		int height = 300;
		String content = "这是一个二维码";
		
		String format = "png";
		
		//定义二维码参数
		HashMap hhmp =  new HashMap<>();
		hhmp.put(EncodeHintType.CHARACTER_SET, "utf-8");
		hhmp.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		hhmp.put(EncodeHintType.MARGIN, 2);
		
		// 生成路径
		Path file = new File("E:/二维码/img.png").toPath();
		
		//生成二维码
		try {
			BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hhmp);
			MatrixToImageWriter.writeToPath(encode, format, file);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

2. 读取二维码的内容:

package com.mk.zxing;

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

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class ReadQRCode {

	public static void main(String[] args) {
		try {
			MultiFormatReader mfr = new MultiFormatReader();
			File file = new File("E:/二维码/img.png");
			BufferedImage img = ImageIO.read(file);
			BinaryBitmap binaryBitMap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(img)));
			//定义二维码参数
			HashMap hhmp =  new HashMap<>();
			hhmp.put(EncodeHintType.CHARACTER_SET, "utf-8");
			Result decode = mfr.decode(binaryBitMap, hhmp);
			System.out.println("解析结果:"+decode.toString());
			System.out.println("二维码格式类型:"+decode.getBarcodeFormat());
			System.out.println("二维码内容:"+decode.getText());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
}

二 . QrCode 

1. 通过 QrCode 的方式创建二维码:

需要的jar包: 链接: https://pan.baidu.com/s/1uCAMxMYpZzj-R6nlBUTR2A  提取码:3qu3 

package com.imooc_qrcode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

/**
 * @Description: 
 * @author 01391056
 * @date 2018年12月20日下午2:56:29
 * CreateQrCode
 * 
 */
public class CreateQrCode {
	public static void main(String[] args) throws Exception {
		Qrcode x=new Qrcode();
		x.setQrcodeErrorCorrect('M'); //纠错等级
		x.setQrcodeEncodeMode('B'); // N:代表数字  A:代表a-Z  B:代码其他字符  
		x.setQrcodeVersion(7); // 版本号
		String qrData = "www.baidu.com";
		int width = 67+12*(7-1);  //公式:67+12*(version -1)
		int height = 67+12*(7-1);
		
		BufferedImage bi = new  BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
		
		Graphics2D gs = bi.createGraphics();
		gs.setBackground(Color.WHITE);
		gs.setColor(Color.black);
		gs.clearRect(0, 0, width, height);
		
		int pixoff = 2; // 偏移量
		byte[] d =qrData.getBytes("gb2312"); // 有中文加"gb2312" 沒有不用加
		if (d.length>0 && d.length <120){
		    boolean[][] s = x.calQrcode(d);

		    for (int i=0;i<s.length;i++){
			for (int j=0;j<s.length;j++){
			    if (s[j][i]) {
				gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
			    }
			}
		    }
		}
		gs.dispose();
		bi.flush();
		
		ImageIO.write(bi, "png", new File("E:/Qrcode/qrcode.png"));
	}

}


2. 通过 QrCode 的方式读取二维码:

package com.imooc_qrcode;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;

/**
 * @Description: 
 * @author 01391056
 * @date 2018年12月20日下午4:14:21
 * 
 * 
 */
public class ReadQrCode {

	public static void main(String[] args) throws Exception {
		File file = new File("E:/Qrcode/qrcode.png");
		BufferedImage bufferedImage = ImageIO.read(file);
		QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
		String result=	new String(qrCodeDecoder.decode(new MyQrCodeImage(bufferedImage)),"gb2312");
		System.out.println(result);
	}
}
package com.imooc_qrcode;

import java.awt.image.BufferedImage;

import jp.sourceforge.qrcode.data.QRCodeImage;

/**
 * @Description: 
 * @author 01391056
 * @date 2018年12月20日下午4:18:04
 * MyQrCodeImage
 * 
 */
public class MyQrCodeImage implements QRCodeImage {

	BufferedImage bufferedImage;
	
	public MyQrCodeImage(BufferedImage bufferedImage){
		this.bufferedImage = bufferedImage;
	}
	/**
	 * getHeight
	 * @author 01391056
	 * @date 2018年12月20日下午4:18:04
	 */
	@Override
	public int getHeight() {

		return bufferedImage.getHeight();

	}

	/**
	 * getPixel
	 * @author 01391056
	 * @date 2018年12月20日下午4:18:04
	 */
	@Override
	public int getPixel(int arg0, int arg1) {

		return bufferedImage.getRGB(arg0, arg1);

	}

	/**
	 * getWidth
	 * @author 01391056
	 * @date 2018年12月20日下午4:18:04
	 */
	@Override
	public int getWidth() {

		return bufferedImage.getWidth();

	}

}

就以上这两种方式创建的二维码,既可以通过zxing 读取 ,也可以通过 qrcode 读取.

猜你喜欢

转载自blog.csdn.net/qq_37928350/article/details/85119428