Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package br.com.gcargo.manager.web.controller.util;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.lowagie.text.pdf.Barcode128;
public class BarcodeGenerator {
public static byte[] generateBarcode(String code) throws IOException {
Barcode128 barcode128 = new Barcode128();
barcode128.setCodeType(Barcode128.CODE128);
barcode128.setCode(code);
java.awt.Image rawImage = barcode128.createAwtImage(Color.BLACK, Color.WHITE);
BufferedImage outImage = new BufferedImage(rawImage.getWidth(null), rawImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
outImage.getGraphics().drawImage(rawImage, 0, 0, null);
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ImageIO.write(outImage, "png", bytesOut);
bytesOut.flush();
byte[] pngImageData = bytesOut.toByteArray();
return pngImageData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment