5

I have an integer array of RGB pixels that looks something like:

pixels[0] = <rgb-value of pixel(0,0)>
pixels[1] = <rgb-value of pixel(1,0)>
pixels[2] = <rgb-value of pixel(2,0)>
pixels[3] = <rgb-value of pixel(0,1)>
...etc...

And I'm trying to create a BufferedImage from it. I tried the following:

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
img.getRaster().setPixels(0, 0, width, height, pixels);

But the resulting image has problems with the color bands. The image is unclear and there are diagonal and horizontal lines through it.

What is the proper way to initialize the image with the rgb values?

EDIT: Here is what my image looks likealt text

thanks, Jeff

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • setDataElements(...) works fine for me. Your int values must be wrong. Post your SSCCE demonstrating the problem. – camickr Jan 06 '11 at 19:32
  • @camickr, I think you are right and that my underlying pixel values may be wrong. I'll look into this a bit more and then post back. – Jeff Storey Jan 06 '11 at 20:11
  • 1
    This looks like bad data alignment. Your pixels are 32-bit ints, but an integer RBG format has only 24 bits. You should try creating a pixel struct with 3 bytes in it and passing an array of those over instead. – Hannesh Jan 20 '11 at 19:53
  • possible duplicate of [int array to BufferedImage](http://stackoverflow.com/questions/14416107/int-array-to-bufferedimage) – Harald K Apr 09 '14 at 22:05

3 Answers3

4

Try setDataElements instead of setPixels.

Another option is for the image to share the array instead of copying from it (see this answer for an example.)

Community
  • 1
  • 1
finnw
  • 47,861
  • 24
  • 143
  • 221
  • +1, cool, I learned the difference between setDataElements and setPixels. – camickr Jan 06 '11 at 18:08
  • I tried setDataElements, but I still get the same problem. I have added an image of what I'm seeing. Maybe it will give a hint into what might be causing it. – Jeff Storey Jan 06 '11 at 18:12
1

Not sure how to do it with a single array value. I believe you need three array values to specify the color when you use TYPE_INT_RGB:

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class ImageFromArray2 extends JFrame
{
    int width = 50;
    int height = 50;
    int imageSize = width * height;

    public ImageFromArray2()
    {
        JPanel panel = new JPanel();
        getContentPane().add( panel );
        int[] pixels = new int[imageSize * 3];

        //  Create Red Image

        for (int i = 0; i < imageSize * 3; i += 3)
        {
            pixels[i] = 255;
            pixels[i+1] = 0;
            pixels[i+2] = 0;
        }

        panel.add( createImageLabel(pixels) );

        //  Create Green Image

        for (int i = 0; i < imageSize * 3; i += 3)
        {
            pixels[i] = 0;
            pixels[i+1] = 255;
            pixels[i+2] = 0;
        }

        panel.add( createImageLabel(pixels) );

        //  Create Blue Image

        for (int i = 0; i < imageSize * 3; i += 3)
        {
            pixels[i] = 0;
            pixels[i+1] = 0;
            pixels[i+2] = 255;
        }

        panel.add( createImageLabel(pixels) );

        //  Create Cyan Image

        for (int i = 0; i < imageSize * 3; i += 3)
        {
            pixels[i] = 0;
            pixels[i+1] = 255;
            pixels[i+2] = 255;
        }

        panel.add( createImageLabel(pixels) );

    }

    private JLabel createImageLabel(int[] pixels)
    {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        WritableRaster raster = image.getRaster();
        raster.setPixels(0, 0, width, height, pixels);
        JLabel label = new JLabel( new ImageIcon(image) );
        return label;
    }

    public static void main(String args[])
    {
        JFrame frame = new ImageFromArray2();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    TYPE_INT_RGB uses a single int per pixel; OP specifies that each element of the int[] has an RGB value. – Michael Brewer-Davis Jan 06 '11 at 17:51
  • @MIchael Brewer-Davis, you would think that TYPE_INT_RGB uses a single int per pixel unit, however that does not appear to be the case when using the setPixel(...) method, which is why the question was asked. I provided a solution that shows how to use the setPixel(...) method. Having said that, the best answer is to use the setDataElements(...) method. – camickr Jan 06 '11 at 18:13
-1

The reason you cant get the correct image is that those pixels include the rgb colors, in order to set well each pix you most do the next

double[] pixelsArr=new double[4];
pixelsArr[0]=(Integer.parseInt(string2.trim())>>16) & 0xFF;
pixelsArr[1]=(Integer.parseInt(string2.trim())>>8) & 0xFF;
pixelsArr[2]=(Integer.parseInt(string2.trim())) & 0xFF;
pixelsArr[3]=0xFF;
img.getRaster().setPixels(col,row,1,1, pixelsArr);

string2 is an integer pixel col is the position of each pix and row the same, and 1,1 is the size of each pixel.

j0k
  • 22,600
  • 28
  • 79
  • 90