从包含 3 通道像素信息的 3d 字节数组创建 java 位图图像

creating a java bitmap image from a 3d byte array which contains 3 channel pixel information

我有一个像这样的 3 维字节数组:

byte[][][] data = pyf.callAttr("test", red,green,blue).toJava(byte[][][].class);

此处byte[0]包含具有红色通道像素值的二维数组

byte[0] 是一个 480x360 数组,与 byte[1]byte[2] 相同,分别包含绿色和蓝色
现在我想从这个 3 维字节数组

创建一个 360x480 java 位图图像

试试这个作为起点 它不适用于 android,因为 android 不支持 AWT。

public Image createImage(byte[][][] bytes){
   Image image = new BufferedImage(360,480,BufferedImage.TYPE_INT_RGB);
   Graphics g = image.getGraphics();
   for(int x=0;x<480;x++){
      for(int y=0;y<360;y++){
         //Construct color
         Color color=new Color(bytes[0][x][y],bytes[1][x][y],bytes[2][x][y]);
         g.setColor(color);//Set color
         g.fillRect(x,y,1,1);//Fill pixel
      }
   }
   return image;
}

您将不得不添加错误检查,也许还有一些事情,比如传递宽度和高度,...

问题的某些部分我不清楚,比如你是否要旋转图像(480x360 到 360x480 的部分)。

因此,我假设以下情况:
(1) 你有一个名为 data 的正确字节数组,它包含你所描述的数据
(2) 你需要从数据
创建一个位图 (3) 如有必要,您可以旋转图像(即以下代码不会旋转图像)

  • 以下代码未经测试。但它应该能让你走上正轨。
int width = 480;
int height = 360;

int alpha = 1; // change alpha as necessary

Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);

for(int x=0; x<width; x++) {
    for(int y=0; y<height; y++) {
        int red = data[0][x][y];
        int green = data[1][x][y];
        int blue = data[2][x][y];

        int color = Color.argb(alpha, red, green, blue);
        
        bmp.setPixel(x, y, color);
    }
}