1. The jar package that will be used to import the project (1, Commons-lang-2.3.jar 2, Barcode4j-light-2.0.jar)
2. New Class Barcodeutil
3. Code part
Package com.sale.util;
Import Java.awt.image.BufferedImage;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Org.apache.commons.lang.StringUtils;
Import Org.krysalis.barcode4j.impl.code128.Code128Bean;
Import Org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
Import Org.krysalis.barcode4j.tools.UnitConv;
/**
*
* @author Saffi
* @date October 6, 2017
*/
public class Barcodeutil {
/**
* Generate Files
*
* @param msg
* @param path
* @return
*/
Public File Generatefiles (string msg, string path) {
File File = new file (path);
try {
Generates (MSG, new FileOutputStream (file));
} catch (FileNotFoundException e) {
throw new RuntimeException (e);
}
return file;
}
/**
* Generate bytes
*
* @param msg
* @return
* @throws IOException
*/
Public byte[] Generates (String msg) throws IOException {
Bytearrayoutputstream ous = new Bytearrayoutputstream ();
Generates (msg, OUs);
Ous.close ();
return Ous.tobytearray ();
}
/**
* Generate to stream
*
* @param msg
* @param ous
*/
public static void generates (String msg, OutputStream OUs) {
if (Stringutils.isempty (msg) | | | ous = = NULL) {
Return
}
Barcode type
Code39bean bean = new Code39bean ();
Code128bean bean = new Code128bean ();
Fine degree
final int dpi = 150;
Module width
Final double modulewidth = unitconv.in2mm (2.0f/dpi);
Configuration objects
Bean.setmodulewidth (Modulewidth);
Bean.doquietzone (FALSE);
String format = "Image/png";
try {
Output to stream
Bitmapcanvasprovider canvas = new Bitmapcanvasprovider (OUs, Format, DPI, Bufferedimage.type_byte_binary,
False, 0);
Generate barcodes
Bean.generatebarcode (canvas, msg);
End Drawing
Canvas.finish ();
Ous.close ();
} catch (IOException e) {
throw new RuntimeException (e);
}
}
}
4. Calling interface
/**
* Barcode Generation
* @author Saffi
* @throws IOException
*/
@RequestMapping ("Testorderbarcodes.action")
Public @ResponseBody String testorderbarcodes (HttpServletResponse respon) throws IOException {
String result= "Error";
try {
Calling interface
Barcodeutil util = new Barcodeutil ();
Barcode Content
String msg = ' Te123456 ';
Path
String Path = "C:/users/administrator/desktop/barcode/barcode" +msg+ ". png";
Util.generatefiles (msg, path);
result= "SUCC";
} catch (Exception e) {
E.printstacktrace ();
Respon.sendredirect ("Error.action");
Log.error ("System error", E.fillinstacktrace ());
}
return result;
}
Java uses barcode4j to generate barcodes