0

This post is a follow up to : ImageIO.read can't read ByteArrayInputStream (image processing)

Similar to the OP, I am getting a null pointer whenever I try to read from my ByteArrayInputStream (as it should, as explained by the top answer). Noticing this, I have implemented the code from the @haraldK 's answer from the post above in order to correct this issue, but I have run into another problem. I have the following code:

byte[] imageInByteArr = ...

// convert byte array back to BufferedImage
int width = 1085;
int height = 696;
BufferedImage convertedGrayScale = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
convertedGrayScale.getRaster().setDataElements(0, 0, width, height, imageInByteArr );

try {
    ImageIO.write(convertedGrayScale, "jpg", new File("C:\\test.jpg"));
}
catch (IOException e) {
    System.err.println("IOException: " + e);
}

Upon execution, I run into a java.lang.ArrayIndexOutOfBoundsException: null error on the line right before the try/catch block. My first thought was that this null pointer was arising for not having a file in my C drive called test.jpg. I adjusted to fix that worry, yet I am still getting the same null pointer issue at convertedGrayScale.getRaster().setDataElements(0, 0, width, height, imageInByteArr );. Why is this happening?

On another note, aside from writing the file uining ImageIO, is there ANY other way for me to convert the byte[] into a visual representation of an image? I have tried to just print the array onto a file and saving it as a '.jpg', but the file will not open. Any suggestions will help. To summarize, I am looking to convert a byte[] into an image and save it OR render it onto a browser. Whichever is easier/doable.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Where did `imageInByteArr` come from? It must be an array of exactly 755,160 (that is, 1,085×696) bytes. If you read it directly from a file or URL, you don’t have an array of data elements; you have a snapshot of a most-likely compressed image format, which cannot be directly applied to a Raster (which is exactly what the answer in that other question pointed out). – VGR Dec 12 '18 at 22:47
  • Maybe `BufferedImage.TYPE_BYTE_GRAY` supports an alpha-channnel, thus your array might not contain enough elements due to that information missing? – Poohl Dec 12 '18 at 23:50
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Dec 13 '18 at 00:12

1 Answers1

0

it appears that your imageInByteArr is too short. I was able to get the same error you get from this

public static void main(String[] args) {
    int width = 1085;
    int height = 696;
    byte[] imageInByteArr = new byte[width ];
    BufferedImage convertedGrayScale = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    convertedGrayScale.getRaster().setDataElements(0, 0, width, height, imageInByteArr);

}

when using width*height for size of imageInByteArr or anything bigger i get no error, but when it's smaller than the data you are trying to update it throws the exception.

mavriksc
  • 1,130
  • 1
  • 7
  • 10