【问题标题】:How can I display an image from an array of pixels' values in java?java - 如何在java中显示像素值数组中的图像?
【发布时间】:2011-09-03 15:53:30
【问题描述】:

我打算在窗口内显示一个 28x28 像素的图像。像素具有“0”值,所以我希望它显示一个带有 28x28 的黑色方块的窗口。但是没有显示图像。也许数组的数据(我不确定像素值是否必须是 0 到 255 范围内的 int)必须是其他数据才能显示图像。谢谢!

公开课 ASD {

public static Image getImageFromArray(int[] pixels, int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = (WritableRaster) image.getData();
    System.out.println(pixels.length + " " + width + " " + height);
    raster.setPixels(0,0,width,height,pixels);
    return image;
}

public static void main(String[] args) throws IOException {
    JFrame jf = new JFrame();
    JLabel jl = new JLabel();

    int[] arrayimage = new int[784];
    for (int i = 0; i < 28; i++)
    {   for (int j = 0; j < 28; j++)
            arrayimage[i*28+j] = 0;
    }
    ImageIcon ii = new ImageIcon(getImageFromArray(arrayimage,28,28));
    jl.setIcon(ii);
    jf.add(jl);
    jf.pack();
    jf.setVisible(true);
}

【问题讨论】:

    标签: java image


    【解决方案1】:

    image.getData() 返回栅格的副本。 也许如果您在修改栅格后调用image.setData(raster) 会看到结果。

    此外,应该为 setPixels 提供一个足够大的数组,以填充栅格的所有波段(A、R、G、B)。在我将像素大小增加到 28*28*4 之前,我得到了一个数组索引越界异常。

    对于 TYPE_INT_RGB,以下应生成白色图像:

    public class ASD
    {
      public static Image getImageFromArray(int[] pixels, int width, int height)
      {
        BufferedImage image =
            new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        WritableRaster raster = (WritableRaster) image.getData();
        raster.setPixels(0, 0, width, height, pixels);
        image.setData(raster);
        return image;
      }
    
      public static void main(String[] args) throws IOException
      {
        JFrame jf = new JFrame();
        JLabel jl = new JLabel();
    
        //3 bands in TYPE_INT_RGB
        int NUM_BANDS = 3;
        int[] arrayimage = new int[28 * 28 * NUM_BANDS];
    
        for (int i = 0; i < 28; i++)
        {
          for (int j = 0; j < 28; j++) {
            for (int band = 0; band < NUM_BANDS; band++)
              arrayimage[((i * 28) + j)*NUM_BANDS + band] = 255;
          }
        }
        ImageIcon ii = new ImageIcon(getImageFromArray(arrayimage, 28, 28));
        jl.setIcon(ii);
        jf.add(jl);
        jf.pack();
        jf.setVisible(true);
      }
    }
    

    【讨论】:

    • 我已更改:1) 数组大小为 28*28*3(所有 0 值),2)TYPE_INT_ARGB 为 TYPE_INT_RGB,3) 并在 setPixels 之后调用 image.setData(raster)。但仍然没有显示图像。
    • @Alexander,上面是您的代码更改为生成白色图像。无论如何它对我有用......你得到一个 JFrame,还是你的程序终止而没有任何影响?
    【解决方案2】:

    我不知道这是问题所在,但您正在使用 TYPE_INT_ARGB。这包括压缩整数中的 Alpha 通道 (opacy),值 0 表示完全透明。

    另一个(阅读docs!):

    BufferedImage.getData() :  The Raster returned is a copy of the image data is not updated if the image is changed.
    

    我认为,您必须调用 setData() 将新像素放置在图像中。

    【讨论】:

    • 好的,我改成TYPE_INT_RGB;但问题仍然存在。而且无论数组值如何,图像都不会显示,无论是 0 还是任何其他值。谢谢。
    • 每个通道的数组必须是 size = widthheight*3;其中 0..widthheight 包含红色的值,widthheight+1..2*widthheight 包含绿色和 2*widthheight+1..width 的值height*3 蓝色的值?谢谢。
    • TYPE_INT_RGB 将组成一个像素的三个样本(每个像素一个)打包成一个整数。
    猜你喜欢
    • 2012-06-03
    • 2012-01-27
    • 2016-04-17
    • 2015-03-07
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    相关资源
    最近更新 更多