zxing的源码中包含很多的模块,模块列表如下:
- core: The core image decoding library, and test code
- javase: J2SE-specific client code
- zxingorg: The source behind zxing.org/w
- zxing.appspot.com: The source behind our web-based barcode generator
- android: Android client, called Barcode Scanner
- androidtest: Android test app
- android-integration: Supports integration with our Barcode Scanner app via Intent
- actionscript: partial port to Actionscript
- glass-mirror: partial implementation for the Google Glass Mirror API
- jruby: Ruby wrapper
core为zxing的核心代码,包括encode和decode的代码。
javase可以定义为一个辅助代码,主要提供一些工具类。比如:读取image的代码类ImageReader、写image到文件的类MatrixToImageWriter。充分使用zxing的javase模块提供的工具类,既方便了代码的编写工作,又避免了上网找一些类似的代码。
刚接触zxing的时候,网上有说需要引用core和javase两个模块,做开发,但是由于刚接触,具体也不清楚javase的功能,做例子的时候就没有引用javase的jar。写了用zxing读写pdf417的例子之后,再看javase,发现javase中已经提供了我在网上找的代码的功能,于是重构上篇博文提供的代码,这里贴出重构后的两个类文件,读者可以与上篇博文中贴出的两个类进行对比,从中体会javase模块的好处。
ZxingPdfWrite
package test;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.ImageReader;
import com.google.zxing.common.HybridBinarizer;
public class ZxingPdfRead {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws Exception {
try {
File testImage = new File(
"E:\\work\\all_workspace\\wp_zxing\\barcode4jTest\\src\\test\\helloworld.png");
BufferedImage image = ImageReader.readImage(testImage);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
String resultText = result.getText();
System.out.println("resultText:"
+ URLDecoder.decode(resultText, "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
ZxingPdfWrite
package test;
import java.io.File;
import java.net.URLEncoder;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.pdf417.PDF417Writer;
public class ZxingPdfWrite {
/**
* @param args
* @throws WriterException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
PDF417Writer pdf417Writer = new PDF417Writer();
//注意中文乱码问题
BitMatrix bitMatrix = pdf417Writer.encode(URLEncoder.encode("我是中国人","UTF-8"),
BarcodeFormat.PDF_417, 100, 50);
MatrixToImageWriter.writeToFile(bitMatrix, "png", new File("E:\\work\\all_workspace\\wp_zxing\\barcode4jTest\\src\\test\\helloworld.png"));
}
}
来源:oschina
链接:https://my.oschina.net/u/781254/blog/185264