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.
Convert CameraX Captured ImageProxy to Bitmap
So the solution was to add extension method to Image
and here is the code
class ImagePickerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_image_picker)
}
private fun startCamera() {
val imageCapture = ImageCapture(imageCaptureConfig)
capture_button.setOnClickListener {
imageCapture.takePicture(object : ImageCapture.OnImageCapturedListener() {
override fun onCaptureSuccess(image: ImageProxy?, rotationDegrees: Int) {
imageView.setImageBitmap(image.image?.toBitmap())
}
//.....
})
}
}
}
fun Image.toBitmap(): Bitmap {
val buffer = planes[0].buffer
buffer.rewind()
val bytes = ByteArray(buffer.capacity())
buffer.get(bytes)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
}
Converting a captured image to a Bitmap
This code worked for me, try it.
private Bitmap getBitmap(ImageProxy image) {
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
buffer.rewind();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
byte[] clonedBytes = bytes.clone();
return BitmapFactory.decodeByteArray(clonedBytes, 0, clonedBytes.length);
}
Convert ImageProxy to Jpeg or Png
I have solved the issue by using the below code
private void takePicture() {
if (imageCapture != null) {
ByteArrayOutputStream result = new ByteArrayOutputStream();
getCacheDir();
imageCapture.takePicture(new ImageCapture.OutputFileOptions.Builder(result).build(),
Executors.newSingleThreadExecutor(),
new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
File cache = new File(getCacheDir().getAbsolutePath() + "/" + "captured.png");
try (FileOutputStream stream = new FileOutputStream(cache)) {
stream.write(result.toByteArray());
upload(cache); // your upload function
} catch (Exception e) {
Log.e(TAG, "onImageSaved: Exception occurred", e);
}
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
Log.i(TAG, "onError: ", exception);
}
});
}
}
thanks to @Commonsware
Related Topics
Android: Create Circular Image with Picasso
What Is the Default Font Family in Android
Limit Height of Listview on Android
Command Line "Android Update Sdk" on Headless Linux
Android Logcat Logs Chatty Module Line Expire Message
Setexactandallowwhileidle - Is Not Exact as of Developer Reference
Official "Boost Library" Support for Android and iOS
Display Huge Images in Android
Android Navigation Architecture Component - Get Current Visible Fragment
How to Merge Call Programmatically While Other Call Is Running (Conference Call)
Calculate the Size of a List View or How to Tell It to Fully Expand
Create Custom Big Notifications
Generate Barcode Image in Android Application
The Item You Requested Is Not Available for Purchase
Mediaplayer Error (1, -1004) Aka Media_Error_Io Trying to Stream Music on Samsung S3