9

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....

5
  • Are you sure you're getting a non-null byte array? Snippets may help. Commented Sep 5, 2013 at 7:23
  • Yes I am getting byte array which is ranging from -128 to 127 Commented Sep 5, 2013 at 7:25
  • It looks like your image cannot be decoded. Is there an offset (because of headers, most likely) you need to go to for actual image data? Commented Sep 5, 2013 at 7:28
  • Actually I am sending image from OpenCV which is array of uchar representing RGB pixel value, there is no header, only pixel value. Commented Sep 5, 2013 at 7:32
  • So the receiving image should be bitmap ? Commented Sep 5, 2013 at 7:36

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

Hi thanks for the answer...Actually my client sending uchar array representing RGB pixel value. There is no header. Does that cause problem
You'll probably need to recreate it manually. There are a few examples here that should work for you though.
But I found that ImageIO is not supported in Android SDK here stackoverflow.com/questions/5311163/…
Bitmap.createBitmap() 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.
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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.