0

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

 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();
        }
4

1 回答 1

0

好吧,我结合了几个条形码的代码看起来像这样

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();
        }
    }
于 2020-04-30T12:26:05.580 回答