barcode generation

At present, the application scenarios of QR codes have spread all over various Internet platforms. Usually, the unique number of the product/commodity is stored in the QR code for scanning identification.
And barcode technology still exists for production environments, such as hardware equipment manufacturing, supply, logistics and so on.
In common product information management and material order systems, there are multiple demand scenarios for generating and printing barcodes (one-dimensional codes).
Barcode format:


 
solution
1) barcode4j
barcode4j is an open source Java barcode generation library. Support a variety of encoding formats, such as: code-39, code-128, etc.
http://barcode4j.sourceforge.net/
2) zxing
It is a 1D/2D codec library open sourced by Google. The goal is to be able to decode QR codes, Data Matrix, UPC 1D barcodes. It provides clients under various platforms including: J2ME, J2SE and Android
This time, barcode4j is used as the solution
Environmental preparation
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j-light</artifactId>
<version>2.0</version>
</dependency>

Sample code:

package com.vip.utils;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.lang3.math.NumberUtils;
import org.krysalis.barcode4j.impl.int2of5.Interleaved2Of5Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

public class BarcodeUtil {
	/** Interleaved 25 barcodes */
	private static final Interleaved2Of5Bean bean = new Interleaved2Of5Bean();;
	/** Set the precision */
	private static final int dpi = 150;
	
	static {
		// Interleaved 25 barcode parameter initialization
		double moduleWidth = UnitConv.in2mm(1.0f / dpi);
		bean.setModuleWidth(moduleWidth);
		bean.setHeight(7L);

		bean.setWideFactor(3L);
		bean.doQuietZone(true);
                //Set the size of the displayed numbers
		bean.setFontSize(0);
	}

	public static void generate(String msg, OutputStream ous, String contentType) {
		if(!NumberUtils.isDigits(msg)){
			throw new IllegalArgumentException("Barcode parameter exception:"+msg);
		}
		if (StringUtils.isBlank(contentType)) {
			contentType = "image/gif";
		}
		try {
			BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, contentType, dpi,
					BufferedImage.TYPE_BYTE_BINARY, false, 0);
			bean.generateBarcode(canvas, msg);
			canvas.finish();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327104303&siteId=291194637