Package com.warehouse.common.util;
Import Org.jbarcode.JBarcode;
Import Org.jbarcode.JBarcodeFactory;
Import Org.jbarcode.encode.Code128Encoder;
Import org.jbarcode.encode.InvalidAtributeException;
Import Org.jbarcode.paint.TextPainter;
Import Org.jbarcode.util.ImageUtil;
Import java.awt.*;
Import Java.awt.image.BufferedImage;
Import java.io.*;
/**
* @class: Jbarcodeutil
* @descript: Generate barcode most standard (barcode code can be Chinese)
* @date: November 25, 2016 1:46:07
* @version: V1.0
Notes
The function of static code blocks: When a class is loaded, a static block of code is executed and executed only once, and static blocks are commonly used to perform initialization of class properties.
* * * The height and font size settings of a constant bar code are important, if the setting is small, you will not see the settings file
*/
public class Jbarcodeutil {
Set bar code Height
private static final int barcode_height = 65;
Set Barcode Default resolution
private static final int barcode_dpi = 203;
Set Barcode font Style
private static final String font_family = "Console";
Set Barcode Font Size
private static final int font_size = 13;
Set up barcode text
public static String TEXT = "";
Create Jbarcode
private static Jbarcode JBC = null;
Static Jbarcode Getjbarcode () throws Invalidatributeexception {
/**
* Reference Setting Style:
*barcode.setencoder (Code128encoder.getinstance ()); Set encoding
*barcode.setpainter (Widthcodedpainter.getinstance ());//set painter
*barcode.settextpainter (Baselinetextpainter.getinstance ()); Set Textpainter
*barcode.setbarheight (17); Set height
*barcode.setwideratio (Double.valueof (). Doublevalue ());//Set width ratio
*barcode.setxdimension (Double.valueof (2). Doublevalue ()); Set size, size density
*barcode.setshowtext (TRUE); Whether to display text
*barcode.setcheckdigit (TRUE); Whether to check the numbers
*barcode.setshowcheckdigit (FALSE); Whether to show check numbers
*/
if (JBC = = null) {
Generate code128
JBC = Jbarcodefactory.getinstance (). createCode128 ();
Jbc.setencoder (Code128encoder.getinstance ());
Jbc.settextpainter (Customtextpainter.getinstance ());
Jbc.setbarheight (Barcode_height);
Jbc.setxdimension (double.valueof (1). Doublevalue ());
Jbc.setshowtext (FALSE);
Jbc.setwideratio (Double.valueof () Doublevalue ());
}
return JBC;
}
/**
* @descript: Generate Barcode file
* @param message Barcode Content
* @param file Makefile
*/
public static void Createbarcode (String message, File file,string text) {
try {
FileOutputStream fos = new FileOutputStream (file);
Createbarcode (message, fos,text);
Fos.close ();
} catch (IOException e) {
throw new RuntimeException (e);
}
}
/**
* @descript: Generate a barcode and write to the specified output stream
* @param message Barcode Content
* @param OS output stream
*/
public static void Createbarcode (String message, OutputStream os,string text) {
try {
Set up barcode text
Text=text;
Create a bufferedimage image of a barcode
BufferedImage image = Getjbarcode (). Createbarcode (message);
Imageutil.encodeandwrite (image, Imageutil.png, OS, barcode_dpi, barcode_dpi);
Os.flush ();
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* Static Inner class
* Custom Textpainter, allow to define font, size, text, etc.
* Refer to the underlying implementation: Baselinetextpainter.getinstance ()
*/
Protected static class Customtextpainter implements Textpainter {
Private static Customtextpainter instance =new Customtextpainter ();
public static Customtextpainter getinstance () {
return instance;
}
public void Painttext (BufferedImage barcodeimage, String text, int width) {
Drawing
Graphics g2d = Barcodeimage.getgraphics ();
Creating fonts
Font font = new Font (font_family, Font.plain, font_size * width);
G2d.setfont (font);
FontMetrics fm = G2d.getfontmetrics ();
int height = fm.getheight ();
int center = (Barcodeimage.getwidth ()-fm.stringwidth (text))/2;
G2d.setcolor (Color.White);
G2d.fillrect (0, 0, barcodeimage.getwidth (), Barcodeimage.getheight () * 1/20);
G2d.fillrect (0, Barcodeimage.getheight ()-(Height * 9/10), Barcodeimage.getwidth (), (height * 9/10));
G2d.setcolor (Color.Black);
Draw text
G2d.drawstring (TEXT, 0, 145);
Drawing barcodes
G2d.drawstring (text, center, Barcodeimage.getheight ()-(HEIGHT/10)-2);
}
}
Test
public static void Main (string[] args) throws FileNotFoundException, IOException {
Jbarcodeutil.createbarcode (data.getidentity (), File, "");
}
}
Java generates one-dimensional code