gpt4 book ai didi

java - 如何将多个条形码图像添加到一张PNG图像中?

转载 作者:行者123 更新时间:2023-12-01 17:00:52 25 4
gpt4 key购买 nike

我的情况是我需要在一个 png 文件中添加使用 Barcode4j 库生成的多个条形码 (png)。我找不到任何例子,也无法下定决心解决它。因此,任何帮助将不胜感激。好吧,我以通常的方式(通过 for)生成条形码并将它们收集在 bufferedImages 列表(列表)中。现在我需要将这些图像粘合在一起。 我的代码:

 try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, "image/x-png", 300, BufferedImage.TYPE_BYTE_BINARY, false, 0);
List<BufferedImage> bufferedImageList = new ArrayList<>(); // list for bufferedImages
for (int i = 0; i < barcodesList.size(); i++) {

try {
Code128Bean code128 = new Code128Bean();
code128.setHeight(15f);
code128.setModuleWidth(0.3);
code128.setQuietZone(10);
code128.doQuietZone(true);

code128.generateBarcode(canvas, (String) barcodesList.get(i));
bufferedImageList.add(canvas.getBufferedImage()); // collect images of barcode in cicle
canvas.finish();

} catch (Exception e) {
e.printStackTrace();
}
}

FileOutputStream fos = new FileOutputStream(barcodePath.toString());
// to do smth to make one png from collected images
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}

最佳答案

嗯,我的代码组合了几个条形码,看起来像这样

ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, "image/x-png", 300, BufferedImage.TYPE_BYTE_BINARY, false, 0);
List<BufferedImage> bufferedImageList = new ArrayList<>(); // list for bufferedImages
int sumHeight = 0;
int sumWhide = 0;
int columns = 2;
int rows = 0;
for (int i = 0; i < barcodesList.size(); i++) {

try {
Code128Bean code128 = new Code128Bean();
code128.setHeight(15f);
code128.setModuleWidth(0.3);
code128.setQuietZone(10);
code128.doQuietZone(true);

code128.generateBarcode(canvas, (String) barcodesList.get(i));
sumHeight = sumHeight + canvas.getBufferedImage().getHeight();
sumWhide = sumWhide + canvas.getBufferedImage().getWidth();
bufferedImageList.add(canvas.getBufferedImage()); // collect images of barcode in cycle
canvas.finish();

} catch (Exception e) {
e.printStackTrace();
}
}


double dbl = barcodesList.size() / (double) columns;
rows = (int)Math.ceil(dbl);


int oneWhide = sumWhide / barcodesList.size();
int imgWhide = oneWhide * columns;


int oneHeight = sumHeight / barcodesList.size();
int imgHeight = oneHeight * rows;


BufferedImage bigImage = new BufferedImage(imgWhide, imgHeight, BufferedImage.TYPE_BYTE_BINARY);
Graphics g = bigImage.getGraphics();
int x = 0;
int y = 0;
for (BufferedImage bufferedImage : bufferedImageList) {

g.drawImage(bufferedImage, x, y, null);
x += oneWhide;
if(x >= bigImage.getWidth()){
x = 0;
y += bufferedImage.getHeight();
}
}
ImageIO.write(bigImage,"png",new File(barcodePath.toString()));



} catch (Exception e) {
e.printStackTrace();
}
}

关于java - 如何将多个条形码图像添加到一张PNG图像中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61506785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com