use*_*490 2 java arrays image image-processing
我有一个2D数组,其中包含RGB值。我需要根据这些像素值创建一个有效的图像并将其保存。我在下面给出了2D数组。我想在我的项目中实现这一部分,所以请帮助我。谢谢。
         int[] pixels = new int[imageSize * 3];
         int k = 0;
         for(int i=0; i<height; i++)
         {
            for(int j=0; j<width; j++)
            {
                if(k<imageSize*3)
               {
                    pixels[k] = r[i][j];
                    pixels[k+1] = g[i][j];
                    pixels[k+2] = b[i][j];
                }
               k = k+3;
            }
         }
您可以构建一个BufferedImagetype BufferedImage.TYPE_INT_RGB。此类型将颜色表示为ìnt:
您可以RGB按以下方式获取像素值:
int rgb = red;
rgb = (rgb << 8) + green; 
rgb = (rgb << 8) + blue;
范例(Ideone完整范例程式码):
  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
  for (int y = 0; y < height; y++) {
     for (int x = 0; x < width; x++) {
        int rgb = r[y][x];
        rgb = (rgb << 8) + g[y][x]; 
        rgb = (rgb << 8) + b[y][x];
        image.setRGB(x, y, rgb);
     }
  }
  File outputFile = new File("/output.bmp");
  ImageIO.write(image, "bmp", outputFile);
| 归档时间: | 
 | 
| 查看次数: | 5395 次 | 
| 最近记录: |