0

I try to use itext but I can´t find a way to print a barcode to an image, I only found examples of printing barcode to PDF,I have an image of a credit card , so I need to draw a barcode(card number) to the image,Has someone have an example of how to do it in itext or another example using another library?,

Thanks in advance.

Alan Gaytan
  • 852
  • 4
  • 14
  • 33
  • Any code that you have tried? – DarkV1 Jul 06 '16 at 18:05
  • I dont understand the point well... you need to generate a Barcode and then save it as image over another image you already have????? – ΦXocę 웃 Пepeúpa ツ Jul 06 '16 at 18:06
  • Possible duplicate of [Android Generate QR code and Barcode using Zxing](http://stackoverflow.com/questions/22371626/android-generate-qr-code-and-barcode-using-zxing) – Hitesh Sahu Jul 06 '16 at 18:07
  • Yes I have a credit card image but I need to draw the barcode with the card number and place it on the image, also I try the code of the itext examples, http://developers.itextpdf.com/examples/miscellaneous/bar-codes , but as you can see itext only create pdf files – Alan Gaytan Jul 06 '16 at 18:18
  • There are so many barcode types. Would you clarify which one you really need? – Alexey Subach Jul 06 '16 at 21:09
  • Many barcodes in `iText` have `createAwtImage` method. It will not print any text on the image, though, just pure barcode – Alexey Subach Jul 06 '16 at 21:11

2 Answers2

0

Solution:

Basically, you want to draw a barcode and create a .png. If that is the case the Buffered Image API should do the trick

Example

BufferedImage bufferedImage = new        
BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();

g.fillRect(0,0, 20,20); // draws barcode

Example of Writing to an .png

try {
    // retrieve image
    BufferedImage bi = getMyImage();
    File outputfile = new File("saved.png");
    ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
    ...
}

It creates an image, then writes it to a file.

DarkV1
  • 1,776
  • 12
  • 17
0

I found a solution,hope this help someone else, thanks to all

Create the barcode using itext:

Barcode39 barcode = new Barcode39();
barcode.setCode("7001390283546141");
barcode.setBarHeight(40);

Image img = barcode.createAwtImage(Color.BLACK, Color.WHITE);

BufferedImage outImage = new BufferedImage(img.getWidth(null), img.getHeight(null),BufferedImage.TYPE_INT_RGB);

outImage.getGraphics().drawImage(img, 0, 0, null);
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ImageIO.write(outImage, "png", bytesOut);
bytesOut.flush();
byte[] pngImageData = bytesOut.toByteArray();
FileOutputStream fos = new FileOutputStream("C:/results/barcode.jpg");
fos.write(pngImageData);
fos.flush();
fos.close();

After create the barcode image

final BufferedImage image1 = ImageIO.read(new File("C:/results/image.jpg"));
final BufferedImage image2 = ImageIO.read(new File("C:/results/barcode.jpg"));

Graphics g = image2.getGraphics();
g.drawImage(image2, 0, 0, image2.getWidth(), image2.getHeight(), null);
g.dispose();

final int xMax = image1.getWidth() - image2.getWidth();
final int yMax = image1.getHeight() - image2.getHeight();

Graphics g2 = image1.getGraphics();
Random random = new Random();
int x = random.nextInt(xMax);
int y = random.nextInt(yMax);

g2.drawImage(image2, x, y, null);
g2.dispose();

File outputfile = new File("C:/results/final.jpg");
ImageIO.write(image1, "png", outputfile);
Alan Gaytan
  • 852
  • 4
  • 14
  • 33