So I have a byte array representing pixel data (8bit grayscale). No header. No nothing. Just the data. I want to create a buffered image from this data. I did
image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setDataElements(0, 0, w, h, data);
this.x = w;
this.y = h;
scalemode=false;
exactmode=true;
where w is just width in pixel,h is height in pixel,data is the byte array and image is BufferedImage
here is my paint method
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int rx = (this.getWidth() - x) / 2;
int ry = (this.getHeight() - y) / 2;
g2d.drawImage(image, rx, ry,x,y, null);
}
I, however, get this image (the real image is a fingerprint, which is mostly white pixel)
What went wrong? I tried saving the data as is and then viewing it in Photoshop. The data is fine.
[edit] Never mind this problem. I fucked up in other part of the code and was not aware. Thank you for all inputs though
3
Answers
draw every byte on each pixel…
you didn’t set up your Buffer properly…
see http://docs.oracle.com/javase/7/docs/api/java/awt/image/WritableRaster.html#setDataElements%28int,%20int,%20java.lang.Object%29
It’s hard to know exactly what is wrong, as you haven’t posted enough information. We don’t know
w
,h
or what information is in yourdata
. We don’t know what the image should look like.However, here’s some code that does pretty much exactly what you are doing, and it works for me:
Here is the result: