作者:O八戒有点坏O | 来源:互联网 | 2023-07-24 12:58
利用google 的 zxing 生成和解析二维码。
1、下载或maven加入依赖,我这选用的是3.3.0版本,
jar下载地址:
http://mvnrepository.com/artifact/com.google.zxing/core/3.3.0
http://mvnrepository.com/artifact/com.google.zxing/javase/3.3.0
下载core 和 javase 这两个jar 包,导入工程,即可。
如果用maven加依赖的话
- <dependency>
- <groupId>com.google.zxinggroupId>
- <artifactId>coreartifactId>
- <version>3.3.0version>
- dependency>
- <dependency>
- <groupId>com.google.zxinggroupId>
- <artifactId>javaseartifactId>
- <version>3.3.0version>
- dependency>
工具类&#xff1a;
- import com.google.zxing.*;
- 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 javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileInputStream;
- import java.nio.file.Path;
- import java.util.HashMap;
-
-
-
-
-
- public class QrCodeUtils {
-
- static String QRCODE_IMG_PATH &#61; "d:/opt/qrCode/";
-
-
-
-
-
-
-
- public static String createQrCode(String content, String fileName){
- String qrCodeFilePath &#61; "";
- try {
- int qrCodeWidth &#61; 300;
- int qrCodeHeight &#61; 300;
- String qrCodeFormat &#61; "png";
- HashMap hints &#61; new HashMap<>();
- hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
- BitMatrix bitMatrix &#61; new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, hints);
-
- BufferedImage image &#61; new BufferedImage(qrCodeWidth, qrCodeHeight, BufferedImage.TYPE_INT_RGB);
- File qrCodeFile &#61; new File(QRCODE_IMG_PATH &#43; fileName &#43;"." &#43; qrCodeFormat);
- ImageIO.write(image, qrCodeFormat, qrCodeFile);
-
- Path path &#61; qrCodeFile.toPath();
- MatrixToImageWriter.writeToPath(bitMatrix, qrCodeFormat, path);
- qrCodeFilePath &#61; qrCodeFile.getAbsolutePath();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return qrCodeFilePath;
- }
-
-
-
-
-
-
-
- public static String decodeQrCode(String filePath) {
- String retStr &#61; "";
- if ("".equalsIgnoreCase(filePath) && filePath.length() &#61;&#61; 0) {
- return "图片路径为空!";
- }
- try {
- BufferedImage bufferedImage &#61; ImageIO.read(new FileInputStream(filePath));
- LuminanceSource source &#61; new BufferedImageLuminanceSource(bufferedImage);
- Binarizer binarizer &#61; new HybridBinarizer(source);
- BinaryBitmap bitmap &#61; new BinaryBitmap(binarizer);
- HashMap hintTypeObjectHashMap &#61; new HashMap<>();
- hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
- Result result &#61; new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap);
- retStr &#61; result.getText();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return retStr;
- }
-
- public static void main(String[] args) {
- createQrCode("http://blog.csdn.net/Saindy5828", "1610432809");
- String str &#61; decodeQrCode(QRCODE_IMG_PATH &#43; "1610432809.png");
- System.out.println("解析二维码得到的内容&#xff1a;"&#43;str);
- }
- }
生成的二维码&#xff1a;
![](https://img-blog.csdn.net/20170827104050738?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU2FpbmR5NTgyOA&#61;&#61;/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA&#61;&#61;/dissolve/70/gravity/Center)
解析得到的字符串&#xff1a;
解析二维码得到的内容&#xff1a;http://blog.csdn.net/Saindy5828