2

I'm trying to add opencv4 to my android app, and did everything as in this post.

I added System.loadLibrary to the begining of my kotlin file s below:

class MainActivity : AppCompatActivity() {
    companion object {
        init {
            System.loadLibrary("opencv_java4")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) { }

But the app exit after launching with the below:

09/22 16:08:04: Launching 'app' on Pixel 3a API 29.
$ adb shell am start -n "hasan.tts_mobile/hasan.tts_mobile.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Waiting for process to come online...
Timed out waiting for process to appear on Pixel_3a_API_29 [emulator-5554].

1 Answer 1

0

Thanks to this answer, it looks there is something changed in opencv 4 so that all existing tutorials talking about importing java folder as module, while actually the one required to be imported is the skd folder itself.

You can use JavaCV that is a wrapper of OpenCV, or do OpenCV manually as below:

1- From here download OpenCV – 4.1.1 for Android

2- Extract the folder, it will be extracted to OpenCV-android-sdk, that contains the following:

Hasans-Air:OpenCV-android-sdk hasan$ ls
LICENSE     README.android  samples     sdk

3- Head to your project at Android Studio

4- File -> New -> Import Module

5- Select the sdk folder in the extracted folder in point 2, as shown the proposed module name is :sdk but you can rename it like :sdkOpenCV4, do not forget the : in front of the name

enter image description here

6- Right click the app, select Open Module Settings, select Dependencies, then click the app module, in the Declared Dependencies tap click + then you'll see the OpenCV module there, click add it, click Apply then Add:

enter image description here

enter image description here

enter image description here

enter image description here

7- From OpenCV SDK copy the sdk/native/libs folder, and go to tour application folder src/main and paste it inside it, then rename the libs folder to jnilibs, so that in your application src/main folder you'll be having:

enter image description here

8- In the OnCreate you can add a check before start using it:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val ocvLoaded = OpenCVLoader.initDebug()
        if (ocvLoaded) {
            Toast.makeText(
                this@MainActivity, "OpenCV loaded",
                Toast.LENGTH_SHORT
            ).show()
        } else {
            Toast.makeText(
                this@MainActivity, "Unable to load OpenCV",
                Toast.LENGTH_SHORT
            ).show()
            Log.d("openCV", "loader: ${OpenCVLoader.initDebug()}")
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.