Java 生成二维码。
文章目录
所需资源包:https://download.csdn.net/download/lyfGeek/12463531
二维码。
二维码又称二维条码,常见的二维码为 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能存更多的信息,也能表示更多的数据类型。
二维条码 / 二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的、黑白相间的、记录数据符号信息的图形;在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理:它具有条码技术的一些共性:每种码制有其特定的字符集;每个字符占有一定的宽度;具有一定的校验功能等。同时还具有对不同行的信息自动识别功能、及处理图形旋转变化点。 [1]
2016年8月3日,支付清算协会向支付机构下发《条码支付业务规范》(征求意见稿),意见稿中明确指出支付机构开展条码业务需要遵循的安全标准。这是央行在2014年叫停二维码支付以后首次官方承认二维码支付地位。 [2]
发展。
一维码。
https://baike.baidu.com/item/%E4%B8%80%E7%BB%B4%E6%9D%A1%E7%A0%81/2522516?fr=aladdin
二维码。
- 堆叠式/行排式二维条码。
- 矩阵式二维条码。0 / 1。
- 邮政码。
优缺点。
QR Code。
生成方法。
第三方 jar。eg. zxing 和 qrcodejar。
JavaScript。eg. jquery.qrcode.js。
ZXing Project。
https://zxing.org/w/decode.jspx
https://github.com/zxing/zxing
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<!-- Core barcode encoding/decoding library -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<!-- Java SE-specific extensions to core ZXing library -->
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
package com.geek.zxing;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
public class createQRCode {
public static void main(String[] args) {
int width = 300;
int height = 300;
String format = "png";
String content = "https://me.csdn.net/lyfGeek";
// 二维码参数。
Map hints = new HashMap<>();
// n. 暗示;提示;示意;征兆;迹象;少许;少量
// v. 暗示;透露;示意
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(content, BarcodeFormat.QR_CODE, width, height, hints);
Path path = new File("./qr.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, path);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
zxing 解析。
package com.geek.zxing;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ReadQRCode {
public static void main(String[] args) {
MultiFormatReader multiFormatReader = new MultiFormatReader();
File file = new File("./qr.png");
try {
BufferedImage image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
// 二维码参数。
Map hints = new HashMap<>();
// n. 暗示;提示;示意;征兆;迹象;少许;少量
// v. 暗示;透露;示意
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
Result result = multiFormatReader.decode(binaryBitmap, hints);
System.out.println(result);
// https://me.csdn.net/lyfGeek
BarcodeFormat barcodeFormat = result.getBarcodeFormat();
System.out.println("barcodeFormat = " + barcodeFormat);
// barcodeFormat = QR_CODE
byte[] rawBytes = result.getRawBytes();
System.out.println("rawBytes = " + rawBytes);
// [B@6bc168e5
String string = new String(rawBytes);
// A��GG3����R�76F���WB�ǖdvVV���������
System.out.println("string = " + string);
Map<ResultMetadataType, Object> resultMetadata = result.getResultMetadata();
System.out.println("resultMetadata = " + resultMetadata);
// resultMetadata = {BYTE_SEGMENTS=[[B@7b3300e5], ERROR_CORRECTION_LEVEL=M}
ResultPoint[] resultPoints = result.getResultPoints();
System.out.println("resultPoints = " + resultPoints);
// [Lcom.google.zxing.ResultPoint;@2e5c649
String text = result.getText();
System.out.println("text = " + text);
// https://me.csdn.net/lyfGeek
long timestamp = result.getTimestamp();
System.out.println("timestamp = " + timestamp);
} catch (IOException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
}
QRCode。
- 生成。
http://www.swetake.com/index-e.php
http://www.swetake.com/qrcode/index-e.html
http://www.swetake.com/qrcode/java/qr_java.html
- 读取。
https://osdn.net/projects/qrcode/downloads/28391/qrcode.zip/
package com.geek.qrcode;
import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CreateQRCode {
public static void main(String[] args) throws IOException {
Qrcode x = new Qrcode();
x.setQrcodeErrorCorrect('M');// 纠错等级。
x.setQrcodeEncodeMode('B');// N(数字)。A(a-Z)。B(其他)。
x.setQrcodeVersion(7);// 版本号。
String qrData = "https://me.csdn.net/lyfGeek";
// int width = 300;
int width = 67 + 12 * (7 - 1);
// int height = 300;
int height = 67 + 12 * (7 - 1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
graphics.setBackground(Color.white);
graphics.setColor(Color.black);
graphics.clearRect(0, 0, width, height);
int pixoff = 2;// 偏移量。
// byte[] d = qrData.getBytes();
byte[] d = qrData.getBytes("utf-8");
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]) {
graphics.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
}
graphics.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("./qrcode.png"));
}
}
- 读取。
实现 QRCodeImage 接口。
package com.geek.qrcode;
import jp.sourceforge.qrcode.data.QRCodeImage;
import java.awt.image.BufferedImage;
public class MyQRCodeImage implements QRCodeImage {
BufferedImage bufferedImage;
public MyQRCodeImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
@Override
public int getWidth() {
return bufferedImage.getWidth();
}
@Override
public int getHeight() {
return bufferedImage.getHeight();
}
@Override
public int getPixel(int i, int i1) {
return bufferedImage.getRGB(i, i1);
}
}
package com.geek.qrcode;
import jp.sourceforge.qrcode.QRCodeDecoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ReadQRCode {
public static void main(String[] args) throws IOException {
File file = new File("./qrcode.png");
BufferedImage bufferedImage = ImageIO.read(file);
QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
byte[] bytes = qrCodeDecoder.decode(new MyQRCodeImage(bufferedImage));
String result = new String(bytes, "utf-8");
System.out.println("result = " + result);
// result = https://me.csdn.net/lyfGeek
}
}
jQuery。
https://github.com/jeromeetienne/jquery-qrcode
扫描出来不是页面而是文本?
请加上 https://
“https://me.csdn.net/lyfGeek”
实现手机扫码安装软件。
二维码微信名片。
vCard。
vCard 是电子名片的文件格式标准。它一般附加在电子邮件之后,但也可以用于其它场合(如在互联网上相互交换)。vCard 可包含的信息有:姓名、地址资讯、电话号码、URL,logo,相片等。
https://en.wikipedia.org/wiki/VCard
vCard
From Wikipedia, the free encyclopedia
Jump to navigationJump to search
Not to be confused with Virginity.
“.vcf” redirects here. For the bioinformatics text file format, see Variant Call Format.
vCard
Vcard-big-transp.png
Filename extension .vcf, .v card
Internet media type text/vcard
Type code vCrd
Uniform Type Identifier (UTI) public.vcard
Developed by Created by Versit Consortium, all rights transferred in 1996 to Internet Mail Consortium, all rights transferred in 2004 to CalConnect
Type of format Electronic business card
vCard, also known as VCF (Virtual Contact File), is a file format standard for electronic business cards. vCards are often attached to e-mail messages, but can be exchanged in other ways, such as Multimedia Messaging Service (MMS), on the World Wide Web, instant messaging or through QR code. They can contain name and address information, telephone numbers, e-mail addresses, URLs, logos, photographs, and audio clips.[1]
vCard is used as data interchange format in personal digital assistants (PDAs), personal information managers (PIMs) and customer relationship management (CRMs). To accomplish these data interchange applications, other “vCard variants” have been used and proposed as “variant standards”, each for its specific niche: XML representation, JSON representation, or web pages.