【问题标题】:How to create Image From Array of Pixel Values and known width and height..?如何从像素值数组和已知宽度和高度创建图像..?
【发布时间】:2012-02-24 01:29:30
【问题描述】:

我有像素值的一维数组,我可以通过这种方式获得红色、绿色和蓝色。

int rgb[] = new int[]
        {
            (argb >> 16) & 0xff, //red
            (argb >>  8) & 0xff, //green
            (argb      ) & 0xff  //blue
        };

我也知道我想要创建的图像的宽度高度。 所以,我总共有以下数据。 1) 新图像的宽度 2) 新图像的高度 3) 像素值的一维数组。

我的主管建议我使用 createRaster 方法,但函数参数对我来说很难理解。 你能给我一些简单的代码吗? 谢谢。

【问题讨论】:

  • 您对 createRaster 中的函数参数有什么难以理解的地方。你的主管给你设置了一项任务,让你学习和理解一些东西,而不是走捷径,只求一些代码。

标签: java image image-processing raster


【解决方案1】:

this 之前的 SO 帖子所述:

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();
            raster.setPixels(0,0,width,height,pixels);
            return image;
        }

如果您无法理解参数是什么,您应该查看Java Documentation

【讨论】:

    【解决方案2】:

    你可以:

     InputStream is = new ByteArrayInputStream(rgb);
     Image img = ImageIO.read(is);
    

    rgb 应该是一个字节数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2011-01-11
      相关资源
      最近更新 更多