java二维码工具类:生成二维码和解析二维码

本文介绍如何利用Google的ZXing库生成二维码,并提供了解析二维码的方法。通过示例代码展示了生成指定内容的二维码及读取二维码信息的过程。

利用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加依赖的话

[html]  view plain  copy
  1. <dependency>  
  2.             <groupId>com.google.zxing</groupId>  
  3.             <artifactId>core</artifactId>  
  4.             <version>3.3.0</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>com.google.zxing</groupId>  
  8.             <artifactId>javase</artifactId>  
  9.             <version>3.3.0</version>  
  10.         </dependency>  



工具类:

[java]  view plain  copy
  1. import com.google.zxing.*;  
  2. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;  
  3. import com.google.zxing.client.j2se.MatrixToImageWriter;  
  4. import com.google.zxing.common.BitMatrix;  
  5. import com.google.zxing.common.HybridBinarizer;  
  6.   
  7. import javax.imageio.ImageIO;  
  8. import java.awt.image.BufferedImage;  
  9. import java.io.File;  
  10. import java.io.FileInputStream;  
  11. import java.nio.file.Path;  
  12. import java.util.HashMap;  
  13.   
  14. /** 
  15.  * 二维码工具类 
  16.  * Created by Saindy on 2017-8-26. 
  17.  */  
  18. public class QrCodeUtils {  
  19.   
  20.     static String QRCODE_IMG_PATH = "d:/opt/qrCode/"// 存放二维码的文件夹  
  21.   
  22.     /** 
  23.      * 生成二维码 
  24.      * @param content 要生成的二维码内容 
  25.      * @param fileName 不带扩展名的文件名 
  26.      * @return 返回以.png格式的文件的绝对路径 
  27.      */  
  28.     public static String createQrCode(String content, String fileName){  
  29.         String qrCodeFilePath = "";  
  30.         try {  
  31.             int qrCodeWidth = 300;  
  32.             int qrCodeHeight = 300;  
  33.             String qrCodeFormat = "png";  
  34.             HashMap<EncodeHintType, String> hints = new HashMap<>();  
  35.             hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
  36.             BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, hints);  
  37.   
  38.             BufferedImage image = new BufferedImage(qrCodeWidth, qrCodeHeight, BufferedImage.TYPE_INT_RGB);  
  39.             File qrCodeFile = new File(QRCODE_IMG_PATH + fileName +"." + qrCodeFormat);  
  40.             ImageIO.write(image, qrCodeFormat, qrCodeFile);  
  41. //            MatrixToImageWriter.writeToFile(bitMatrix, qrCodeFormat, qrCodeFile);  
  42.             Path path = qrCodeFile.toPath();  
  43.             MatrixToImageWriter.writeToPath(bitMatrix, qrCodeFormat, path);  
  44.             qrCodeFilePath = qrCodeFile.getAbsolutePath();  
  45.         } catch (Exception e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.         return qrCodeFilePath;  
  49.     }  
  50.   
  51.     /** 
  52.      * 解析二维码 
  53.      * @param filePath 要进行解析的二维码图片地址 
  54.      * @return 解析后得到的文字 
  55.      */  
  56.   
  57.     public static String decodeQrCode(String filePath) {  
  58.         String retStr = "";  
  59.         if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {  
  60.             return "图片路径为空!";  
  61.         }  
  62.         try {  
  63.             BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));  
  64.             LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);  
  65.             Binarizer binarizer = new HybridBinarizer(source);  
  66.             BinaryBitmap bitmap = new BinaryBitmap(binarizer);  
  67.             HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>();  
  68.             hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");  
  69.             Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap);  
  70.             retStr = result.getText();  
  71.         } catch (Exception e) {  
  72.             e.printStackTrace();  
  73.         }  
  74.         return retStr;  
  75.     }  
  76.   
  77.     public static void main(String[] args) {  
  78.         createQrCode("http://blog.youkuaiyun.com/Saindy5828""1610432809");  
  79.         String str = decodeQrCode(QRCODE_IMG_PATH + "1610432809.png");  
  80.         System.out.println("解析二维码得到的内容:"+str);  
  81.     }  
  82. }  


生成的二维码:


解析得到的字符串:

解析二维码得到的内容:http://blog.youkuaiyun.com/Saindy5828

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值