Converting preview frame to bitmap
as I found out the width and height read from PreviewSize were wrong....
In other words the conversion went wrong because it was based on false values.
After use a new way of setting the preview settings(from example deleivered by google) everything works fine.
EDIT:
Sorry for the quick answer above and long delay.
It was while ago and I can not dig the code now, but i think that I used:
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#getSupportedPreviewSizes()
and took the first element from the list and set the values with
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewSize(int, int)
i stored the width and height and used it to transform picture.
It dont know if it was the best solution, but it worked out.
Converting ImageProxy to Bitmap
You will need to check the image.format
to see if it is ImageFormat.YUV_420_888
. If so , then you can you use this extension to convert image to bitmap:
fun Image.toBitmap(): Bitmap {
val yBuffer = planes[0].buffer // Y
val vuBuffer = planes[2].buffer // VU
val ySize = yBuffer.remaining()
val vuSize = vuBuffer.remaining()
val nv21 = ByteArray(ySize + vuSize)
yBuffer.get(nv21, 0, ySize)
vuBuffer.get(nv21, ySize, vuSize)
val yuvImage = YuvImage(nv21, ImageFormat.NV21, this.width, this.height, null)
val out = ByteArrayOutputStream()
yuvImage.compressToJpeg(Rect(0, 0, yuvImage.width, yuvImage.height), 50, out)
val imageBytes = out.toByteArray()
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}
This works for a number of camera configurations. However, you might need to use a more advanced method that considers pixel strides.
Related Topics
How to Show Text in Android System Status Bar
Why Is My Field 'Null' After Injection? How to Inject My Object
Sliding Navigation Drawer Not Handling Clicks on Menu Items Android
How to Hide One Item in an Android Spinner
Manage Google Maps API Key with Gradle in Android Studio
How to Extract the File Name from Uri Returned from Intent.Action_Get_Content
Android Mediaplayer/Videoview Error (1, -2147483648)
Converting Preview Frame to Bitmap
Is Deprecated Word the Only Difference Between Fill_Parent and Match_Parent
Android Spinner Error:Android.View.Windowmanager$Badtokenexception: Unable to Add Window
Implement Double Click for Button in Android