0

I'm trying to display multiple images from a directory using ImageIO and ByteArrayOutputStream.

For one image I can do this easily:

public byte[] getImage() {
    try {
        InputStream inputStream = new FileInputStream("image.jpg");

        BufferedImage bufferedImage = ImageIO.read(inputStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);

        return byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    return null;
}

What is the best approach to display multiple images?

I'm doing the following:

  1. Create a new Buffered Image with size appropriate to fit the images.
  2. Get the graphics object from the new Buffered Image
  3. Draw the images via the graphics object

Here is the code I'm using:

    public byte[] retrieveFaceDb() {

        // TODO:

        String faceDbPath = Utils.commandLineArgs[2];

        File dir = new File(faceDbPath);

        if (dir.isDirectory()) {

            ByteArrayOutputStream finalByteArrayOutputStream = new ByteArrayOutputStream();

            BufferedImage[] buffImages = new BufferedImage[10];

            int type = 0;
            int idx = 0;
            for (final File f : dir.listFiles()) {

                try {
                    buffImages[idx] = ImageIO.read(f);
                    type = buffImages[idx].getType();

                    idx ++;

                } catch (final IOException e) {
                    // handle errors here
                }
            }

            int num = 0;
            BufferedImage finalBufferedImage = new BufferedImage(buffImages[0].getWidth()*10, buffImages[0].getHeight(), type);
            for (int i = 0; i < 1; i++) {
                for (int j = 0; j < 10; j++) {
                    finalBufferedImage.createGraphics().drawImage(buffImages[num], buffImages[0].getWidth()*j, buffImages[0].getHeight()*i, null);
                    num ++;
                }
            }

            try {
                ImageIO.write(finalBufferedImage, "png", finalByteArrayOutputStream);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return finalByteArrayOutputStream.toByteArray();

//          try {
//              return java.nio.file.Files.readAllBytes(dir.toPath());
//          } catch (IOException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }
        }

        return null;
    }

Is there a better way to do this? Thank you.

mdp
  • 171
  • 1
  • 1
  • 8
  • 2
    Which difficulties? – Nicolas Filotto Sep 09 '16 at 16:44
  • 2
    If the image is already a JPEG, why are you writing it to the byte array as another JPEG? For that matter, why use ImageIO at all? Why not just use [Files.readAllBytes](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllBytes-java.nio.file.Path-)? – VGR Sep 09 '16 at 17:23
  • @NicolasFilotto - It's more on the approach. So for now what I do is: 1. Create a new Buffered Image with size appropriate to fit the images. 2. Get the graphics object from the new Buffered Image 3. Draw the images via the graphic object – mdp Sep 09 '16 at 17:43
  • so please clarify your question, if you don't want to see it been closed – Nicolas Filotto Sep 09 '16 at 17:44
  • @VGR - Thank you for your suggestion. I gave it a try and I'm getting java.nio.file.AccessDeniedException. – mdp Sep 09 '16 at 17:52
  • 1
    You talk about displaying images, however, your code does no attempt at displaying anything.. So, you are asking for better way to do... what exactly? – Harald K Sep 10 '16 at 17:55
  • @haraldK - I guess I should clarify that this method has a RequestMapping annotation and is used in a Spring Controller. So when doing a GET request - the multiple images are returned as one and displayed in the browser for ex. – mdp Sep 10 '16 at 18:09

0 Answers0