Get bytes from iText's Barcode39 image
how can I get the bytes from an image generated using itext's barcode39 class ? I have:
Document document = new Document(new Rectangle(340, 842));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
PdfCont开发者_运维百科entByte cb = writer.getDirectContent();
Barcode39 code39ext = new Barcode39();
code39ext.setCode("Testing Text");
code39ext.setStartStopText(false);
code39ext.setExtended(true);
Image img = code39ext.createImageWithBarcode(cb, null, null);
Now I need help to get the bytes from img in order to send it via email and save it to a file.
Thanks in advance.
Assuming that you actually do not need the PDF file but only the barcode image, then you might try:
Barcode39 code39ext = new Barcode39();
code39ext.setCode("Testing Text");
code39ext.setStartStopText(false);
code39ext.setExtended(true);
java.awt.Image rawImage = code39ext.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();
This should just create the barcode image, render it to memory and save it to a stream / byte[] for further usage.
精彩评论