-2

I am trying to initialize Mat and this is my code

    Mat imgRgba = new Mat();
    final Bitmap bitmap =
    Bitmap.createBitmap(imgRgba.width(), imgRgba.height(), Bitmap.Config.RGB_565);
    Utils.matToBitmap(imgRgba, bitmap);

I have tried this too

Bitmap.createBitmap(imgRgba.cols(), imgRgba.rows(), Bitmap.Config.RGB_565);

but when I am going to run my app I am getting crash and exception is this

    Stack trace:  
    java.lang.IllegalArgumentException: width and height must be > 0
    at android.graphics.Bitmap.createBitmap(Bitmap.java:969)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:948)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:915)
    at com.jmr.agency.banking.ui.facerecognition.FRAddPersonPreviewActivity.onCameraFrame(FRAddPersonPreviewActivity.java:156)
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43

2 Answers2

0

Try either mat.create(int rows, int cols, int type) or some other constructor for Mat object, as currently it does not have a size.

isaaaaame
  • 460
  • 4
  • 9
  • Can You Initialize this Mat because I have tried all way? – Pooja Patwa Dec 30 '19 at 08:08
  • and one more thing , am I getting this exception because Mat is not initialized properly? – Pooja Patwa Dec 30 '19 at 08:09
  • Yes. You can open any of the links I added in my comment, and there you'll have the full explanation of 1.) how to call mat.create() + example of calling it; and 2.) how to use different constructors – isaaaaame Dec 30 '19 at 08:14
0

You did new instance Mat for size. But it has no size.If you have bitmap or Mat data you can use your code like this:

 private Bitmap getBitmap(Mat imgRgb){
    final Bitmap bitmap =
    Bitmap.createBitmap(imgRgba.width(), imgRgba.height(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(imgRgba, bitmap);
    return bitmap;
}

Now you can call this method with your existing Mat data for convert your Mat data to bitmap.

Or you can convert your Bitmap to Mat:

private Bitmap getMat(Bitmap bitmap){
        Mat mat = new Mat();
        mat = Utils.bitmapToMat(bitmap,mat);
        return mat;
}

Hope you understand.

Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43