OpenCV in android

 Integrating into the Android project

  1. Download the OpenCV from the official site for android. In my case, the latest is 4.5.5 
  2. Now in the android studio, Open File > New > Import Module navigate to the SDK of OpenCV  where the build.gradle file exist in my case D:\softwares\opencv-4.5.5-android-sdk\OpenCV-android-sdk\sdk
  3. Now give the name to the imported module.

  4. That's it OpenCV integrated successfully to the android project.
  5. To test whether the Opencv integrated successfully or not use the log cat. 
    Log.d("TAG", "onCreate: "+ OpenCVLoader.initDebug());

Solving Errors in OpenCV

Blank Screen Problem

  • make sure the camera permissions are requested in the manifest.
  • and after runtime permission request we need to notify that the camera permission was granted with the method cameraBridgeViewBase.setCameraPermissionGranted();

Orientation Problem

I know it's to late to answer this question .But i finally found the solution which will not make camera frame slow.
You have to make changes in default opencv class . Follow this steps: 1) In CameraBridgeViewBase class add following code

Matrix matrix = new Matrix();
matrix.setRotate(90f);
Bitmap bitmap = Bitmap.createBitmap(mCacheBitmap, 0, 0, mCacheBitmap.getWidth(), mCacheBitmap.getHeight(), matrix, true);

2) now in drawbitmap method replace above bitmap with mCacheBitmap , as like below

if (mScale != 0) {
            canvas.drawBitmap(bitmap, new Rect(0,0,bitmap.getWidth(), bitmap.getHeight()),
                    new Rect((int)((canvas.getWidth() - mScale*bitmap.getWidth()) / 2),
                            (int)((canvas.getHeight() - mScale*bitmap.getHeight()) / 2),
                            (int)((canvas.getWidth() - mScale*bitmap.getWidth()) / 2 + mScale*bitmap.getWidth()),
                            (int)((canvas.getHeight() - mScale*bitmap.getHeight()) / 2 + mScale*bitmap.getHeight())), null);
        } else {
 canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
                    new Rect((canvas.getWidth() - bitmap.getWidth()) / 2,
                            (canvas.getHeight() - bitmap.getHeight()) / 2,
                            (canvas.getWidth() - bitmap.getWidth()) / 2 + bitmap.getWidth(),
                            (canvas.getHeight() - bitmap.getHeight()) / 2 + bitmap.getHeight()), null);

}

3) now , In your JavaCameraView class replace following code in initializeCamera method (changing height ,width for Portrait mode)

if ((getLayoutParams().width == ActionBar.LayoutParams.MATCH_PARENT) && (getLayoutParams().height == ActionBar.LayoutParams.MATCH_PARENT))
                mScale = Math.min(((float)height)/mFrameWidth, ((float)width)/mFrameHeight); 
            else
                mScale = 0;

and you are done !! Hope it helped..

Post a Comment

0 Comments