java生成解析二维码功能

[java]  view plain copy
  1. package com.herman.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import javax.imageio.ImageIO;  
  11.   
  12. import com.google.zxing.BarcodeFormat;  
  13. import com.google.zxing.BinaryBitmap;  
  14. import com.google.zxing.DecodeHintType;  
  15. import com.google.zxing.EncodeHintType;  
  16. import com.google.zxing.MultiFormatReader;  
  17. import com.google.zxing.MultiFormatWriter;  
  18. import com.google.zxing.NotFoundException;  
  19. import com.google.zxing.WriterException;  
  20. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;  
  21. import com.google.zxing.client.j2se.MatrixToImageWriter;  
  22. import com.google.zxing.common.HybridBinarizer;  
  23. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;  
  24.   
  25. /** 
  26.  * 二维码工具类 
  27.  * @author Herman.Xiong 
  28.  * @date 2015-7-10 上午10:52:15 
  29.  * @version V3.0 
  30.  * @since jdk 1.6,tomcat 6.0 
  31.  */  
  32. public class ZxingUtil {  
  33.     private static final Map<EncodeHintType, ErrorCorrectionLevel> encodeMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();  
  34.     private static final Map<DecodeHintType, ErrorCorrectionLevel> decodeMap=new HashMap<DecodeHintType, ErrorCorrectionLevel>();  
  35.     private static final String charset="UTF-8",format="png";  
  36.     private static final int size=150;  
  37.     private ZxingUtil() {}  
  38.   
  39.     /** 
  40.      * 生成二维码图片 
  41.      * @author Herman.Xiong 
  42.      * @date 2015-7-10 上午10:26:33 
  43.      * @param file 
  44.      * @throws WriterException 
  45.      * @throws IOException 
  46.      */  
  47.     public static void createQRCode(String data, File file)  
  48.             throws WriterException, IOException {  
  49.         MatrixToImageWriter.writeToFile(new MultiFormatWriter().encode(  
  50.                 new String(data.getBytes(charset), charset),  
  51.                 BarcodeFormat.QR_CODE, size, size, encodeMap), format, file);  
  52.     }  
  53.   
  54.     /** 
  55.      * 读取二维码信息 
  56.      * @author Herman.Xiong 
  57.      * @date 2015-7-10 上午10:25:58 
  58.      * @param filePath 二维码图片路径 
  59.      * @return 
  60.      * @throws FileNotFoundException 
  61.      * @throws IOException 
  62.      * @throws NotFoundException 
  63.      */  
  64.     public static String readQRCode(String filePath)  
  65.             throws FileNotFoundException, IOException, NotFoundException {  
  66.         return new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(filePath))))),decodeMap).getText();  
  67.     }  
  68.       
  69.     public static void main(String [] arg){  
  70.         try{  
  71.             createQRCode("http://blog.youkuaiyun.com/xmtblog"new File("D:\\111.png"));  
  72.             System.out.println(readQRCode("D:\\111.png"));  
  73.             //源码下载地址:http://download.youkuaiyun.com/detail/xmt1139057136/8886473  
  74.             //欢迎大家关注我的博客!如有疑问,请加QQ群:454796847共同学习!  
  75.         }catch(Exception e){  
  76.             e.printStackTrace();  
  77.         }  
  78.     }  
  79. }  

运行效果如下:

源码下载地址:http://download.youkuaiyun.com/detail/xmt1139057136/8886473

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值