Created
December 10, 2014 00:18
-
-
Save rodrik/296f99e39c9d055d6a57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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