【问题标题】:create barcode and draw it to an existing image using java使用java创建条形码并将其绘制到现有图像
【发布时间】:2016-11-08 21:33:30
【问题描述】:

我尝试使用itext,但我找不到将条形码打印到图像的方法,我只找到了将条形码打印到PDF 的示例,我有一张信用卡的图像,所以我需要在图像上绘制条形码(卡号),有人在itext 中提供了如何操作的示例或使用其他库的另一个示例吗?,

提前致谢。

【问题讨论】:

  • 您尝试过任何代码吗?
  • 我不太明白这一点...您需要生成条形码,然后将其另存为图像而不是您已经拥有的另一个图像?????
  • 是的,我有一张信用卡图像,但我需要用卡号绘制条形码并将其放在图像上,我也尝试了 itext 示例的代码 developers.itextpdf.com/examples/miscellaneous/bar-codes ,但正如你可以看到itext只创建pdf文件
  • 条码种类太多了。你能澄清一下你真正需要哪一个吗?

标签: java itext barcode


【解决方案1】:

我找到了一个解决方案,希望这对其他人有帮助,谢谢大家

使用 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);

【讨论】:

    【解决方案2】:

    解决方案:

    基本上,您想绘制一个条形码并创建一个 .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
    

    写入 .png 的示例

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

    它创建一个图像,然后将其写入一个文件。

    【讨论】:

      猜你喜欢
      • 2011-04-04
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      相关资源
      最近更新 更多