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

标签 java barcode4j

我的情况是我需要在一个 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/

相关文章:

java - 使用 Barcode4j 为多组 AI 和数据生成 EAN128 代码的问题

java - 使用barcode4j 添加补充到EAN13 条形码

java - 如何在 Java 中生成有效的 EAN13 条形码?

java - 在 web.xml 中发现以元素 'display-name' 开头的无效内容

java - sun.misc.BASE64 到 apache commons

java - 如何使用将加载 JPA 实体的动态 id 属性在 Play Framework v1.2.7 中配置 POST 路由

java - 给出错误 "EXception in thread "main"java.util.InputMistmatchException... .", I can' t 找到错误。请解释一下

Barcode4j - 在 EAN13 条形码中生成校验位

java - 条形码人类可读放置平行于条形码

java - LibGdx - 如何从播放器获取输入,同时也能够从舞台获取输入