ARGB_8888 形式のビットマップを、アプリケーションのネイティブ部分で使用する必要がある単一チャネルのバイト配列に変換しようとしています。
を使用していくつかのサニティチェックを行いました
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
int alphaValue = Color.alpha(pixel);
そして、形式は実際には ARGB ではなく RGBA のようです。
このタスクを達成するために次のコードを使用しました
public static byte[] bitmapToByteArray(Bitmap bm) {
// Create the buffer with the correct size
int iBytes = bm.getWidth() * bm.getHeight() ;
byte[] res = new byte[iBytes];
Bitmap.Config format = bm.getConfig();
if (format == Bitmap.Config.ARGB_8888)
{
ByteBuffer buffer = ByteBuffer.allocate(iBytes*4);
// Log.e("DBG", buffer.remaining()+""); -- Returns a correct number based on dimensions
// Copy to buffer and then into byte array
bm.copyPixelsToBuffer(buffer);
byte[] arr = buffer.array();
for(int i=0;i<iBytes;i++)
{
int A,R,G,B;
R=(int)(arr[i*4+0]) & 0xff;
G=(int)(arr[i*4+1]) & 0xff;
B=(int)(arr[i*4+2]) & 0xff;
//A=arr[i*4+3];
byte r = (byte)(0.2989 * R + 0.5870 * G + 0.1140 * B) ;
res[i] = r;
}
}
if (format == Bitmap.Config.RGB_565)
{
ByteBuffer buffer = ByteBuffer.allocate(iBytes*2);
// Log.e("DBG", buffer.remaining()+""); -- Returns a correct number based on dimensions
// Copy to buffer and then into byte array
bm.copyPixelsToBuffer(buffer);
byte[] arr = buffer.array();
for(int i=0;i<iBytes;i++)
{
float A,R,G,B;
R = ((arr[i*2+0] & 0xF8) );
G = ((arr[i*2+0] & 0x7) << 5) + ((arr[i*2+1] & 0xE0) >> 5);
B = ((arr[i*2+1] & 0x1F) << 3 );
byte r = (byte)(0.2989 * R + 0.5870 * G + 0.1140 * B) ;
res[i] = r;
}
}
// Log.e("DBG", buffer.remaining()+""); -- Returns 0
return res;
}
}
バイト配列は、デバッグの理由でリモート サーバーに送信されています。サーバーでビットマップを再構築すると、画像がすべてピクセル化され、グレーレベルに奇妙な値が表示されます。私が間違っていることは何ですか?どんなコメントでも大歓迎です。ありがとう