Hello,
Is there any easier way to do this, or any utility classes that can be used for this?
I cannot find eaisier way to do this, I search for Android.Util
api, I cannot find related api to do this.
You can create method and convert image to bitmap like following code.
public Bitmap ConvertImageToBitMap(Android.Media.Image image)
{
ByteBuffer buffer = image.GetPlanes()[0].Buffer;
byte[] bytes = new byte[buffer.Capacity()];
buffer.Get(bytes);
Bitmap bitmapImage = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length, null);
return bitmapImage;
}
========Update=====
Please check the image's format to see if it is ImageFormat.YUV_420_888, if so, please use following code to transfer it.
public Bitmap ConvertImageToBitMap(Android.Media.Image image)
{
ByteBuffer yBuffer = image.GetPlanes()[0].Buffer; //y
ByteBuffer vuBuffer = image.GetPlanes()[2].Buffer; //vu
int ySize = yBuffer.Remaining();
int vuSize = vuBuffer.Remaining();
byte[] nv21 = new byte[ySize + vuSize];
yBuffer.Get(nv21, 0, ySize);
vuBuffer.Get(nv21, ySize, vuSize);
YuvImage yuvImage = new YuvImage(nv21, ImageFormatType.Nv21, image.Width, image.Height, null);
System.IO.MemoryStream outstream = new System.IO.MemoryStream();
yuvImage.CompressToJpeg(new Rect(0, 0, yuvImage.Width, yuvImage.Height), 50, outstream);
byte[] imagebytes = outstream.ToArray();
Bitmap bitmapImage = BitmapFactory.DecodeByteArray(imagebytes, 0, imagebytes.Length, null);
return bitmapImage;
}
Best Regards, Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.