从像素读取字节 - libGDX

时间:2017-11-10 20:28:13

标签: java image-processing libgdx

H ow我可以将给定的Pixmap转换为字节数组吗?

byte[] bytes = Gdx.files.internal("image.png").readBytes();

// pass the correct bytes into pixmap
Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);

byte[] sameBytes = new byte[SIZE];
int count = 0;
for (int x = 0; x < pixmap.getWidth(); x++) {
     for (int y = 0; y < pixmap.getHeight(); y++) {
          int pixel = pixmap.getPixel(x, y);

          sameBytes[count++] = ((pixel >> 24) & 0xff);
          sameBytes[count++] = ((pixel >> 16) & 0xff);
          sameBytes[count++] = ((pixel >>  8) & 0xff);
          sameBytes[count++] = ((pixel      ) & 0xff);
     }
}

// sameBytes is not correct bytes into pixmap
Pixmap pixmap = new Pixmap(sameBytes, 0, sameBytes.length);
  

无法从图像数据加载像素图

1 个答案:

答案 0 :(得分:0)

也许这可能有效

for (int x = 0; x < pixmap.getWidth(); x++) {
  for (int y = 0; y < pixmap.getHeight(); y++)
    int pixel = pixmap.getPixel(x, y);
    sameBytes[count++] = ((pixel & 0xff000000) >>> 24);
    sameBytes[count++] = ((pixel & 0x00ff0000) >>> 16);
    sameBytes[count++] = ((pixel & 0x0000ff00) >>> 8);
    sameBytes[count++] = ((pixel & 0x000000ff));
  }
}