二维码:java生成二维码

1. 二维码介绍

草料解析:

我们现在日常生活中常用的二维码一般指 QR code

QR码是二维码码制中的一种,由日本 DENSO WAVE公司于1994年发明。

二维条码是指在一维条码的基础上扩展出另一维具有可读性的条码,使用黑白矩形图案表示二进制数据,被设备扫描后可获取其中所包含的信息。一维条码的宽度记载着数据,而其长度没有记载数据。二维条码的长度、宽度均记载着数据。二维条码有一维条码没有的“定位点”和“容错机制”。容错机制在即使没有辨识到全部的条码、或是说条码有污损时,也可以正确地还原条码上的信息。二维条码的种类很多,不同的机构开发出的二维条码具有不同的结构以及编写、读取方法。

简单来说,二维码就是将一些信息按照某些原则用方块来记录显示。

二维码又分为静态码和活码:

静态码:就是固定的信息,用手机扫描识别只会获取到固定的文本信息。

活码:就是保存一个url,用手机扫描识别后会跳转到该url对应的网页或者应用,能够获取的信息大大增加,并且具有相当大的灵活性。因此称为活码。

2. 二维码原理

具体的原理,请看这里:https://www.cnblogs.com/alantu2018/p/8504373.html

3. java生成二维码图片

要在java项目中生产二维码图片,一般直接引用jar包即可。现在主要有两个选择,

扫描二维码关注公众号,回复: 7839077 查看本文章

一个是qrcode的官网(https://www.supershareware.com/qrcode-jar-free/)下载QrCode.jar

另一个选择就是下载谷歌(http://repo1.maven.org/maven2/com/google/zxing/core/)的Zxing.jar

个人感受谷歌的Zxing.jarQrCode.jar相对好用,而且Zxing.jar还可以解析条形码之类的功能。因此,推荐下载Zxing.jar使用。

下面是用java生成二维码图片的一个简单应用:

  1 import java.io.ByteArrayOutputStream;
  2 
  3 import java.io.FileOutputStream;
  4 
  5 import java.io.IOException;
  6 
  7 import java.io.OutputStream;
  8 
  9 import java.util.HashMap;
 10 
 11 import java.util.Map;
 12 
 13  
 14 
 15 import com.google.zxing.BarcodeFormat;
 16 
 17 import com.google.zxing.EncodeHintType;
 18 
 19 import com.google.zxing.MultiFormatWriter;
 20 
 21 import com.google.zxing.WriterException;
 22 
 23 import com.google.zxing.client.j2se.MatrixToImageWriter;
 24 
 25 import com.google.zxing.common.BitMatrix;
 26 
 27 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 28 
 29  
 30 
 31 /**
 32 
 33  * 二维码工具类
 34 
 35  * @author limingcheng
 36 
 37  *
 38 
 39  */
 40 
 41 public class QrCodeUtil {
 42 
 43  
 44 
 45 /**
 46 
 47  * 生成一个二维码图片
 48 
 49  * @param width
 50 
 51  * @param height
 52 
 53  * @param content
 54 
 55  * @return
 56 
 57  * @throws WriterException
 58 
 59  * @throws IOException
 60 
 61  */
 62 
 63 public static byte[] createQRCode(int width, int height, String content) throws WriterException, IOException {
 64 
 65 // 二维码基本参数设置
 66 
 67 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
 68 
 69 hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码字符集utf-8
 70 
 71 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 设置纠错等级L/M/Q/H,纠错等级越高越不易识别,当前设置等级为最高等级H
 72 
 73 hints.put(EncodeHintType.MARGIN, 0);// 可设置范围为0-10,但仅四个变化0 1(2) 3(4 5 6) 7(8 9 10)
 74 
 75 // 生成图片类型为QRCode
 76 
 77 BarcodeFormat format = BarcodeFormat.QR_CODE;
 78 
 79 // 创建位矩阵对象
 80 
 81 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, format, width, height, hints);
 82 
 83 // 设置位矩阵转图片的参数
 84 
 85 //        MatrixToImageConfig config = new MatrixToImageConfig(Color.black.getRGB(), Color.white.getRGB());
 86 
 87 // 位矩阵对象转流对象
 88 
 89 ByteArrayOutputStream os = new ByteArrayOutputStream();
 90 
 91 MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
 92 
 93 return os.toByteArray();
 94 
 95 }
 96 
 97  
 98 
 99 public static void main(String[] args) throws WriterException, IOException {
100 
101 byte[] b = createQRCode(100, 100, "遇见最好的自己!");
102 
103 OutputStream os = new FileOutputStream("E:\\bestme.png");
104 
105 os.write(b);
106 
107 os.close();
108 
109 }
110 
111 }

生成的二维码图片如下:

 

猜你喜欢

转载自www.cnblogs.com/bestlmc/p/11846639.html