版权声明:最终解释权归属Hern所有,恒! https://blog.csdn.net/qq_36761831/article/details/84888902
基于zxing3.3.3,点击进行跳转到github的zxing开源项目下载开源项目文件
JDK版本为1.8
注意:
1、二维码扫描出来的网址显示的是文本而不是链接网站的原因:没有在网址前面加http://或https://
例如:将www.baidu.com更改为https://www.baidu.com
2、实现二维码扫描下载安装软件(以安卓为例)
apk下载安卓网址尽量写成:http://www.hern.com/mobile/appdown,不要写成(这种方式在部分软件上会出现不安全警告,例如微信和QQ的扫一扫,但是这种方式是正确的,只是不是本系(腾讯系)软件而已):http://www.hern.com/mobile/hern.apk
3、实现二维码名片
需要借助VCard标准通信簿基本格式实现
生成二维码:
package com.zxing;
import java.util.*;
import java.io.File;
import java.math.*;
import java.net.*;
import java.nio.file.Path;
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.awt.*;
/*
*作者:Hern
*生成二维码图片
* */
public class CreateQRCode {
private static final int width = 300;//设置二维码的宽度
private static final int height = 300;//设置二维码的高度
private static final String format = "jpg";//设置二维码的格式
private static final String url1 = "D:/QRCode/QRCode1.jpg";
private static final String url2 = "D:/QRCode/QRCode2.jpg";
private static final Path file1 = new File(url1).toPath();//设置二维码的保存地址
private static final Path file2 = new File(url2).toPath();
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义二维码参数
HashMap hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//设置编码格式
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);//设置纠错能力,纠错级别(L 7%、M 15%、Q 25%、H 30%)
hints.put(EncodeHintType.MARGIN, 5);//设置边距
try {
URL content = new URL("https://blog.csdn.net/qq_36761831/article/details/84075497");//设置显示网页地址
String str = "作者是Hern,基于GitHub开源项目生成的二维码\n"+"纠错能力最低为75%,本人测试最高纠错能力测试为82.35%\n"+"编码采用UTF-8格式\n"+"生成的二维码格式为jpg\n";//设置显示的内退
BitMatrix bitMatrix1 = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, width, height, hints);//生成二维码
BitMatrix bitMatrix2 = new MultiFormatWriter().encode(content.toString(), BarcodeFormat.QR_CODE, width, height, hints);//生成二维码
MatrixToImageWriter.writeToPath(bitMatrix1, format, file1);//将二维码写入到指定文件夹中
MatrixToImageWriter.writeToPath(bitMatrix2, format, file2);//将二维码写入到指定文件夹中
System.out.println("成功创建二维码\n"+"二维码的文件保存位置是:"+url1+" "+url2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解析二维码(二维码译码):
package com.zxing;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import javax.imageio.ImageIO;
public class ReadQRCode {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
HashMap hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//设置编码格式
File file = new File("D:/QRCode/QRCode1.jpg");//文件的读取路径
BufferedImage image = ImageIO.read(file);//读取图片
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));//转换成位图形式
MultiFormatReader formatReader = new MultiFormatReader();
Result result = formatReader.decode(binaryBitmap, hints);//进行译码
System.out.println("二维码的识别结果是:"+result.toString());
System.out.println("二维码格式类型:"+result.getBarcodeFormat());
System.out.println("二维码文本内容:"+result.getText());
System.out.println("二维码的编码数:"+result.getNumBits());
System.out.println("二维码时间截点是:"+result.getTimestamp());
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}