有一個好的工具,會讓你的開發事半功倍。再將講這個工具類之前,我先給小白補充一點條形碼和二維碼(以下基礎知識選自,我本科階段的一本教材:《物聯網導論》(劉雲浩 編著)。有對物聯網感興趣的,可以看看這本書),我們要內外兼修,你說是不是這么個理呢!
多行組成的條形碼,不需要連接一個數據庫,本身可存儲大量數據,應用於:醫院、駕駛證、物料管理、貨物運輸,當條形碼受一定破壞時,錯誤糾正能使條形碼能正確解碼。二維碼,是一個多行、連續
性、可變長、包含大量數據的符號標識。每個條形碼有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 + "\nBlog [ http://jeeplus.iteye.com ]"
153 + "\nEMail [ 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/
感謝您的閱讀。如果文章對您有用,那么請輕輕點個贊,以資鼓勵。