java实现二维码和解码

  1. import java.awt.image.BufferedImage;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import java.util.Hashtable;  
  5.   
  6. import javax.imageio.ImageIO;  
  7.   
  8. import com.google.zxing.BarcodeFormat;  
  9. import com.google.zxing.BinaryBitmap;  
  10. import com.google.zxing.DecodeHintType;  
  11. import com.google.zxing.LuminanceSource;  
  12. import com.google.zxing.MultiFormatReader;  
  13. import com.google.zxing.MultiFormatWriter;  
  14. import com.google.zxing.Reader;  
  15. import com.google.zxing.ReaderException;  
  16. import com.google.zxing.Result;  
  17. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;  
  18. import com.google.zxing.client.j2se.MatrixToImageWriter;  
  19. import com.google.zxing.common.BitMatrix;  
  20. import com.google.zxing.common.HybridBinarizer;  
  21.   
  22. public class TestEnDeCode {  
  23.   
  24.     /** 
  25.      *  
  26.      */  
  27.     public TestEnDeCode() {  
  28.         // TODO Auto-generated constructor stub  
  29.     }  
  30.   
  31.     /** 
  32.      * @param args 
  33.      */  
  34.     public static void main(String[] args) {  
  35.         TestEnDeCode t=new TestEnDeCode();  
  36.         t.encode();  
  37.         t.decode();  
  38.   
  39.     }  
  40.   
  41.     // 编码  
  42.     public void encode() {  
  43.         try {  
  44.             String str = "CN:男;COP:公司;ZW:职务";// 二维码内容  
  45.             String path = "D:\\hwy.png";  
  46.             BitMatrix byteMatrix;  
  47.             byteMatrix = new MultiFormatWriter().encode(new String(str.getBytes("GBK"),"iso-8859-1"),  
  48.                     BarcodeFormat.QR_CODE, 200200);  
  49.             File file = new File(path);  
  50.               
  51.             MatrixToImageWriter.writeToFile(byteMatrix, "png", file);  
  52.         } catch (Exception e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.     }  
  56.   
  57.     // 解码  
  58.     public void decode() {  
  59.         try {  
  60.             Reader reader = new MultiFormatReader();  
  61.             String imgPath = "D:\\hwy.png";  
  62.             File file = new File(imgPath);  
  63.             BufferedImage image;  
  64.             try {  
  65.                 image = ImageIO.read(file);  
  66.                 if (image == null) {  
  67.                     System.out.println("Could not decode image");  
  68.                 }  
  69.                 LuminanceSource source = new BufferedImageLuminanceSource(image);  
  70.                 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(  
  71.                         source));  
  72.                 Result result;  
  73.                 Hashtable hints = new Hashtable();  
  74.                 hints.put(DecodeHintType.CHARACTER_SET, "GBK");  
  75.                 result = new MultiFormatReader().decode(bitmap, hints);  
  76.                 String resultStr = result.getText();  
  77.                 System.out.println(resultStr);  
  78.   
  79.             } catch (IOException ioe) {  
  80.                 System.out.println(ioe.toString());  
  81.             } catch (ReaderException re) {  
  82.                 System.out.println(re.toString());  
  83.             }  
  84.   
  85.         } catch (Exception ex) {  
  86.   
  87.         }  
  88.     }  
  89.   
  90. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值