Create a buffered image from rgb pixel values

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

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. 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 color bars. The image is fuzzy, and diagonal and horizontal lines pass through it.

What is the correct way to initialize an image using rgb values?

EDIT: This is what my image looks like alt text

thanks jeff

+4
source share
3 answers

Try setDataElements instead of setPixels .

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

+3
source

Not sure how to do this with a single array value. I believe that you need three array values ​​to indicate the color when using 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 ); } } 
+1
source

The reason you cannot get the right image is because these pixels include rgb colors to best customize every pixel that you perform most

 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 - integer pixel col - the position of each pixel and line is the same, and 1.1 is the size of each pixel.

-1
source

Source: https://habr.com/ru/post/1334538/


All Articles