利用qrcode.jar生成和解析二维码(非zxing) 利用qrcode.jar生成和解析二维码(非zxing)

利用qrcode.jar生成和解析二维码(非zxing)


弄了几天 android,感觉zxing 很大,很多包,

个人推荐:j2ee 显示二维码,或者 上传到二维码解析 or .... android生成 & 扫描 &解析二维码

下载地址

http://download.csdn.net/detail/hejias/5891099

-------------------------------------------------------------------华丽的分割线----------------------------------------------------------------------------------

1.

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

2

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


 


弄了几天 android,感觉zxing 很大,很多包,

个人推荐:j2ee 显示二维码,或者 上传到二维码解析 or .... android生成 & 扫描 &解析二维码

下载地址

http://download.csdn.net/detail/hejias/5891099

-------------------------------------------------------------------华丽的分割线----------------------------------------------------------------------------------

1.

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

2

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


 

猜你喜欢

转载自blog.csdn.net/jhope/article/details/80841507