Google开发的QRcode二维码生成和解码及最大容量

1.源码

package com.test;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
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.QRCodeWriter;

public class main {
	public static void main(String[] args) {
		String filePath = "d:/qr_png.png";
		String str = "";
		/*
		for (int i = 0; i < 2685; i++) {
			str += 1;
		}
		*/
	
		for (int i = 0; i < 635; i++) {
			str += "赵";
		}
			
		encode(str, filePath);
		decode(filePath);
	}

	// qrcode 编码
	static void encode(String conent, String filePath) {
		Charset charset = Charset.forName("UTF-8");
		CharsetEncoder encoder = charset.newEncoder();
		byte[] b = null;
		try { // Convert a string to ISO-8859-1 bytes in a ByteBuffer
			System.out.println("-------->" + conent.length());
			ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(conent));
			b = bbuf.array();
		} catch (CharacterCodingException e) {
			System.out.println(e.getMessage());
		}
		String data = "";
		try {
			data = new String(b, "iso8859-1");
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} // get a byte matrix for the data
		BitMatrix matrix = null;
		int h = 900;
		int w = 800;
		com.google.zxing.Writer writer = new QRCodeWriter();
		try {
			matrix = writer.encode(data,
					com.google.zxing.BarcodeFormat.QR_CODE, w, h);
		} catch (com.google.zxing.WriterException e) {
			System.out.println(e.getMessage());
		}
		File file = new File(filePath);
		try {
			MatrixToImageWriter.writeToFile(matrix, "PNG", file);
			System.out.println("printing to " + file.getAbsolutePath());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	// qrcode 解码
	static void decode(String file) {
		try {
			Result result = null;
			BufferedImage image = null;
			image = ImageIO.read(new File(file));
			LuminanceSource source = new BufferedImageLuminanceSource(image);
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
			Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
			hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
			result = new MultiFormatReader().decode(bitmap, hints);
			String rtn = result.getText();
			System.out.println(rtn);
			System.out.println(rtn.length());
		} catch (Exception ex) {
			System.out.println(ex.toString());
		}
	}
}

2.最多2685个字母635个汉字

635个汉字


2685个字母


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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

赵侠客

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值