Not able to convert byte[] to Bitmap in onPreviewFrame() callback method - android

I am not able to convert a byte array to a Bitmap in the preview callback method. I'm using the following code:
public void onPreviewFrame(byte[] data, Camera camera) {
Bitmap bmp = BitmapFactory.decodeByteArray(data , 0, data.length);
Mat orig = new Mat(bmp.getHeight(),bmp.getWidth(),CvType.CV_8UC3);
Bitmap myBitmap32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, orig);
}
bmp is null. Please justify how to convert bitmap image.

Related

Efficient way of converting InputImage to Bitmap

I have to convert com.google.mlkit.vision.common.InputImage to equivalent Bitmap image in android using Java. Right now I am using the following code.
// iImage is an object of InputImage
Bitmap bmap = Bitmap.createBitmap(iImage.getWidth(), iImage.getHeight(), Bitmap.Config.RGB_565);
bmap.copyPixelsFromBuffer(iImage.getByteBuffer());
The above code is NOT converting the InputImage to Bitmap. Can anyone please suggest me the efficient way of converting InputImage to Bitmap.
You can create bitmap from byteBuffer, that can be received by call the method getByteBuffer().In the offical quick start example of ML Kit Vission you can find the vay how to achieve this. Below is a piece of code that can solve your problem:
Method getBitmap() thats converts NV21 format byte buffer to bitmap:
#Nullable
public static Bitmap getBitmap(ByteBuffer data, FrameMetadata metadata) {
data.rewind();
byte[] imageInBuffer = new byte[data.limit()];
data.get(imageInBuffer, 0, imageInBuffer.length);
try {
YuvImage image =
new YuvImage(
imageInBuffer, ImageFormat.NV21, metadata.getWidth(), metadata.getHeight(), null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0, metadata.getWidth(), metadata.getHeight()), 80, stream);
Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
stream.close();
return rotateBitmap(bmp, metadata.getRotation(), false, false);
} catch (Exception e) {
Log.e("VisionProcessorBase", "Error: " + e.getMessage());
}
return null;
}
Method rotateBitmap():
private static Bitmap rotateBitmap(
Bitmap bitmap, int rotationDegrees, boolean flipX, boolean flipY) {
Matrix matrix = new Matrix();
// Rotate the image back to straight.
matrix.postRotate(rotationDegrees);
// Mirror the image along the X or Y axis.
matrix.postScale(flipX ? -1.0f : 1.0f, flipY ? -1.0f : 1.0f);
Bitmap rotatedBitmap =
Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
// Recycle the old bitmap if it has changed.
if (rotatedBitmap != bitmap) {
bitmap.recycle();
}
return rotatedBitmap;
}
The full code can be seen by clicking on the link: https://github.com/googlesamples/mlkit/blob/master/android/vision-quickstart/app/src/main/java/com/google/mlkit/vision/demo/BitmapUtils.java

Android Opencv exception on converting MAT to bitmap after processing

My goal is to take bytearray at onPreviewFrame convert it into openCv MAT for some processing and later display processed frame in imageview.
My implementation of onPreviewFrame is as follows:
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
Mat cvMat=Byte_to_Mat(data);
Imgproc.cvtColor(cvMat, cvMat, Imgproc.COLOR_YUV2BGR_NV12, 4);
Imgproc.cvtColor(cvMat, cvMat, Imgproc.COLOR_BGR2GRAY, 4);
Bitmap bm = Bitmap.createBitmap(cvMat.cols(), cvMat.rows(), Bitmap.Config.ARGB_8888);
public Mat Byte_to_Mat(byte[] data) {
Mat jpegData = new Mat(1, data.length, CvType.CV_8UC1);
jpegData.put(0, 0, data);
Mat bgrMat = new Mat();
bgrMat = Imgcodecs.imdecode(jpegData, Imgcodecs.IMREAD_COLOR);
return bgrMat;
}
However Bitmap.createBitmap throws following exception
java.lang.IllegalArgumentException: width and height must be > 0

Why does BitmapFactory.decodeByteArray not work with camera previews?

So I'm trying to use face detection with a camera, so I need to convert the byte array supplied to the preview callback, into a bitmap, using this code:
Camera.PreviewCallback previewCallback=new Camera.PreviewCallback()
{
#Override
public void onPreviewFrame(byte[] data, Camera camera)
{
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
if(mBitmap==null) faceDetected=false;
else faceDetected=(findFace(mBitmap)!=null);
}
};
Unfortunately, mBitmap is always null, and options outHeight and outWidth are always -1 - which indicates a decode error.
Naturally, there are no diagnostics, so it's impossible to fix.

Getting Out of Memory Error onPictureTaken BitmapFactory.decodeByteArray

I am getting a weird OOM error on decoding the bytearray I get when taking a picture. My code is as below. Please tell if there is a better way to do it.
#Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO something with the image data
// Restart the preview and re-enable the shutter button so that we can take another picture
camera.startPreview();
inPreview = true;
shutterButton.setEnabled(true);
System.gc();
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, false);
if(bitmap != null){
boolean fileSaved = Funcs.saveImage(getActivity(),resizedBitmap, "moizali");
if(fileSaved){
((BaseActivity)getActivity()).goToPhotoEditActivity("moizali");
}
}
}

Bytearray to string android

I am trying to create a camera intent and a sub portion of the code is given below.
public void onPictureTaken(byte[] data, Camera camera) {
String dat = new String(data);
byte[] datas = dat.getBytes();
preview.setVisibility(View.GONE);
ImageView iv2 = (ImageView)findViewById(R.id.iv1);
Bitmap bMap = BitmapFactory.decodeByteArray(datas, 0, datas.length);
iv2.setImageBitmap(bMap);
}
This keeps the imageview blank, however when I give
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
the imageview is properly loaded. Am I doing any mistake in byte array to string conversion??
why do you need these two lines?
String dat = new String(data);
byte[] datas = dat.getBytes();
use data directly in the decodeByteArray
You dont even need to convert the byte[] to String. Just use it as it is.
public void onPictureTaken(byte[] data, Camera camera) {
preview.setVisibility(View.GONE);
ImageView iv2 = (ImageView)findViewById(R.id.iv1);
// ensure ImageView is visible.
iv2.setVisibility( View.VISIBLE);
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
iv2.setImageBitmap(bMap);
}
Just use the above modified code.

Categories

Resources