Android byte array to Bitmap How to

后端 未结 3 1580
面向向阳花
面向向阳花 2020-12-19 07:40

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 receivin

3条回答
  •  萌比男神i
    2020-12-19 08:07

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

提交回复
热议问题