I am using OpenCV4Android version 2.4.10 and i test my code on Samsung Galayx GT-I9300. the problem i have is, that i must download Opencv Manager from play store so that my
Share to you about my way.
--
def opencvsdk = '<path_to_opencv_android_sdk_rootdir>'
include ':opencv'
project(':opencv').projectDir = new File(opencvsdk + '/sdk')
Example with my settings.gradle file
include ':app'
def opencvsdk = '/Volumes/Data/Projects/machine-learning/OpenCV-android-sdk'
include ':opencv'
project(':opencv').projectDir = new File(opencvsdk + '/sdk')
--
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
....
implementation project(':opencv')
}
--
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
static {
OpenCVLoader.initDebug();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.activity_main_iv_result);
try {
Mat img = Utils.loadResource(this, R.drawable.eye);
Mat gryimg = new Mat(img.size(),CvType.CV_8U);
Imgproc.cvtColor(img, gryimg, Imgproc.COLOR_RGB2GRAY);
Bitmap bm = Bitmap.createBitmap(gryimg.cols(), gryimg.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(gryimg, bm);
imageView.setImageBitmap(bm);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Tested with opencv-3.4.1-android-sdk and Android Studio 3.1.3
We have some way to integrate sdk to your project. You should read /sdk/build.gradle for getting more options
The correct way to load the native library is:
System.loadLibrary("opencv_java3");
As you want step by step procedure, So I would start with creating a SampleOpenCV
project from scratch, and would also suggest to do the following steps in a new project, Once it starts working you may try to migrate the changes to your main project.
SampleOpenCV
project in Android Studio, then File -> New -> Import Module, which would open a new pop-up to enter the module path, select {unzipped_opencv}/sdk/java
, this would create a OpenCVLibrary310
directory under SampleOpenCV
.SampleOpenCV/OpenCVLibrary310/build.gradle
and copy the following fields from SampleOpenCV/app/build.gradle
:
SampleOpenCV
project and click Open Module Settings, look for Modules >> app
and select DependenciesClick at the top-right + sign, in the pop window, and select 3 Module Dependency. Now choose OpencvLibrary310
. Close the pop up and let the gradle sync.
Copy libs folder {unzipped_opencv}/sdk/native/libs to Android Studio under app/src/main and rename from it to jniLibs
(Mind the case here).
public class MainActivity extends AppCompatActivity {
static {
OpenCVLoader.initDebug();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Mat m = new Mat(100, 100, CvType.CV_8UC4);
}
}
NOTE: OpenCVLoader.initDebug()
must be used for debugging purposes only as when you are developing locally on your machine. But for production purposes where you need to release the app on Play Store, etc. you must use OpenCVLoader.initAsync()
. Actually initializing the OpenCVLoader takes some time depending upon the phone. So if you load it uisng initDebug()
, then it would be executed in the main thread, which may block the UI for a fractional time. So it is advised to load the OpenCV in background which can be achieved using initAsync()
UPDATED ANSWER
If you are done with all the steps and getting java.lang.UnsatisfiedLinkError
, possibly you are missing jniLibs
or you haven't implemented step 6 properly.
Add this code in your app level graddle:
andriod{
sourceSets.main {
jniLibs.srcDirs = ['libs']
}
}
After graddle sync jniLibs will show up like this