Google開源項目ZXing(二維條碼編解碼)簡單使用(Java版)__Java

來源:互聯網
上載者:User

http://code.google.com/p/zxing/

ZXing (pronounced "zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. Our focus is on using the built-in camera on mobile phones to scan and decode barcodes on the device, without communicating with a server. However the project can be used to encode and decode barcodes on desktops and servers as well. 

ZXing項目是google code上面提供的一個關於條碼編解碼的開源項目。

要對本項目進行二次開發,首先你需要在源碼下載列表中下載ZXing1.7.zip原始碼

解壓檔案,你會看到裡面有很多檔案夾,包括J2SE的,Android的,J2ME,C#等等。我們以J2SE為例。

你需要使用裡面的Core和J2SE檔案夾。然後匯入到你在Eclipse中建立的工程,目錄形式如下:



J2SE包中,提供了編碼和解碼的.Java檔案,但是由於寫的過於複雜,還沒弄清楚這個開源項目源碼之前,還是不推薦使用。

自己在j2se包中建立兩個Java檔案Encoder和Decoder。代碼如下:

package com.google.zxing.client.j2se;import java.io.File;import java.io.IOException;import java.util.Hashtable;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;public class Encoder {public static void main(String[] args) {String contents = "今天,我們來簡單聊聊google開源項目——ZXing(二維條碼編解碼)";Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();hints.put(EncodeHintType.CHARACTER_SET, "GBK");BitMatrix matrix = null;try {matrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, 300, 300, hints);} catch (WriterException e) {e.printStackTrace();}File file = new File("D://qrcodeImage.png");try {MatrixToImageWriter.writeToFile(matrix, "png", file);} catch (IOException e) {e.printStackTrace();}}}

package com.google.zxing.client.j2se;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.Hashtable;import javax.imageio.ImageIO;import com.google.zxing.BinaryBitmap;import com.google.zxing.DecodeHintType;import com.google.zxing.LuminanceSource;import com.google.zxing.MultiFormatReader;import com.google.zxing.NotFoundException;import com.google.zxing.Result;import com.google.zxing.common.HybridBinarizer;public class Decoder {public static void main(String[] args) {File file = new File("D://qrcodeImage.png");BufferedImage bufferedImage = null;try {bufferedImage = ImageIO.read(file);} catch (IOException e) {e.printStackTrace();}LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();hints.put(DecodeHintType.CHARACTER_SET, "GBK");Result result = null;try {result = new MultiFormatReader().decode(bitmap, hints);} catch (NotFoundException e) {e.printStackTrace();}System.out.println(result.toString());}}

之後你就可以開始進行條碼編輯和識別了。這裡是支援中文的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.