Java uses barcode4j to generate barcodes

Source: Internet
Author: User
Tags string format

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.