二维码_encode与decode

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介:

二维码encode和decode工具类


[html]  view plain  copy
 print ?
  1. import java.awt.Color;    
  2. import java.awt.Graphics2D;    
  3. import java.awt.image.BufferedImage;    
  4. import java.io.File;    
  5. import java.io.IOException;    
  6. import java.io.InputStream;    
  7. import java.io.OutputStream;    
  8.     
  9. import javax.imageio.ImageIO;    
  10.     
  11. import jp.sourceforge.qrcode.QRCodeDecoder;    
  12. import jp.sourceforge.qrcode.exception.DecodingFailedException;    
  13.     
  14. import com.swetake.util.Qrcode;    
  15.     
  16. public class TwoDimensionCode {    
  17.         
  18.     /**   
  19.      * 生成二维码(QRCode)图片   
  20.      * @param content 存储内容   
  21.      * @param imgPath 图片路径   
  22.      */    
  23.     public void encoderQRCode(String content, String imgPath) {    
  24.         this.encoderQRCode(content, imgPath, "png", 7);    
  25.     }    
  26.         
  27.     /**   
  28.      * 生成二维码(QRCode)图片   
  29.      * @param content 存储内容   
  30.      * @param output 输出流   
  31.      */    
  32.     public void encoderQRCode(String content, OutputStream output) {    
  33.         this.encoderQRCode(content, output, "png", 7);    
  34.     }    
  35.         
  36.     /**   
  37.      * 生成二维码(QRCode)图片   
  38.      * @param content 存储内容   
  39.      * @param imgPath 图片路径   
  40.      * @param imgType 图片类型   
  41.      */    
  42.     public void encoderQRCode(String content, String imgPath, String imgType) {    
  43.         this.encoderQRCode(content, imgPath, imgType, 7);    
  44.     }    
  45.         
  46.     /**   
  47.      * 生成二维码(QRCode)图片   
  48.      * @param content 存储内容   
  49.      * @param output 输出流   
  50.      * @param imgType 图片类型   
  51.      */    
  52.     public void encoderQRCode(String content, OutputStream output, String imgType) {    
  53.         this.encoderQRCode(content, output, imgType, 7);    
  54.     }    
  55.     
  56.     /**   
  57.      * 生成二维码(QRCode)图片   
  58.      * @param content 存储内容   
  59.      * @param imgPath 图片路径   
  60.      * @param imgType 图片类型   
  61.      * @param size 二维码尺寸   
  62.      */    
  63.     public void encoderQRCode(String content, String imgPath, String imgType, int size) {    
  64.         try {    
  65.             BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);    
  66.                 
  67.             File imgFile = new File(imgPath);    
  68.             // 生成二维码QRCode图片    
  69.             ImageIO.write(bufImg, imgType, imgFile);    
  70.         } catch (Exception e) {    
  71.             e.printStackTrace();    
  72.         }    
  73.     }    
  74.     
  75.     /**   
  76.      * 生成二维码(QRCode)图片   
  77.      * @param content 存储内容   
  78.      * @param output 输出流   
  79.      * @param imgType 图片类型   
  80.      * @param size 二维码尺寸   
  81.      */    
  82.     public void encoderQRCode(String content, OutputStream output, String imgType, int size) {    
  83.         try {    
  84.             BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);    
  85.             // 生成二维码QRCode图片    
  86.             ImageIO.write(bufImg, imgType, output);    
  87.         } catch (Exception e) {    
  88.             e.printStackTrace();    
  89.         }    
  90.     }    
  91.         
  92.     /**   
  93.      * 生成二维码(QRCode)图片的公共方法   
  94.      * @param content 存储内容   
  95.      * @param imgType 图片类型   
  96.      * @param size 二维码尺寸   
  97.      * @return   
  98.      */    
  99.     private BufferedImage qRCodeCommon(String content, String imgType, int size) {    
  100.         BufferedImage bufImg = null;    
  101.         try {    
  102.             Qrcode qrcodeHandler = new Qrcode();    
  103.             // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小    
  104.             qrcodeHandler.setQrcodeErrorCorrect('M');    
  105.             qrcodeHandler.setQrcodeEncodeMode('B');    
  106.             // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大    
  107.             qrcodeHandler.setQrcodeVersion(size);    
  108.             // 获得内容的字节数组,设置编码格式    
  109.             byte[] contentBytes = content.getBytes("utf-8");    
  110.             // 图片尺寸    
  111.             int imgSize = 67 + 12 * (size - 1);    
  112.             bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);    
  113.             Graphics2D gs = bufImg.createGraphics();    
  114.             // 设置背景颜色    
  115.             gs.setBackground(Color.WHITE);    
  116.             gs.clearRect(0, 0, imgSize, imgSize);    
  117.     
  118.             // 设定图像颜色> BLACK    
  119.             gs.setColor(Color.BLACK);    
  120.             // 设置偏移量,不设置可能导致解析出错    
  121.             int pixoff = 2;    
  122.             // 输出内容> 二维码    
  123.             if (contentBytes.length > 0 && contentBytes.length < 800) {    
  124.                 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);    
  125.                 for (int i = 0; i < codeOut.length; i++) {    
  126.                     for (int j = 0; j < codeOut.length; j++) {    
  127.                         if (codeOut[j][i]) {    
  128.                             gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);    
  129.                         }    
  130.                     }    
  131.                 }    
  132.             } else {    
  133.                 throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");    
  134.             }    
  135.             gs.dispose();    
  136.             bufImg.flush();    
  137.         } catch (Exception e) {    
  138.             e.printStackTrace();    
  139.         }    
  140.         return bufImg;    
  141.     }    
  142.         
  143.     /**   
  144.      * 解析二维码(QRCode)   
  145.      * @param imgPath 图片路径   
  146.      * @return   
  147.      */    
  148.     public String decoderQRCode(String imgPath) {    
  149.         // QRCode 二维码图片的文件    
  150.         File imageFile = new File(imgPath);    
  151.         BufferedImage bufImg = null;    
  152.         String content = null;    
  153.         try {    
  154.             bufImg = ImageIO.read(imageFile);    
  155.             QRCodeDecoder decoder = new QRCodeDecoder();    
  156.             content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8");     
  157.         } catch (IOException e) {    
  158.             System.out.println("Error: " + e.getMessage());    
  159.             e.printStackTrace();    
  160.         } catch (DecodingFailedException dfe) {    
  161.             System.out.println("Error: " + dfe.getMessage());    
  162.             dfe.printStackTrace();    
  163.         }    
  164.         return content;    
  165.     }    
  166.         
  167.     /**   
  168.      * 解析二维码(QRCode)   
  169.      * @param input 输入流   
  170.      * @return   
  171.      */    
  172.     public String decoderQRCode(InputStream input) {    
  173.         BufferedImage bufImg = null;    
  174.         String content = null;    
  175.         try {    
  176.             bufImg = ImageIO.read(input);    
  177.             QRCodeDecoder decoder = new QRCodeDecoder();    
  178.             content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8");     
  179.         } catch (IOException e) {    
  180.             System.out.println("Error: " + e.getMessage());    
  181.             e.printStackTrace();    
  182.         } catch (DecodingFailedException dfe) {    
  183.             System.out.println("Error: " + dfe.getMessage());    
  184.             dfe.printStackTrace();    
  185.         }    
  186.         return content;    
  187.     }    
  188.     
  189.     public static void main(String[] args) {    
  190.         String imgPath = "E:/abc.png";    
  191.         String encoderContent = "http://blog.csdn.net/benjamin_whx";    
  192.         TwoDimensionCode handler = new TwoDimensionCode();    
  193.         handler.encoderQRCode(encoderContent, imgPath, "png");    
  194. //      try {    
  195. //          OutputStream output = new FileOutputStream(imgPath);    
  196. //          handler.encoderQRCode(content, output);    
  197. //      } catch (Exception e) {    
  198. //          e.printStackTrace();    
  199. //      }    
  200.         System.out.println("========encoder success");    
  201.             
  202.             
  203.         String decoderContent = handler.decoderQRCode("E:/aaa.jpg");    
  204.         System.out.println("解析结果如下:");    
  205.         System.out.println(decoderContent);    
  206.         System.out.println("========decoder success!!!");    
  207.     }    
  208. }    


TwoDimensionCodeImage对象类


[html]  view plain  copy
 print ?
  1. import java.awt.image.BufferedImage;    
  2.     
  3. import jp.sourceforge.qrcode.data.QRCodeImage;    
  4.     
  5. public class TwoDimensionCodeImage implements QRCodeImage {    
  6.     
  7.     BufferedImage bufImg;    
  8.         
  9.     public TwoDimensionCodeImage(BufferedImage bufImg) {    
  10.         this.bufImg = bufImg;    
  11.     }    
  12.         
  13.     @Override    
  14.     public int getHeight() {    
  15.         return bufImg.getHeight();    
  16.     }    
  17.     
  18.     @Override    
  19.     public int getPixel(int x, int y) {    
  20.         return bufImg.getRGB(x, y);    
  21.     }    
  22.     
  23.     @Override    
  24.     public int getWidth() {    
  25.         return bufImg.getWidth();    
  26.     }    
  27.     
  28. }    


jar包下载http://download.csdn.net/detail/u012959829/7941481

目录
相关文章
|
6月前
[BJDCTF 2020]encode
[BJDCTF 2020]encode
33 0
|
8月前
|
编解码 数据可视化 Java
Java如何进行Base64的编码(Encode)与解码(Decode)?
Java如何进行Base64的编码(Encode)与解码(Decode)?
545 1
|
存储 JSON 安全
base64_encode()和base64_decode(),URL的加密解密详解
base64_encode()和base64_decode(),URL的加密解密详解
504 0
|
Cloud Native 关系型数据库 OLAP
可以使用内置的函数`encode()`
可以使用内置的函数`encode()`
136 0
|
Java Python
LeetCode 394:字符串解码 Decode String
题目: 给定一个经过编码的字符串,返回它解码后的字符串。Given an encoded string, return its decoded string. 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。
1035 0
|
Python
Python编码介绍——encode和decode
在 python 源代码文件中,如果你有用到非ASCII字符,则需要在文件头部进行字符编码的声明,声明如下: # code: UTF-8 因为python 只检查 #、coding 和编码字符串,所以你可能回见到下面的声明方式,这是有些人为了美观等原因才这样写的: #-*- cod...
960 0

热门文章

最新文章