1

I used A Beginner’s Guide to Setting up OpenCV Android Library on Android Studio as a guide to setup OpenCV in an Android Studio project. I have got the project to build and install on my phone but I am not getting a video feed from the camera (that is the JavaCameraView is empty).

The main issues seems to be that the OpenCV Manager has been pulled from the google play store. :,(

I have tried the solution from the this SO question to try to use opencv without needing to get it off the play store, but that didn't help either.

I have all the required permissions to use use the camera on my app.

My code is pretty similar to the one from the Beginner’s Guide tutorial, but let me know if you need to see any more of my code.

My onResume:

public override fun onResume() {
    super.onResume()
    if (!OpenCVLoader.initDebug()) {
        OpenCVLoader.initDebug()
    }
    loaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS)
}

BaseLoadCallback:

private val loaderCallback = object : BaseLoaderCallback(this) {
    override fun onManagerConnected(status: Int) {
        when (status) {
            LoaderCallbackInterface.SUCCESS -> {
                Log.i(TAG, "OpenCV loaded successfully")
                opencv_camera_view.enableView()
                opencv_camera_view.setOnTouchListener(this@MainActivity)
            }
            else -> {
                super.onManagerConnected(status)
            }
        }
    }
}
DrkStr
  • 1,752
  • 5
  • 38
  • 90
  • Which version of OpenCv are you using? – vSomers Aug 13 '19 at 15:45
  • I am using version 4.1.1 of the Android pack from here: https://opencv.org/releases/ – DrkStr Aug 14 '19 at 01:58
  • I also had troubles setting up OpenCv 4.1.1 Android release with instructions working on 4.1.0 version. Did you manage to make it work for 4.1.0? – vSomers Aug 20 '19 at 16:45
  • Yup, I have added the steps I took to make it work as an answer below. Good Luck :) – DrkStr Aug 21 '19 at 05:57
  • @vSomers can you also look at my question maybe you could help [question](https://stackoverflow.com/questions/65362431/opencv-4-5-0-empty-javacameraview-and-javacamera2view). – lazydevpro Dec 18 '20 at 18:54

2 Answers2

7

These are the steps that worked for me.

  1. Go to the OpenCV releases page and click on Android pack link of the current version section (https://opencv.org/releases.html). I used 4.1.1
  2. Unzip the file named opencv--android-sdk
  3. In Android Studio, go to File > New > Import Module... and the choose /sdk and wait for the sync to finish.
  4. Include open-cv as a module dependency. File -> Project Structure.
  5. Select the project module(not the open-cv module we added in step 3)
  6. Add the opencv as a module dependency.
  7. Check in the build.gradle file is it has been added as a dependency.
  8. Clean and build the project.

NOTEs

  • Do not import /sdk/java like most tutorials suggest.
  • You do not have to copy the native libraries into your project like most tutorials suggest.

Once you have OpenCV imported, use this tutorial to get the preview to fill the preview window.

And then use this version of the JavaCamera2View. JavaCamera2View uses the camera2 api.

These are the steps that worked for me, happy to be corrected if I have done something wrong.

Good Luck :)

DrkStr
  • 1,752
  • 5
  • 38
  • 90
  • I exactly did that and `OpenCVLoader.initDebug();` showed me success. But in JavaCameraView no image is loading. Also the `System.loadLibrary("native-lib");` is showing couldn't find "libnative-lib.so". can you help please? – lazydevpro Dec 16 '20 at 15:52
  • You need to use the camera2 api, I have linked to a version of the JavaCamera2View class that worked for me. Let me know if that workers for you. – DrkStr Dec 17 '20 at 08:41
  • No luck. can you share your code or see mine? btw I am using 4.5.0. I'll try on 4.1.1 – lazydevpro Dec 17 '20 at 17:36
  • Happy to look at your code, but I don't think I will be able to help as I haven't worked on Android or Java in over a year now. I'm using Flutter for all my front end needs these days. – DrkStr Dec 18 '20 at 10:29
  • I have added the question on the stack, please check and suggest something. [Question](https://stackoverflow.com/questions/65362431/opencv-4-5-0-empty-javacameraview-and-javacamera2view) – lazydevpro Dec 18 '20 at 18:53
1

I use this method to set opencv 4.1.0. This should work too for 4.1.1.

  1. Download opencv and unzip it
  2. From android studio, file -> new module -> gradle project and select the sdk directory which contain build.gradle file
  3. Add it is as dependency module for our "app" module
  4. On fragment/activity that call opencv api, add

if (!OpenCVLoader.initDebug()) {
    OpenCVLoader.initDebug()
}

on either onResume or onCreateView lifecycle hook(the sample use onResume but I havent encounter any problem using it on onCreateView)

Update 1. How to check if opencv installed correctly

  1. After unzip the library, we have two folder, sdk and samples.
  2. Go to samples folder and copy its .java and .xml file from color-blob-detection(This sample implement JavaCameraView) folder to our blank project. I think this faster than fully import the sample.
  3. Dont forget to copy the manifest file too. It add camera permision. The sample dont implement runtime permission so just toggle it manually from settings -> application -> permissions for now.

note : Read the build.gradle file for more setup config. eg. splitting apk, ndk support etc.

Community
  • 1
  • 1
nfl-x
  • 487
  • 1
  • 6
  • 13
  • Hi, that is pretty much what I have done. Can you think of anything that I could have missed? – DrkStr Aug 13 '19 at 14:44
  • Maybe try to do simple thing like grayscaling and show it to imageview just to be sure opencv has been setup correctly – nfl-x Aug 13 '19 at 14:49
  • I've never user OpenCV on android before, do you have a link to a tutorial which shows how to do grayscaling using OpenCV in Android? – DrkStr Aug 13 '19 at 14:59