2017-11-10 134 views
0

H ow我可以將給定的Pixmap轉換爲字節數組嗎?從像素讀取字節 - libGDX

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); 

無法從圖像數據加載像素圖

+0

檢查[這](https://stackoverflow.com/questions/34482186/ extract-byte-from-libgdx-texture)主題 – Squiddie

+0

@Squiddie這些代碼行的問題,它不能在HTML項目上運行。 – Crowni

+0

我有原始數據沒有編碼數據這是我的主要問題。 – Crowni

回答

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)); 
    } 
}