我尝试使用itext
,但我找不到将条形码打印到图像的方法,我只找到打印条形码的示例,以PDF
,我有一张信用卡,所以我需要绘制一个条形码(卡号)的图像,有人有一个例子如何在itext
或另一个例子使用另一个图书馆?,使用java创建条形码并将其绘制到现有图像
在此先感谢。
我尝试使用itext
,但我找不到将条形码打印到图像的方法,我只找到打印条形码的示例,以PDF
,我有一张信用卡,所以我需要绘制一个条形码(卡号)的图像,有人有一个例子如何在itext
或另一个例子使用另一个图书馆?,使用java创建条形码并将其绘制到现有图像
在此先感谢。
我找到了解决方法,希望这种帮助别人的,感谢所有
利用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();
创建后的条码图像
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);
解决方案:
基本上,你想画一个条形码,并创建为.png。如果是这样的话 的Buffered Image API应该做的伎俩
例写作
BufferedImage bufferedImage = new
BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.fillRect(0,0, 20,20); // draws barcode
例到巴纽
try {
// retrieve image
BufferedImage bi = getMyImage();
File outputfile = new File("saved.png");
ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
...
}
它创建了一个图像,然后将它写入到一个文件。
的任何代码,您试过? – DarkV1
我不明白这一点...你需要生成一个条形码,然后将它保存为另一个图像上的图像,你已经有了????? –
[Android使用Zxing生成QR代码和条形码]的可能重复(http://stackoverflow.com/questions/22371626/android-generate-qr-code-and-barcode-using-zxing) –