7

ネットワークから生の画像を読み込んでいます。この画像は、ファイルからではなく、イメージ センサーによって読み取られました。

これらは、画像について私が知っていることです:
~ 高さと幅
~ 合計サイズ (バイト単位)
~ 8 ビット グレースケール
~ 1 バイト/ピクセル

この画像をビットマップに変換してイメージビューに表示しようとしています。

これが私が試したことです:

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.outHeight = shortHeight; //360
opt.outWidth = shortWidth;//248
imageBitmap = BitmapFactory.decodeByteArray(imageArray, 0, imageSize, opt);

画像をデコードできないため、decodeByteArray はnullを返します。

また、最初にバイト配列に変換せずに、入力ストリームから直接読み取ろうとしました。

imageBitmap = BitmapFactory.decodeStream(imageInputStream, null, opt);

これもnullを返します。

このフォーラムや他のフォーラムを検索しましたが、これを達成する方法が見つかりません。

何か案は?

編集:最初に行ったのは、ストリームに実際に生の画像が含まれているかどうかを確認することでした。他のアプリケーション (iPhone/Windows MFC) を使用してこれを行いましたが、それらはそれを読み取って画像を正しく表示できます。Java/Android でこれを行う方法を理解する必要があります。

4

4 に答える 4

14

Android does not support grayscale bitmaps. So first thing, you have to extend every byte to a 32-bit ARGB int. Alpha is 0xff, and R, G and B bytes are copies of the source image's byte pixel value. Then create the bitmap on top of that array.

Also (see comments), it seems that the device thinks that 0 is white, 1 is black - we have to invert the source bits.

So, let's assume that the source image is in the byte array called Src. Here's the code:

byte [] src; //Comes from somewhere...
byte [] bits = new byte[src.length*4]; //That's where the RGBA array goes.
int i;
for(i=0;i<src.length;i++)
{
    bits[i*4] =
        bits[i*4+1] =
        bits[i*4+2] = ~src[i]; //Invert the source bits
    bits[i*4+3] = 0xff; // the alpha.
}

//Now put these nice RGBA pixels into a Bitmap object

Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bm.copyPixelsFromBuffer(ByteBuffer.wrap(bits));
于 2011-04-12T14:31:49.443 に答える
1

カメラプレビューコールバックから取得したバイトストリームをデコードするためにこのようなことをしたら、次のようになります。

    Bitmap.createBitmap(imageBytes, previewWidth, previewHeight, 
                        Bitmap.Config.ARGB_8888);

試してみる。

于 2011-04-11T20:32:32.407 に答える
0

ストリームからドローアブル作成を使用します。HttpResponse でそれを行う方法は次のとおりですが、必要に応じて入力ストリームを取得できます。

  InputStream stream = response.getEntity().getContent();

  Drawable drawable = Drawable.createFromStream(stream, "Get Full Image Task");
于 2011-04-11T20:47:35.553 に答える