• 条形码和二维码编码解码工具类源码


      有一个好的工具,会让你的开发事半功倍。再将讲这个工具类之前,我先给小白补充一点条形码和二维码(以下基础知识选自,我本科阶段的一本教材:《物联网导论》(刘云浩 编著)。有对物联网感兴趣的,可以看看这本书),我们要内外兼修,你说是不是这么个理呢!

      多行组成的条形码,不需要连接一个数据库,本身可存储大量数据,应用于:医院、驾驶证、物料管理、货物运输,当条形码受一定破坏时,错误纠正能使条形码能正确解码。二维码,是一个多行、连续

    性、可变长、包含大量数据的符号标识。每个条形码有3 - 90行,每一行有一个起始部分、数据部分、终止部分。它的字符集包括所有128个字符,最大数据含量是1850个字符。

      一维条形码只是在一个方向(一般是水平方向)表达信息,而在垂直方向则不表达任何信息,其一定的高度通常是为了便于阅读器的对准。

      一维条形码的应用可以提高信息录入的速度,减少差错率,但是一维条形码也存在一些不足之处:

        数据容量较小: 30个字符左右

        只能包含字母和数字

        条形码尺寸相对较大(空间利用率较低)

        条形码遭到损坏后便不能阅读

      在水平和垂直方向的二维空间存储信息的条形码, 称为二维条形码(dimensional bar code)

      优势

      从以上的介绍可以看出,与二维条形码相比一维条形码有着明显的优势,归纳起来主要有以下几个方面:

        (一)数据容量更大

        (二)超越了字母数字的限制

        (三)条形码相对尺寸小

        (四)具有抗损毁能力

      优点

      1.高密度编码,信息容量大:

        可容纳多达1850个大写字母或2710个数字或1108个字节,或500多个汉字,比普通条码信息容量约高几十倍。

      2.编码范围广:

        该条码可以把图片、声音、文字、签字、指纹等可以数字化的信息进行编码,用条码表示出来;可以表示多种语言文字;可表示图像数据。

      3.容错能力强,具有纠错功能:

        这使得二维条码因穿孔、污损等引起局部损坏时,照样可以正确得到识读,损毁面积达50%仍可恢复信息。

      4.译码可靠性高:

        它比普通条码译码错误率百万分之二要低得多,误码率不超过千万分之一。

      5.可引入加密措施:

        保密性、防伪性好。

      6.成本低,易制作,持久耐用。

      7.条码符号形状、尺寸大小比例可变。

      8.二维条码可以使用激光或CCD阅读器识读。

     

      看到这里,接下来,我给大家讲解一下封装的条形码和二维码编码解码类。

      1 import java.awt.image.BufferedImage;
      2 import java.io.File;
      3 import java.util.Hashtable;
      4 
      5 import javax.imageio.ImageIO;
      6 
      7 import com.google.zxing.BarcodeFormat;
      8 import com.google.zxing.BinaryBitmap;
      9 import com.google.zxing.DecodeHintType;
     10 import com.google.zxing.EncodeHintType;
     11 import com.google.zxing.LuminanceSource;
     12 import com.google.zxing.MultiFormatReader;
     13 import com.google.zxing.MultiFormatWriter;
     14 import com.google.zxing.Result;
     15 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
     16 import com.google.zxing.client.j2se.MatrixToImageWriter;
     17 import com.google.zxing.common.BitMatrix;
     18 import com.google.zxing.common.HybridBinarizer;
     19 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
     20 
     21 /**
     22  * 条形码和二维码编码解码
     23  */
     24 public class ZxingHandler {
     25 
     26     /**
     27      * 条形码编码
     28      * 
     29      * @param contents
     30      * @param width
     31      * @param height
     32      * @param imgPath
     33      */
     34     public static void encode(String contents, int width, int height, String imgPath) {
     35         int codeWidth = 3 + // start guard
     36                 (7 * 6) + // left bars
     37                 5 + // middle guard
     38                 (7 * 6) + // right bars
     39                 3; // end guard
     40         codeWidth = Math.max(codeWidth, width);
     41         try {
     42             BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
     43                     BarcodeFormat.EAN_13, codeWidth, height, null);
     44 
     45             MatrixToImageWriter
     46                     .writeToFile(bitMatrix, "png", new File(imgPath));
     47 
     48         } catch (Exception e) {
     49             e.printStackTrace();
     50         }
     51     }
     52 
     53     /**
     54      * 条形码解码
     55      * 
     56      * @param imgPath
     57      * @return String
     58      */
     59     public static String decode(String imgPath) {
     60         BufferedImage image = null;
     61         Result result = null;
     62         try {
     63             image = ImageIO.read(new File(imgPath));
     64             if (image == null) {
     65                 System.out.println("the decode image may be not exit.");
     66             }
     67             LuminanceSource source = new BufferedImageLuminanceSource(image);
     68             BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
     69 
     70             result = new MultiFormatReader().decode(bitmap, null);
     71             return result.getText();
     72         } catch (Exception e) {
     73             e.printStackTrace();
     74         }
     75         return null;
     76     }
     77     
     78     /**
     79      * 二维码编码
     80      * 
     81      * @param contents
     82      * @param width
     83      * @param height
     84      * @param imgPath
     85      */
     86     public static void encode2(String contents, int width, int height, String imgPath) {
     87         Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
     88         // 指定纠错等级
     89         hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
     90         // 指定编码格式
     91         hints.put(EncodeHintType.CHARACTER_SET, "GBK");
     92         try {
     93             BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
     94                     BarcodeFormat.QR_CODE, width, height, hints);
     95 
     96             MatrixToImageWriter
     97                     .writeToFile(bitMatrix, "png", new File(imgPath));
     98 
     99         } catch (Exception e) {
    100             e.printStackTrace();
    101         }
    102     }
    103 
    104     /**
    105      * 二维码解码
    106      * 
    107      * @param imgPath
    108      * @return String
    109      */
    110     public static String decode2(String imgPath) {
    111         BufferedImage image = null;
    112         Result result = null;
    113         try {
    114             image = ImageIO.read(new File(imgPath));
    115             if (image == null) {
    116                 System.out.println("the decode image may be not exit.");
    117             }
    118             LuminanceSource source = new BufferedImageLuminanceSource(image);
    119             BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    120 
    121             Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
    122             hints.put(DecodeHintType.CHARACTER_SET, "GBK");
    123 
    124             result = new MultiFormatReader().decode(bitmap, hints);
    125             return result.getText();
    126         } catch (Exception e) {
    127             e.printStackTrace();
    128         }
    129         return null;
    130     }
    131 
    132     /**
    133      * @param args
    134      */
    135     public static void main(String[] args) {
    136 
    137         // 条形码
    138         String imgPath = "target\zxing_EAN13.png";
    139         String contents = "6923450657713";
    140         int width = 105, height = 50;
    141         
    142         ZxingHandler.encode(contents, width, height, imgPath);
    143         System.out.println("finished zxing EAN-13 encode.");
    144 
    145         String decodeContent = ZxingHandler.decode(imgPath);
    146         System.out.println("解码内容如下:" + decodeContent);
    147         System.out.println("finished zxing EAN-13 decode.");
    148         
    149         // 二维码
    150         String imgPath2 = "target\zxing.png";
    151         String contents2 = "Hello Gem, welcome to Zxing!"
    152                 + "
    Blog [ http://jeeplus.iteye.com ]"
    153                 + "
    EMail [ jeeplus@163.com ]";
    154         int width2 = 300, height2 = 300;
    155 
    156         ZxingHandler.encode2(contents2, width2, height2, imgPath2);
    157         System.out.println("finished zxing encode.");
    158 
    159         String decodeContent2 = ZxingHandler.decode2(imgPath2);
    160         System.out.println("解码内容如下:" + decodeContent2);
    161         System.out.println("finished zxing decode.");
    162         
    163     }
    164     
    165 }

    转载请注明出处!

    http://www.cnblogs.com/libingbin/

    感谢您的阅读。如果文章对您有用,那么请轻轻点个赞,以资鼓励。

  • 相关阅读:
    python 获取当前文件夹下所有文件名
    leetcode 205. Isomorphic Strings
    leetcode 204. Count Primes
    leetcode 203. Remove Linked List Elements
    神经网络中的激活函数tanh sigmoid RELU softplus softmatx
    leetcode 189. Rotate Array
    一个简单的二进制加法器
    AliOS编译安装MyRocks
    MYSQL5.7无法启动服务原因及解决方案
    基础知识巩固笔记(链接、装载与库)
  • 原文地址:https://www.cnblogs.com/libingbin/p/6032973.html
Copyright © 2020-2023  润新知