OpenCV:
*Instructions
Tested with A.S. v1.4.1 but should work with newer versions too.
- Call it "**cvtest1**"
- Form factor: **API 19, Android 4.4 (KitKat)**
- **Blank Activity** named **MainActivity**
You should have a *cvtest1* directory where this project is stored. (the title bar of Android studio shows you where cvtest1 is when you open the project)
**Menu:/File/New/Import_Module**:
- Source-directory: **{unzip-dir}/sdk/java**
- Module name: Android studio automatically fills in this field with **openCVLibrary310** (the exact name probably doesn't matter but we'll go with this).
- Click on **next**. You get a screen with three checkboxes and questions about jars, libraries and import options. All three should be checked. Click on **Finish.**
Android Studio starts to import the module and you are shown an **import-summary.txt** file that has a list of what was not imported (mostly javadoc files) and other pieces of information.
But you also get an error message saying **failed to find target with hash string 'android-14'...**. This happens because the build.gradle file in the OpenCV zip file you downloaded says to compile using android API version 14,
which by default you don't have with Android Studio v1.4.1.
- compileSDKVersion (mine says 23)
- buildToolsVersion (mine says 23.0.2)
- minSdkVersion (mine says 19)
- targetSdkVersion (mine says 23)
Use some other tool, such as any file manager, and go to this directory. You can also switch the project view from Android to Project Files and you can find this directory as shown in this screenshot:
Inside there is another build.gradle file (it’s highlighted in the above screenshot). Update this file with the four values from step 6.
and you should see many references to **:openCVLibrary310** in the **0:Messages** screen.
[![enter image description here][5]][5]
At this point the module should appear in the project hierarchy as **openCVLibrary310**, just like **app**. (Note that in that little drop-down menu I switched back from **Project View** to **Android View** ). You should also see an additional **build.gradle** file under "Gradle Scripts" but I find the Android Studio interface a little bit glitchy and sometimes it does not do this right away. So try resynching, cleaning, even restarting Android Studio.
You should see the openCVLibrary310 module with all the OpenCV functions under java like in this screenshot:
[![enter image description here][6]][6]
if (!OpenCVLoader.initDebug()) {
Log.e(this.getClass().getSimpleName(), " OpenCVLoader.initDebug(), not working.");
} else {
Log.d(this.getClass().getSimpleName(), " OpenCVLoader.initDebug(), working.");
}
Then run your application. You should see lines like this in the Android Monitor:
(I don’t know why that line with the error message is there)
Put this code just below the code from the previous step and alter it to match your own files/directories.
String inputFileName="simm_01";
String inputExtension = "jpg";
String inputDir = getCacheDir().getAbsolutePath(); // use the cache directory for i/o
String outputDir = getCacheDir().getAbsolutePath();
String outputExtension = "png";
String inputFilePath = inputDir + File.separator + inputFileName + "." + inputExtension;
Log.d (this.getClass().getSimpleName(), "loading " + inputFilePath + "...");
Mat image = Imgcodecs.imread(inputFilePath);
Log.d (this.getClass().getSimpleName(), "width of " + inputFileName + ": " + image.width());
// if width is 0 then it did not read your image.
// for the canny edge detection algorithm, play with these to see different results
int threshold1 = 70;
int threshold2 = 100;
Mat im_canny = new Mat(); // you have to initialize output image before giving it to the Canny method
Imgproc.Canny(image, im_canny, threshold1, threshold2);
String cannyFilename = outputDir + File.separator + inputFileName + "_canny-" + threshold1 + "-" + threshold2 + "." + outputExtension;
Log.d (this.getClass().getSimpleName(), "Writing " + cannyFilename);
Imgcodecs.imwrite(cannyFilename, im_canny);