0

My case is what I need to add several barcodes (png) which I generated using Barcode4j library in one png file. I couldn't find any examples, also couldn't make up my mind to solve it. So any help will be appreciated. Well, I generate barcodes in usual way (throug for) and collecte them in a list of bufferedImages (List). Now I need to glue this images in one. My code:

 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();
        }
  • Does this help? [How to combine multiple PNGs into one big PNG file?](https://stackoverflow.com/questions/3922276/how-to-combine-multiple-pngs-into-one-big-png-file) – Abra Apr 29 '20 at 17:22

1 Answers1

0

Well, my code which combine several barcodes looks like this

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