I have an integer array of RGB pixels that looks something like:
我有一个RGB像素的整数数组,看起来像:
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。我尝试了以下方法:
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?
使用rgb值初始化图像的正确方法是什么?
EDIT: Here is what my image looks like
编辑:这是我的图像的样子
thanks, Jeff
3 个解决方案
#1
3
Try setDataElements
instead of setPixels
.
尝试使用setDataElements而不是setPixels。
Another option is for the image to share the array instead of copying from it (see this answer for an example.)
另一个选择是图像共享数组而不是从中复制(请参阅此答案以获取示例。)
#2
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:
不确定如何使用单个数组值。我相信当你使用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 );
}
}
#3
-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
你无法获得正确图像的原因是这些像素包含rgb颜色,以便很好地设置你最常做的每个像素
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.
string2是整数像素col,每个像素和行的位置相同,1,1是每个像素的大小。
#1
3
Try setDataElements
instead of setPixels
.
尝试使用setDataElements而不是setPixels。
Another option is for the image to share the array instead of copying from it (see this answer for an example.)
另一个选择是图像共享数组而不是从中复制(请参阅此答案以获取示例。)
#2
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:
不确定如何使用单个数组值。我相信当你使用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 );
}
}
#3
-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
你无法获得正确图像的原因是这些像素包含rgb颜色,以便很好地设置你最常做的每个像素
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.
string2是整数像素col,每个像素和行的位置相同,1,1是每个像素的大小。