Android byte array to Bitmap How to

后端 未结 3 1588
面向向阳花
面向向阳花 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条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 07:55

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

提交回复
热议问题