zxing的源码中包含不少的模块,模块列表以下: java
core为zxing的核心代码,包括encode和decode的代码。 android
javase能够定义为一个辅助代码,主要提供一些工具类。好比:读取image的代码类ImageReader、写image到文件的类MatrixToImageWriter。充分使用zxing的javase模块提供的工具类,既方便了代码的编写工做,又避免了上网找一些相似的代码。 web
刚接触zxing的时候,网上有说须要引用core和javase两个模块,作开发,可是因为刚接触,具体也不清楚javase的功能,作例子的时候就没有引用javase的jar。写了用zxing读写pdf417的例子以后,再看javase,发现javase中已经提供了我在网上找的代码的功能,因而重构上篇博文提供的代码,这里贴出重构后的两个类文件,读者能够与上篇博文中贴出的两个类进行对比,从中体会javase模块的好处。 ruby
ZxingPdfWrite app
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")); } }