Android: Convert image object to bitmap does not work
You haven't copied bytes.You checked capacity but not copied bytes.
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);
Android Media Image to Bitmap Conversion for Tensorflow Lite
The null return value suggests that the image data could not be decoded when you call BitmapFactory.decodeByteArray
. You may check your input contents first.
Here is a link to another similar question about how to convert android.media.image to Bitmap : How to convert android.media.Image to bitmap object?
If it doesn't work, you may also try to check and handle Java error, where this question may be helpful How do I keep track of Java Exceptions
converting drawable resource image into bitmap
You probably mean Notification.Builder.setLargeIcon(Bitmap)
, right? :)
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
notBuilder.setLargeIcon(largeIcon);
This is a great method of converting resource images into Android Bitmap
s.
converting image to bitmap in android
use this code
Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
How to create Bitmap from Android MediaImage in OUTPUT_IMAGE_FORMAT_RGBA_8888 format?
I actually found a solution myself, so I post it here if anyone is interested:
private Bitmap toBitmap(Image image) {
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * image.getWidth();
Bitmap bitmap = Bitmap.createBitmap(image.getWidth()+rowPadding/pixelStride,
image.getHeight(), Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
return bitmap;
}
Convert android.media.Image (YUV_420_888) to Bitmap
I write some code about this, and it's the YUV datas preview and chang it to JPEG datas ,and I can use it to save as bitmap ,byte[] ,or others.(You can see the class "Allocation" ).
And SDK document says:
"For efficient YUV processing with android.renderscript: Create a RenderScript Allocation with a supported YUV type, the IO_INPUT flag, and one of the sizes returned by getOutputSizes(Allocation.class), Then obtain the Surface with getSurface()."
here is the code, hope it will help you:https://github.com/pinguo-yuyidong/Camera2/blob/master/camera2/src/main/rs/yuv2rgb.rs
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
Android Fastboot Waiting for Devices
How to Disable Preveious Date in a Date Picker
How to Set Transparency of a Background Image Android Xml File
Sending Files Using Post With Httpurlconnection
Android Studio - How to Find Valid Certification Path to Requested Target
How to Use Adb With Emulator Like Memu/Bluestacks/Nox App Player
Recyclerview Indicates That Reaches the Last Item
Charging Animation for Android
Passing an Arraylist of Objects to the New Activity
Android Studio 0.4.3 - Task 'Assemble' Not Found in Root Project
How to Set Align (Right or Left) of Text in Textview, Programmatically
How to Refresh a Previous Activity After the Back Button Is Pressed
How to Delete or Remove Cardview from Recyclerview - Android Studio
Android Adb Doesn't See Device
Recyclerview Slow Scrolling Performance and Lag/Halt Sometime While Scrolling
Android:Get Current Date and Time from Firebase
How to Reduce an Image File Size Before Uploading to a Server
Httpmessagenotreadableexception: Could Not Read Json: Unrecognized Field Using Spring for Android