1

I have the following code, that transforms an Image into a byte[]:

BufferedImage image = ImageIO.read(new File("Path/To/Custom/image.jpg"));
ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
ImageIO.write(image, "jpg", baos);
byte[] imageBytes = baos.toByteArray();

This code works totally fine, at least I get a byte array containing different values. But now comes the difficult part: The byte[] has to be reconstructed into am image again. The following code does not work, ImageIO.read(...) returns null. I read the documentation, but still I can not find out what to change so that the code functions in the way I want it to.

ByteArrayInputStream ba = new ByteArrayInputStream(imageBytes);
BufferedImage image = ImageIO.read(ba);
//image is always null, no matter what the stream or the byte values are.
F_Schmidt
  • 902
  • 1
  • 11
  • 32
  • Can you try to put the ImageIO.read () in a try-catch-block and tell us if an error occurs and if so which one? – verity Jul 13 '20 at 22:11
  • oh sorry for not being specific enough. I already tested in try catch, but there is no exception. ImageIO.read(...) just returns null, but the original image is not null. This code here is very short to simplify things. – F_Schmidt Jul 13 '20 at 22:13

2 Answers2

3
import java.io.ByteArrayOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ByteArrayToImage {
   public static void main(String args[]) throws Exception {
      BufferedImage bImage = ImageIO.read(new File("sample.jpg"));
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ImageIO.write(bImage, "jpg", bos );
      byte [] data = bos.toByteArray();
      ByteArrayInputStream bis = new ByteArrayInputStream(data);
      BufferedImage bImage2 = ImageIO.read(bis);
      ImageIO.write(bImage2, "jpg", new File("output.jpg") );
      System.out.println("image created");
   }
}

Modify this to your needs.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Aayush Panda
  • 544
  • 4
  • 15
  • Your code works, mine does not. But I do not see any difference? – F_Schmidt Jul 13 '20 at 22:19
  • If I am to be honest, I do not know what the difference is. I just typed out from memory what I usually do – Aayush Panda Jul 13 '20 at 22:25
  • Okay, this is also the way how I think it must work. I used you code (which is actually really the same than mine ^^) and tested it. It worked. After that I included the byte[] into a dynamic byte array and then re-read it. After that it did not work. Maybe the streams must reference the same byte[]? – F_Schmidt Jul 13 '20 at 22:33
  • What is this "dynamic byte array"? Can you include the code you use in your question @F_Schmidt – Joni Jul 13 '20 at 23:10
  • Well, it was my mistake. I had an error in a different part of my code. This code snippet works like a charm. Just a hint which might be helpful: I am using this to create a binary network protocol, but for that I use `ImageIO.write(bImage, "png", bos );` instead of jpg, because `png` is a wide spread format. Maybe it helps someone ;) – F_Schmidt Jul 19 '20 at 16:46
1

Try reading an ByteArrayInputStream on ImageIO.read() not an ByteArrayOutputstream.

Dharman
  • 30,962
  • 25
  • 85
  • 135
verity
  • 375
  • 1
  • 11
  • You got a good eye! but it was not the mistake, it was just a copy-paste-fail into stackoverflow ;) – F_Schmidt Jul 13 '20 at 22:19
  • What do you need the BufferedImage for? – verity Jul 13 '20 at 22:23
  • I want to write images byte by byte and send them to a server. I know that I could just use a DataOutputStream, but I do not like that solution. I really want to create this image -> byte and bytes -> image transformation. – F_Schmidt Jul 13 '20 at 22:25
  • But are you sure that the file really exists and is found? – verity Jul 13 '20 at 22:29
  • Read this, maybe it helps you: (But I am not sure) https://stackoverflow.com/a/9944029/13189807 – verity Jul 13 '20 at 22:33