An exercise in frustration

Hi guys,
I have not been so frustrated in long time. After a full day, I am still unable to compile an hello world in Android.

OpenCV for android 4.7.0 as downloaded
Android Studio Flamingo | 2022.2.1
Installed multiple SDK version, SDK built tools, NDK, command line tools, platform tools + some irrelevant extras.
Successfully tested a simple camera capture program using only android (not opencv).

Followed the tutorial:

However, I am unable to get it compiled:
error: package org.opencv.engine does not exist
import org.opencv.engine.OpenCVEngineInterface;

I found hundred of “solutions” on the internet. NONE of them works.
Ended up copying all the opencv java into the project,
i have the whole org/opencv/ tree in main, i have added the jniLibs folder, fixed version inconsistencies in gradle files etc…

Still, nothing works.
I was assuming that maybe I need to compile the opencv sdk.
Attempting to import the opencv/sdk in Android Studio to see if it compile. It does not:

A problem occurred evaluating root project ‘sdk’.

Plugin with id ‘com.android.library’ not found.

Questions:

  • The android packet is ready to go or need compiled ?
  • How to get OpenCV to compile in Android Studio, if this is the problem?
  • If no, any hints to alleviate a nerve-racking experience ?

Tnx.

1 Like

You could have a try to add “android.defaults.buildfeatures.aidl=true” to the gradle.properties. to enable the aidl buildfeature.
ref: BuildFeatures  |  Android Developers

1 Like

Thank you very much Henry.
That is a very good piece of info. It fixedthat point,

Now, I advanced to the next frustration :grin:

error: cannot find symbol import org.opencv.BuildConfig;

No advice found by Google helped so far either.

Just in case somebody else have the org.opencv.BuildConfig; issue, I was able to pass that by adding:

buildFeatures {
    buildConfig true
}

in build.gradle for opencv.

Android is so frustrating … :frowning:

1 Like

recent updates, please take a look:

thank you so much. This saved me after 3 hours of searching all over.

if somebody still needs help

that was a life saver holy hell thank you guys

Thank you @ankit_sachan for documenting this. I followed the documentation and the build went smoothly. But when I try to run the app, I get an error that “OpenCV Manager” package not installed and it redirects me to Google Playstore. Any idea what needs to be done to tackle this?

OK, just after I posted this, I happened to look at the OpenCV build.gradle file that had this comment:

// Load OpenCV native library before using:
//
// - avoid using of "OpenCVLoader.initAsync()" approach - it is deprecated
//   It may load library with different version (from OpenCV Android Manager, which is installed separatelly on device)
//
// - use "System.loadLibrary("opencv_java4")" or "OpenCVLoader.initDebug()"

So, now, I just have:

    static {
        System.loadLibrary("opencv_java4");
    }

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentFirstBinding.inflate(inflater, container, false);
        Log.d(TAG, "OpenCV loaded: "+ OpenCVLoader.initDebug());
        if (OpenCVLoader.initDebug()) {
            binding.mainCamera.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_BACK);
            binding.mainCamera.setCameraPermissionGranted();
            binding.mainCamera.enableView();
        }
        return binding.getRoot();

    }

And the preview pane opened :slight_smile:

Only issue I now have is that the preview pane is in landscape mode. Any idea if I can get it to portrait mode?

The code that I have used for portrait view:

        @Override
        public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

            Imgproc.cvtColor(inputFrame.rgba(), rgb, Imgproc.COLOR_RGBA2RGB);
             /*
             --- Your logic
                // Do your processing before rotating the image so that the corner/pose estimations are not affected
             */
             Mat localRGB;
             // Ref: https://stackoverflow.com/a/28601634
            Core.rotate(rgb, localRGB, Core.ROTATE_90_CLOCKWISE);
            //ref: https://stackoverflow.com/a/37972813
            Imgproc.resize(localRGB, rgb, rgb.size());
            return rgb;
        }