mimimi
mimimi

Reputation: 78

Java Greyscale buffered image

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)

messed up image

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

Upvotes: 1

Views: 1700

Answers (3)

Harald K
Harald K

Reputation: 27084

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 your data. 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:

// Set up h/w and backing data
int w = 300;
int h = 200;
byte[] data = new byte[w * h];

// Create a smooth gradient
for (int y = 0; y < h; y++) {
    int off = y * w;

    for (int x = 0; x < w; x++) {
        data[off + x] = (byte) (Math.round((x / (double) w) * 127) 
                              + Math.round((y / (double) h) * 127));
    }
}

// Create BufferedImage from data
final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setDataElements(0, 0, w, h, data);

// Show it all in a window
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        JFrame frame = new JFrame(getClass().getSimpleName());
        frame.add(new JLabel(new ImageIcon(image)));

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
});

Here is the result:

Image of JFrame from above code

Upvotes: 2

Martin Frank
Martin Frank

Reputation: 3454

you didn't set up your Buffer properly...

byte[] data = ... 
DataBufferByte db = new DataBufferByte(data, w*h);
image.getRaster().setDataElements(0, 0, w, h, db );

see http://docs.oracle.com/javase/7/docs/api/java/awt/image/WritableRaster.html#setDataElements%28int,%20int,%20java.lang.Object%29

Upvotes: -1

Martin Frank
Martin Frank

Reputation: 3454

draw every byte on each pixel...

BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
for (int dy = 0; dy < h; dy ++){
    for(int dx = 0; dx < w; dx ++){
        int index = dy*w + dx;
        int rgb = data[index];
        rgb = rgb << 24 & 0xFFFF; //BufferedImage.TYPE_BYTE_GRAY consideres only the red-channel;
        //rgb = 00 data[index] FF FF
        image.setRGB(dx,dy,rgb);
        }
    }
}

Upvotes: 1

Related Questions