2013-09-05 13 views
7

ソケットを使用して受け取ったバイト配列をどのように変換できますか。Androidバイト配列をビットマップに変換する

  1. C++クライアントは、ucharタイプの画像データを送信します。

  2. アンドロイド側では、このuchar配列を-128から+127の範囲のbyte []として受け取ります。

私がしたいことは、このデータを受信して​​表示することです。そのために私はBitmapFactory.decodeByteArray()を使ってビットマップに変換しようとしていましたが、幸運なことにnullビットマップを取得しています。私は正しいことをしていますか他の方法もあります。

事前に感謝します....

+0

をあなたがnull以外のバイト配列を取得していますか?スニペットが役に立ちます。 –

+0

はい-128から127の範囲のバイト配列を取得しています – Haris

+0

画像がデコードできないようです。実際のイメージデータのために移動する必要があるオフセット(ヘッダーのため、おそらく)はありますか? –

答えて

8

私は私のプロジェクトの一つで、以下のようにそれを使用してきたし、これまでのところ、それはかなり固体となっています。私はそれがPNG形式で圧縮されていない限り、それがどれほど厄介であるかわからない。

byte[] bytesImage; 
Bitmap bmpOld; // Contains original Bitmap 
Bitmap bmpNew; 

ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); 
bmpOld.compress(Bitmap.CompressFormat.PNG, 100, baoStream); 
bytesImage = baoStream.toByteArray(); 
bmpNew = BitmapFactory.decodeByteArray(bytesImage, 0, bytesImage.length); 

編集:私はRGBを使用するthis postからコードを適応してきましたので、以下のコードは、あなたのために働く必要があります。私はまだそれをテストする機会がなかったので、調整が必要な場合があります。上記の回答にコメントから

Byte[] bytesImage = {0,1,2, 0,1,2, 0,1,2, 0,1,2}; 
int intByteCount = bytesImage.length; 
int[] intColors = new int[intByteCount/3]; 
int intWidth = 2; 
int intHeight = 2; 
final int intAlpha = 255; 
if ((intByteCount/3) != (intWidth * intHeight)) { 
    throw new ArrayStoreException(); 
} 
for (int intIndex = 0; intIndex < intByteCount - 2; intIndex = intIndex + 3) { 
    intColors[intIndex/3] = (intAlpha << 24) | (bytesImage[intIndex] << 16) | (bytesImage[intIndex + 1] << 8) | bytesImage[intIndex + 2]; 
} 
Bitmap bmpImage = Bitmap.createBitmap(intColors, intWidth, intHeight, Bitmap.Config.ARGB_8888); 
+0

こんにちは、ありがとうございます...実際に私のクライアントはRGBピクセル値を表すuchar配列を送信します。ヘッダーはありません。それは問題を引き起こしますか? – Haris

+0

おそらく手動で再作成する必要があります。あなたにはうまくいくはずのいくつかの例があります(http://stackoverflow.com/questions/1143293/how-to-convert-array-of-bytes-into-image-in-java-se)。 – Jon

+0

しかしImageIOがAndroid SDKでサポートされていないことがわかりました。http://stackoverflow.com/questions/5311163/how-to-load-bufferedimage-in-android – Haris

9

あなたはRGB値のストリームからではなく、PNGまたはJPEGなどの任意の画像フォーマットからBitmapオブジェクトを作成したいのように、それはそうです。

これはイメージサイズが既にわかっている可能性があります。このケースでは、このような何か行うことができます:

byte[] rgbData = ... // From your server 
int nrOfPixels = rgbData.length/3; // Three bytes per pixel. 
int pixels[] = new int[nrOfPixels]; 
for(int i = 0; i < nrOfPixels; i++) { 
    int r = data[3*i]; 
    int g = data[3*i + 1]; 
    int b = data[3*i + 2]; 
    pixels[i] = Color.rgb(r,g,b); 
} 
Bitmap bitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888); 
0
InputStream is = new java.net.URL(urldisplay).openStream(); 
byte[] colors = IOUtils.toByteArray(is); 
int nrOfPixels = colors.length/3; // Three bytes per pixel. 
int pixels[] = new int[nrOfPixels]; 
    for(int i = 0; i < nrOfPixels; i++) { 
     int r = (int)(0xFF & colors[3*i]); 
     int g = (int)(0xFF & colors[3*i+1]); 
     int b = (int)(0xFF & colors[3*i+2]); 
     pixels[i] = Color.rgb(r,g,b); 
} 
imageBitmap = Bitmap.createBitmap(pixels, width, height,Bitmap.Config.ARGB_4444); 
    bmImage.setImageBitmap(imageBitmap); 
関連する問題