繁体   English   中英

将.bmp图像加载到二维字节数组中

[英]loading a .bmp image into a 2-D byte array

我有一个30 x 40像素的.bmp文件,我想将其加载到声明如下的inputData中:

byte[][] inputData = new byte[30][40];

我对编程还比较陌生,因此任何人都可以告诉我应该使用哪些类进行编程? 谢谢!

我不知道如何访问同一包中的.bmp文件,并将相应的(x, y)位置分配到我2-D byte array 到目前为止,我有以下内容:

for (int x = 0; x < inputData.length; x++)
{
    for (int y = 0; y < inputData[x].length; y++)
    {
        // inputData[x][y] =
    }
}

您有一个1像素等于1字节的想法,这是不正确的。 RGB像素已经是每个像素3个字节。 另外,BMP文件不是像素阵列,而是压缩图像。 简单加载到阵列将无济于事。 最好使用一些现成的库。

看这里:

GIF http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html

BMP http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

TGA http://www.java-tips.org/other-api-tips/jogl/loading-compressed-and-uncompressed-tgas-nehe-tutorial-jogl.html

您可以在Java 5+中使用ImageIO将BMP文件读入BufferedImage BufferedImage已经可以转换为int[]

在您的情况下,将绿色通道提取到字节数组中:

BufferedImage img = ImageIO.read(new File("example.bmp"));
// you should stop here
byte[][] green = new byte[30][40];
for(int x=0; x<30; x++){
  for(int y=0; y<40; y++){
     int color = img.getRGB(x,y);
     //alpha[x][y] = (byte)(color>>24);
     //red[x][y] = (byte)(color>>16);
     green[x][y] = (byte)(color>>8);
     //blue[x][y] = (byte)(color);
  }
}
byte[][] inputData = green;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM