Skip to content

Commit 7d12c2f

Browse files
committed
update openCV: fix libpng vulnerability in android
updte to opencv version 3.1 to fix libpng vulnerability in android implementation
1 parent 0fbc996 commit 7d12c2f

File tree

4 files changed

+67
-36
lines changed

4 files changed

+67
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Image Detection Plugin (android & ios)
2-
This plugin allows the application to detect if an inputed image target is visible, or not, by matching the image features with the device camera features using [OpenCV](http://opencv.org/) v2.4.11. It also presents the device camera preview in the background.
2+
This plugin allows the application to detect if an inputed image target is visible, or not, by matching the image features with the device camera features using [OpenCV](http://opencv.org/) v3.1. It also presents the device camera preview in the background.
33

44
### Note
55
The plugin is aimed to work in **portrait mode**, should also work in landscape but no guarantees.

plugin.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
id="com.cloudoki.imagedetectionplugin"
5-
version="0.0.1">
5+
version="0.0.2">
66
<name>Image Detection Plugin</name>
77

88
<description>The ImageDetectionPlugin for Cordova enables the use of OpenCV SDK to detect an inputed image.</description>
99

1010
<author>Délio Amaral</author>
1111

12-
<keywords>OpenCV,image detection, image matching</keywords>
12+
<keywords>OpenCV, image detection, image matching</keywords>
1313

1414
<repo>https://github.com/</repo>
1515

src/android/ImageDetectionPlugin.java

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import android.view.View;
2525
import android.view.ViewGroup;
2626
import android.view.WindowManager;
27-
import android.webkit.WebView;
2827
import android.widget.FrameLayout;
2928

3029
import org.apache.cordova.CallbackContext;
@@ -50,13 +49,13 @@
5049
import org.opencv.core.Point;
5150
import org.opencv.core.Scalar;
5251
import org.opencv.core.Size;
53-
import org.opencv.features2d.DMatch;
52+
import org.opencv.core.DMatch;
5453
import org.opencv.features2d.DescriptorExtractor;
5554
import org.opencv.features2d.DescriptorMatcher;
5655
import org.opencv.features2d.FeatureDetector;
5756
import org.opencv.features2d.Features2d;
58-
import org.opencv.features2d.KeyPoint;
59-
import org.opencv.highgui.Highgui;
57+
import org.opencv.core.KeyPoint;
58+
import org.opencv.imgcodecs.Imgcodecs;
6059
import org.opencv.imgproc.Imgproc;
6160

6261
import java.util.ArrayList;
@@ -120,10 +119,10 @@ public int getHeight(Object obj) {
120119
}
121120
}
122121

123-
@SuppressWarnings("deprecation")
124122
@Override
125123
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
126124
activity = cordova.getActivity();
125+
127126
super.initialize(cordova, webView);
128127

129128
mLoaderCallback = new BaseLoaderCallback(activity) {
@@ -314,7 +313,7 @@ public void onResume(boolean multitasking) {
314313
super.onResume(multitasking);
315314
if (!OpenCVLoader.initDebug()) {
316315
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
317-
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_11, activity, mLoaderCallback);
316+
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, activity, mLoaderCallback);
318317
} else {
319318
Log.d(TAG, "OpenCV library found inside package. Using it!");
320319
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
@@ -611,7 +610,7 @@ public void run() {
611610
if(save_files) {
612611
if (count % 10 == 0) {
613612
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
614-
Highgui.imwrite(extStorageDirectory + "/pic" + count + ".png", gray);
613+
Imgcodecs.imwrite(extStorageDirectory + "/pic" + count + ".png", gray);
615614
Log.i("### FILE ###", "File saved to " + extStorageDirectory + "/pic" + count + ".png");
616615
}
617616
count++;
@@ -699,22 +698,53 @@ public void run() {
699698
Mat H = Calib3d.findHomography(obj, scene, Calib3d.RANSAC, 5);
700699

701700
boolean result = true;
702-
double det = H.get(0, 0)[0] * H.get(1, 1)[0] - H.get(1, 0)[0] * H.get(0, 1)[0];
703-
if (det < 0) {
704-
result = false;
705-
}
706-
double N1 = Math.sqrt(H.get(0, 0)[0] * H.get(0, 0)[0] + H.get(1, 0)[0] * H.get(1, 0)[0]);
707-
if (N1 > 4 || N1 < 0.1) {
708-
result = false;
709-
}
710701

711-
double N2 = Math.sqrt(H.get(0, 1)[0] * H.get(0, 1)[0] + H.get(1, 1)[0] * H.get(1, 1)[0]);
712-
if (N2 > 4 || N2 < 0.1) {
713-
result = false;
714-
}
702+
double det = 0, N1 = 0, N2 = 0, N3 = 0;
703+
704+
if (H != null) {
705+
double[] p1 = H.get(0, 0);
706+
double[] p2 = H.get(1, 1);
707+
double[] p3 = H.get(1, 0);
708+
double[] p4 = H.get(0, 1);
709+
double[] p5 = H.get(2, 0);
710+
double[] p6 = H.get(2, 1);
711+
712+
if (p1 != null && p2 != null && p3 != null && p4 != null) {
713+
det = p1[0] * p2[0] - p3[0] * p4[0];
714+
if (det < 0) {
715+
result = false;
716+
}
717+
} else {
718+
result = false;
719+
}
720+
721+
if (p1 != null && p3 != null) {
722+
N1 = Math.sqrt(p1[0] * p1[0] + p3[0] * p3[0]);
723+
if (N1 > 4 || N1 < 0.1) {
724+
result = false;
725+
}
726+
} else {
727+
result = false;
728+
}
729+
730+
if (p2 != null && p4 != null) {
731+
N2 = Math.sqrt(p4[0] * p4[0] + p2[0] * p2[0]);
732+
if (N2 > 4 || N2 < 0.1) {
733+
result = false;
734+
}
735+
} else {
736+
result = false;
737+
}
715738

716-
double N3 = Math.sqrt(H.get(2, 0)[0] * H.get(2, 0)[0] + H.get(2, 1)[0] * H.get(2, 1)[0]);
717-
if (N3 > 0.002) {
739+
if (p5 != null && p6 != null) {
740+
N3 = Math.sqrt(p5[0] * p5[0] + p6[0] * p6[0]);
741+
if (N3 > 0.002) {
742+
result = false;
743+
}
744+
} else {
745+
result = false;
746+
}
747+
} else {
718748
result = false;
719749
}
720750

@@ -729,22 +759,24 @@ public void run() {
729759
Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
730760
Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);
731761

732-
obj_corners.put(0, 0, new double[]{0, 0});
733-
obj_corners.put(1, 0, new double[]{pattern.cols(), 0});
734-
obj_corners.put(2, 0, new double[]{pattern.cols(), pattern.rows()});
735-
obj_corners.put(3, 0, new double[]{0, pattern.rows()});
762+
obj_corners.put(0, 0, 0, 0);
763+
obj_corners.put(1, 0, pattern.cols(), 0);
764+
obj_corners.put(2, 0, pattern.cols(), pattern.rows());
765+
obj_corners.put(3, 0, 0, pattern.rows());
736766

737767
Core.perspectiveTransform(obj_corners, scene_corners, H);
738768

739-
Core.line(img_matches, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)), new Scalar(0, 255, 0), 4);
740-
Core.line(img_matches, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)), new Scalar(0, 255, 0), 4);
741-
Core.line(img_matches, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)), new Scalar(0, 255, 0), 4);
742-
Core.line(img_matches, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)), new Scalar(0, 255, 0), 4);
769+
Imgproc.line(img_matches, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)), new Scalar(0, 255, 0), 4);
770+
Imgproc.line(img_matches, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)), new Scalar(0, 255, 0), 4);
771+
Imgproc.line(img_matches, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)), new Scalar(0, 255, 0), 4);
772+
Imgproc.line(img_matches, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)), new Scalar(0, 255, 0), 4);
743773
}
744774
} else {
745775
updateState(false);
746776
}
747-
H.release();
777+
if (H != null) {
778+
H.release();
779+
}
748780
}
749781
}
750782
gray.release();
@@ -773,11 +805,10 @@ private void setBase64Pattern(String image_base64) {
773805
//Imgproc.equalizeHist(pattern, pattern);
774806

775807
if(save_files) {
776-
Bitmap bmp = scaled;
777-
Utils.matToBitmap(pattern, bmp);
808+
Utils.matToBitmap(pattern, scaled);
778809
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
779810
int num = (int) (Math.random() * 10001);
780-
Highgui.imwrite(extStorageDirectory + "/pic" + num + ".png", pattern);
811+
Imgcodecs.imwrite(extStorageDirectory + "/pic" + num + ".png", pattern);
781812
Log.i("### FILE ###", "File saved to " + extStorageDirectory + "/pic" + num + ".png");
782813
}
783814

src/android/libs/opencv-release.aar

10.2 MB
Binary file not shown.

0 commit comments

Comments
 (0)