I'm following this tutorial but with Android Studio. I have done the following steps:
Creating a new project in Android Studio
Adding the OpenCV-2.4.5-sdk/sdk/java as a module
Right clicked on my main module->Change Module settings-> added the above opencv module as a dependency
For my MainActivity I used the following code (stripped down from one of the samples):
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
public class MainActivity extends Activity implements CvCameraViewListener {
private CameraBridgeViewBase mOpenCvCameraView;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i("Yay", "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
@Override
public void onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}
@Override
public void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
}
@Override
public Mat onCameraFrame(Mat inputFrame) {
return null;
}
}
Android Studio's text editor could identify the packages I have imported (like org.opencv.android) but when I compiled I got these errors:
Gradle: error: package org.opencv.android does not exist
Gradle: error: package org.opencv.android does not exist
Gradle: error: package org.opencv.android.CameraBridgeViewBase does not exist
Does anyone know why this happens?
The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. The dependencies can be located on your machine or in a remote repository, and any transitive dependencies they declare are automatically included as well.
Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.
Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.
Simply open the gradle tab (can be located on the right) and right-click on the parent in the list (should be called "Android"), then select "Refresh dependencies".
I was having a similar issue with my Android Studio on OS X. I write the code, and Android Studio would see the .jar's I was referencing, and had declared dependencies on, but when it came time for Gradle to build the project, no go.
I opened up the 'build.gradle' file, and had to manually add dependencies. For example, here is my complete file:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/gson-2.2.4.jar')
compile files('libs/jsoup-1.7.2.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 16
}
}
As you can guess, the lines I added are the ones referencing Gson and Jsoup.
Inspired by this answer, these are the steps I did:
compile files('libs/opencv-library-2.4.5.jar')
Now, the project should be built and run just fine.
Note: In later versions of OpenCV (e.g 2.4.7), the library can be found in ...\build\java
.
I ran into a similar problem.
In my case, it was Android Studio failed to add the Gradle dependency for me. I had to add it myself in my project's gradle file:
compile project(":openCVLibrary2410")
It solved the problem. I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With