8

How can I convert byte array received using socket.

  1. The C++ client send image data which is of type uchar.

  2. At the android side I am receiving this uchar array as byte[] which is ranges from -128 to +127.

What I wanted to do is that receives this data and display it. For that I was trying to convert to Bitmap using BitmapFactory.decodeByteArray(), but no luck I am getting null Bitmap. Am I doing right or any other method available.

Thanks in advance....

Haris
  • 13,645
  • 12
  • 90
  • 121

3 Answers3

10

From the comments to the answers above, it seems like you want to create a Bitmap object from a stream of RGB values, not from any image format like PNG or JPEG.

This probably means that you know the image size already. In this case, you could do something like this:

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);
Albin
  • 4,180
  • 2
  • 27
  • 19
8

I've been using it like below in one of my projects and so far it's been pretty solid. I'm not sure how picky it is as far as it not being compressed as a PNG though.

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

edit: I've adapted the code from this post to use RGB, so the code below should work for you. I haven't had a chance to test it yet so it may need some adjusting.

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);
Community
  • 1
  • 1
Jon
  • 1,398
  • 9
  • 14
  • Hi thanks for the answer...Actually my client sending uchar array representing RGB pixel value. There is no header. Does that cause problem – Haris Sep 05 '13 at 07:55
  • You'll probably need to recreate it manually. There are a few [examples here](http://stackoverflow.com/questions/1143293/how-to-convert-array-of-bytes-into-image-in-java-se) that should work for you though. – Jon Sep 05 '13 at 08:00
  • But I found that ImageIO is not supported in Android SDK here http://stackoverflow.com/questions/5311163/how-to-load-bufferedimage-in-android – Haris Sep 05 '13 at 08:04
  • [Bitmap.createBitmap()](https://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap%28int[],%20int,%20int,%20android.graphics.Bitmap.Config%29) sounds like what you need then. I think android stores bitmaps as byte arrays internally so I'm sure there has to be some easy way to convert it. – Jon Sep 05 '13 at 08:17
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 );