backgroundat present, the application of two-dimensional code has been spread all over the Internet platform, usually the unique number of products/commodities stored in the QR code to do scanning code recognition. and the bar code technology for production environment still exists, such as hardware equipment manufacture, supply, logistics transportation and so on. in the common product information management, material order system, there are multiple requirements scenarios for generating and printing barcodes (one-dimensional code). Solution SolutionsJava Barcode Generation Scheme--barcode4j, zxingbarcode4jbarcode4j Open source Java Barcode Generation Library. Supports multiple encoding formats, such as: code-39,code-128, etc.http://barcode4j.sourceforge.net/ zxingis a 1d/2d codec class library made by Google Open source. The goal is to decode the QR code, Data Matrix, and UPC 1D barcodes. It offers a variety of platforms under which clients include: J2ME, J2se and Android this time using barcode4j as a solution Environment Preparation
Download Barcode4j-lightBARCODE4J relies on a slightly bloated Lib package, which includes the Avalon-framework/servelet-api,so this selection is a lightweight version of the
barcode4j-light
maven Addresshttp://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j-light/2.0
<Dependency><groupId>Net.sf.barcode4j</groupId><Artifactid>Barcode4j-light</Artifactid><version>2.0</version></Dependency>
//Alternatively, you can download the Barcode4j-bin packagehttp://sourceforge.net/projects/barcode4j/files/barcode4j/Barcode4J%202.0/Code InstanceBarcodeutil Tool Class
PackageUtils;ImportJava.awt.image.BufferedImage;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream;Importorg.apache.commons.lang.StringUtils;ImportOrg.krysalis.barcode4j.impl.code39.Code39Bean;ImportOrg.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;ImportOrg.krysalis.barcode4j.tools.UnitConv;/*** Barcode Tool class * *@authorTangzz * @createDate September 17, 2015 **/ Public classBarcodeutil {/*** Generate file * *@parammsg *@paramPath *@return */ Public Staticfile Generatefile (string msg, string path) {File file=NewFile (path); Try{Generate (msg,Newfileoutputstream (file)); } Catch(FileNotFoundException e) {Throw NewRuntimeException (e); } returnfile; } /*** Generate bytes * *@parammsg *@return */ Public Static byte[] Generate (String msg) {bytearrayoutputstream ous=NewBytearrayoutputstream (); Generate (msg, OUs); returnOus.tobytearray (); } /*** Generate to stream * *@parammsg *@paramOUs*/ Public Static voidGenerate (String msg, OutputStream OUs) {if(Stringutils.isempty (msg) | | ous = =NULL) { return; } Code39bean Bean=NewCode39bean (); //Fine Degree Final intDPI = 150; //Module Width Final DoubleModulewidth = unitconv.in2mm (1.0f/dpi); //Configuration Objectsbean.setmodulewidth (modulewidth); Bean.setwidefactor (3); Bean.doquietzone (false); String format= "Image/png"; Try { //output to streamBitmapcanvasprovider Canvas=NewBitmapcanvasprovider (OUs, Format, DPI, Bufferedimage.type_byte_binary,false, 0); //Generate BarcodesBean.generatebarcode (canvas, msg); //End DrawingCanvas.finish (); } Catch(IOException e) {Throw NewRuntimeException (e); } } Public Static voidMain (string[] args) {String msg= "123456789"; String Path= "Barcode.png"; Generatefile (msg, path); }}
FAQ
Advantages of QR code relative to barcode
Greater data capacity, exceeding alphanumeric limits, and anti-damage capability
a variety of coding formats for barcodes
how to generate or identify a QR coderecommended use of the Zxing project Https://github.com/zxing/zxing
Java Barcode Generation Technology-barcode4j