java 生成二维码名片
Quick Response Codes (QR Codes) ,即快速响应码(二维码),随着智能手机的应用,二维码正成为最受欢迎条码技术。条码是机器可读的光学标签,包含对应物品相关信息。二维码是矩阵式,最早有日本电装公司发明用于汽车工业领域,但现在更流行在工业之外的各个领域。
使用二维码的场景很多,最常见如交易支付、产品包装及验证等。本文展示如何快速生成二维码,首先我们创建带URL的简单二维码,然后使用VCard生成二维码名片,VCard是为电子商务应用中的标准文件格式。
gradle依赖
// https://mvnrepository.com/artifact/net.glxn.qrgen/javase
compile group: 'net.glxn.qrgen', name: 'javase', version: '2.0'
QRGen 使用 Zebra Crossing (ZXing) 作为底层实现。 ZXing 开源库能够生成、解析几乎所有条目,包括二维码,但缺点是生成一个简单二维码需要写大量的冗余代码。QRGen在ZXing之上做了良好的封装,暴露功能强大且简单的API,用于生成简单二维码以及vCard码。
创建二维码
使用QRGen可以轻松创建二维码。其使用构建模式暴露Api编程方式,意味着你能简单链接多个方法,减少不必要模板代码。
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;
import java.io.*;
public class CreateQrCode {
public static void main(String... args){
ByteArrayOutputStream bout =
QRCode.from("https://www.baidu.com")
.withSize(250, 250)
.to(ImageType.PNG)
.stream();
try {
OutputStream out = new FileOutputStream("qr-code.png");
bout.writeTo(out);
out.flush();
out.close();
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
上面代码从QRCode.from()开始,给该静态方法中传入URL作为参数,然后进行必要的配置,如宽度,图像类型等,然后使用stream()方法返回生成QR Code的ByteArrayOutputStream,最后输入至文件,当日也可以输出流,如网页。
创建二维码名片
下面示例与之前一样,但我们要生成VCard类型名片信息。VCard是标准电子商务名片格式,包含元数据有,名称,地址,公司等。
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.core.vcard.VCard;
import net.glxn.qrgen.javase.QRCode;
import java.io.*;
public class CreateQrCodeVCard {
public static void main(String... args){
VCard vCard = new VCard();
vCard.setName("相信那永恒的");
vCard.setAddress("street 1, xxxx address");
vCard.setCompany("company Inc.");
vCard.setPhoneNumber("+xx/xx.xx.xx");
vCard.setTitle("title");
vCard.setEmail("info@gmail.com");
vCard.setWebsite("https://google.com");
ByteArrayOutputStream bout =
QRCode.from(vCard)
.withCharset("utf-8")
.withSize(250, 250)
.to(ImageType.PNG)
.stream();
try {
OutputStream out = new FileOutputStream("qr-code-vcard.png");
bout.writeTo(out);
out.flush();
out.close();
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我们首先创建VCard实例,然后使用QRCode.from()静态方法,并传入VCard实例。下一步我们设置相应元数据信息,如宽度、图像类型等。再下一步,调用stream()方法返回生成二维码的ByteArrayOutputStream对象,最后生成文件,因为使用中文,所以需要设置相应编码。
QRGen QR Code 生成器应用参考
下面列出一些常用代码,供不同应用进行参考:
// get QR file from text using defaults
File file = QRCode.from("Hello World").file();
// get QR stream from text using defaults
ByteArrayOutputStream stream = QRCode.from("Hello World").stream();
// override the image type to be JPG
QRCode.from("Hello World").to(ImageType.JPG).file();
QRCode.from("Hello World").to(ImageType.JPG).stream();
// override image size to be 250x250
QRCode.from("Hello World").withSize(250, 250).file();
QRCode.from("Hello World").withSize(250, 250).stream();
// override size and image type
QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).file();
QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).stream();
// supply own outputstream
QRCode.from("Hello World").to(ImageType.PNG).writeTo(outputStream);
// supply own file name
QRCode.from("Hello World").file("QRCode");
// supply charset hint to ZXING
QRCode.from("Hello World").withCharset("UTF-8");
// supply error correction level hint to ZXING
QRCode.from("Hello World").withErrorCorrection(ErrorCorrectionLevel.L);
// supply any hint to ZXING
QRCode.from("Hello World").withHint(EncodeHintType.CHARACTER_SET, "UTF-8");
// encode contact data as vcard using defaults
VCard johnDoe = new VCard("John Doe")
.setEmail("john.doe@example.org")
.setAddress("John Doe Street 1, 5678 Doestown")
.setTitle("Mister")
.setCompany("John Doe Inc.")
.setPhoneNumber("1234")
.setWebsite("www.example.org");
QRCode.from(johnDoe).file();
// if using special characters don't forget to supply the encoding
VCard johnSpecial = new VCard("中文名称")
.setAddress("北京东路201号");
QRCode.from(johnSpecial).withCharset("UTF-8").file();
总结
QRGen在ZXing之上做了良好的封装,暴露功能强大且简单的API,用于生成简单二维码以及vCard码。本文通过实例介绍生成简单二维码即二维码名片的应用示例,最后提供QRGen的常见API参考示例。
本文参考链接:https://blog.csdn.net/neweastsun/article/details/80294383