Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a buffered image from rgb pixel values

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

like image 908
Jeff Storey Avatar asked Oct 13 '22 18:10

Jeff Storey


2 Answers

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.)

like image 141
finnw Avatar answered Oct 18 '22 00:10

finnw


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 );
    }
}
like image 44
camickr Avatar answered Oct 18 '22 00:10

camickr