Google开源项目ZXing的网方网址: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上面提供的一个关于条码编解码的开源项目。
要对本项目进行二次开发,首先你需要在源码下载列表中下载ZXing-x.x.x.zip源代码
解压文件,你会看到里面有很多文件夹,包括J2SE的,Android的,J2ME,C#等等。我们以J2SE为例。
J2SE包中,提供了编码和解码的.Java文件,但是由于写的过于复杂,还没弄清楚这个开源项目源码之前,还是不推荐使用。
自己在j2se包中创建两个Java文件Encoder和Decoder。代码如下:
Encoder.java
class="java">package com.bijian.study; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; 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.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; public class Encoder { public static void main(String[] args) throws IOException { 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"); OutputStream out = new FileOutputStream(file); MatrixToImageWriter.writeToStream(matrix, "png", out); } }
Decoder.java
package com.bijian.study; 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.client.j2se.BufferedImageLuminanceSource; 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()); } }
? ? ? ? 之后你就可以开始进行条码编辑和识别了,这里是支持中文的。
? ? ? ? 执行Encoder中的main方法,即在D盘下生成了qrcodeImage.png文件,打开文件,如下所示:
可以用微信的“扫一扫”扫此二维码,获取内容“今天,我们来简单聊聊google开源项目——ZXing(二维条码编解码)”。
接下来,再执行Decoder的main方法,运行输出内容“今天,我们来简单聊聊google开源项目——ZXing(二维条码编解码)”。
?
文章来源:http://yuncode.net/article/a_5218901e0f1e870