java 二维码生成和加密base64压码

因为项目中要实现扫描二维码并实现登录,但本人开发的模块是服务器,跟前台传输用到的主要是json对象。所以不能直接传输图片,必须把图片加密成base64压码的形式。

首先介绍二维码生成的代码,二维码生成我用到的第三方架包是google的zxing。首先下载所需要的包。本来下载一个包zxing-core-2.0.jar,但发现代码有些类找不到具体包,后来查询才发现还少一个包。此包名为:zxing-1.7-javase.jar,稍后会把包上传。

代码如下所示:

[java]  view plain copy
  1. import java.awt.Color;  
  2. import java.awt.Graphics;  
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.util.Hashtable;  
  9.   
  10.   
  11. import sun.misc.BASE64Decoder;  
  12. import sun.misc.BASE64Encoder;  
  13.   
  14. import com.google.zxing.BarcodeFormat;  
  15. import com.google.zxing.EncodeHintType;  
  16. import com.google.zxing.MultiFormatWriter;  
  17. import com.google.zxing.common.BitMatrix;  
  18. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;  
  19. import com.google.zxing.client.j2se.MatrixToImageWriter;   
  20. public class EncoderHandler {  
  21.   
  22.     /** 
  23.  
  24.      * 编码 
  25.  
  26.      * @param contents 
  27.  
  28.      * @param width 
  29.  
  30.      * @param height 
  31.  
  32.      * @param imgPath 
  33.  
  34.      */   
  35.   
  36.     public void encode(String contents, int width, int height, String imgPath) {   
  37.   
  38.         Hashtable<Object, Object> hints = new Hashtable<Object, Object>();   
  39.         // 指定纠错等级   
  40.         hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);   
  41.         // 指定编码格式   
  42.         try {   
  43.   
  44.             BitMatrix byteMatrix;  
  45.             byteMatrix = new MultiFormatWriter().encode(new String(contents.getBytes("UTF-8"),"iso-8859-1"),BarcodeFormat.QR_CODE, width, height);  
  46.             ByteArrayOutputStream bao = new ByteArrayOutputStream();  
  47.             MatrixToImageWriter.writeToStream(byteMatrix, "png", bao);  
  48.             System.out.println(bao.toByteArray());  
  49.             String ok = Base64Code(bao.toByteArray());  
  50.             System.out.println("-----------------------------------------------------------");  
  51.             System.out.println(ok);  
  52.             System.out.println("--------------------------------------------------------------");  
  53.             createImage(ok);  
  54.               
  55.         } catch (Exception e) {   
  56.             e.printStackTrace();   
  57.   
  58.         }   
  59.   
  60.     }   
  61.       
  62.     public String  Base64Code(byte[] b) {  
  63.         BASE64Encoder encoder = new BASE64Encoder();  
  64.         String codeBase64 = "";  
  65.         StringBuilder pictureBuffer = new StringBuilder();  
  66.         pictureBuffer.append(encoder.encode(b));  
  67.         System.out.println(pictureBuffer.toString());  
  68.         codeBase64 = pictureBuffer.toString();  
  69.         return codeBase64;  
  70.     }  
  71.   
  72.     public void createImage(String Base64){  
  73.         BASE64Decoder decoder = new BASE64Decoder();  
  74.         FileOutputStream write;  
  75.         try {  
  76.             write = new FileOutputStream(new File(  
  77.                     "c:/ok.png"));  
  78.             byte[] decoderBytes = decoder  
  79.                     .decodeBuffer(Base64);  
  80.             write.write(decoderBytes);  
  81.         } catch (FileNotFoundException e) {  
  82.             // TODO Auto-generated catch block  
  83.             e.printStackTrace();  
  84.         } catch (IOException e) {  
  85.             // TODO Auto-generated catch block  
  86.             e.printStackTrace();  
  87.         }  
  88.           
  89.         System.out.println("Decoding the picture Success");  
  90.     }  
  91.     /** 
  92.  
  93.      * @param args 
  94.  
  95.      */   
  96.   
  97.     public static void main(String[] args) {   
  98.   
  99.         String imgPath = "e:/zxing.png";   
  100.   
  101.           
  102.         String contents = "http://www.baidu.com";  
  103.   
  104.         int width = 300, height = 300;   
  105.   
  106.         EncoderHandler handler = new EncoderHandler();   
  107.   
  108.         handler.encode(contents, width, height, imgPath);   
  109.         System.out.println("successful");   
  110.   
  111.     }   
  112. }  
主类中的图片地址未用到,主要输出到c盘下面,可以仔细看源码。该源码直接把生成的图片在未存储到本地的情况下,直接转换为base64压码,然后直接以字符串形式存储在mongodb中,无需使用本地硬盘。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值