fix(images): src or template image may be recycled on findImage

This commit is contained in:
hyb1996
2018-04-02 11:41:38 +08:00
parent 2a8b0ce3e0
commit 737f1a901f
6 changed files with 39 additions and 39 deletions

View File

@@ -251,7 +251,7 @@ module.exports = function (runtime, global) {
}); });
if (typeof (view.setOnCheckedChangeListener) == 'function') { if (typeof (view.setOnCheckedChangeListener) == 'function') {
view.setOnCheckedChangeListener(function (v, isChecked) { view.setOnCheckedChangeListener(function (v, isChecked) {
emit("check", isChecked, view); emit("check", isChecked == true ? true : false, view);
}); });
} }
view._id = function (id) { view._id = function (id) {

View File

@@ -75,7 +75,7 @@ public class ColorFinder {
if (rect != null) { if (rect != null) {
Mat m = new Mat(image.getMat(), rect); Mat m = new Mat(image.getMat(), rect);
Core.inRange(m, lowerBound, upperBound, bi); Core.inRange(m, lowerBound, upperBound, bi);
m.release(); OpenCVHelper.release(m);
} else { } else {
Core.inRange(image.getMat(), lowerBound, upperBound, bi); Core.inRange(image.getMat(), lowerBound, upperBound, bi);
} }
@@ -87,8 +87,8 @@ public class ColorFinder {
} else { } else {
result = new MatOfPoint(nonZeroPos); result = new MatOfPoint(nonZeroPos);
} }
bi.release(); OpenCVHelper.release(bi);
nonZeroPos.release(); OpenCVHelper.release(nonZeroPos);
return result; return result;
} }

View File

@@ -1,32 +1,20 @@
package com.stardust.autojs.core.image; package com.stardust.autojs.core.image;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Xfermode;
import android.media.Image; import android.media.Image;
import android.os.Build; import android.os.Build;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.view.KeyEvent;
import com.stardust.autojs.runtime.api.Images;
import com.stardust.pio.UncheckedIOException; import com.stardust.pio.UncheckedIOException;
import org.opencv.android.Utils; import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.highgui.Highgui; import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Collections;
/** /**
* Created by Stardust on 2017/11/25. * Created by Stardust on 2017/11/25.
@@ -128,18 +116,9 @@ public class ImageWrapper {
mBitmap = null; mBitmap = null;
} }
if (mMat != null) { if (mMat != null) {
mMat.release(); OpenCVHelper.release(mMat);
mMat = null; mMat = null;
} }
} }
@Override
protected void finalize() throws Throwable {
try {
recycle();
} finally {
super.finalize();
}
}
} }

View File

@@ -0,0 +1,23 @@
package com.stardust.autojs.core.image;
import android.support.annotation.Nullable;
import android.util.Log;
import org.opencv.core.Mat;
/**
* Created by Stardust on 2018/4/2.
*/
public class OpenCVHelper {
private static final String LOG_TAG = "OpenCv";
public static void release(@Nullable Mat mat) {
if (mat == null)
return;
mat.release();
}
}

View File

@@ -14,9 +14,6 @@ import org.opencv.core.Rect;
import org.opencv.core.Size; import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Imgproc;
import java.util.ArrayList;
import java.util.List;
/** /**
* Created by Stardust on 2017/11/25. * Created by Stardust on 2017/11/25.
@@ -67,8 +64,7 @@ public class TemplateMatching {
break; break;
} }
// FIXME: 2018/3/31 此处的matchResult.release()某些情况下会导致currentTemplate被释放 // FIXME: 2018/3/31 此处的matchResult.release()某些情况下会导致currentTemplate被释放
// if (matchResult != null) OpenCVHelper.release(matchResult);
// matchResult.release();
matchResult = matchTemplate(src, currentTemplate, matchMethod); matchResult = matchTemplate(src, currentTemplate, matchMethod);
Pair<Point, Double> bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold); Pair<Point, Double> bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold);
p = bestMatched.first; p = bestMatched.first;
@@ -76,11 +72,10 @@ public class TemplateMatching {
} else { } else {
//根据上一轮的匹配点,计算本次匹配的区域 //根据上一轮的匹配点,计算本次匹配的区域
Rect r = getROI(p, src, currentTemplate); Rect r = getROI(p, src, currentTemplate);
// if (matchResult != null) OpenCVHelper.release(matchResult);
// matchResult.release();
Mat m = new Mat(src, r); Mat m = new Mat(src, r);
matchResult = matchTemplate(m, currentTemplate, matchMethod); matchResult = matchTemplate(m, currentTemplate, matchMethod);
m.release(); OpenCVHelper.release(m);
Pair<Point, Double> bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold); Pair<Point, Double> bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold);
//不满足弱阈值返回null //不满足弱阈值返回null
if (bestMatched.second < weakThreshold) { if (bestMatched.second < weakThreshold) {
@@ -92,8 +87,10 @@ public class TemplateMatching {
p.x += r.x; p.x += r.x;
p.y += r.y; p.y += r.y;
} }
src.release(); if (src != img)
currentTemplate.release(); OpenCVHelper.release(src);
if (currentTemplate != template)
OpenCVHelper.release(currentTemplate);
//满足强阈值,返回当前结果 //满足强阈值,返回当前结果
if (similarity >= strictThreshold) { if (similarity >= strictThreshold) {
pyrUp(p, level); pyrUp(p, level);
@@ -110,6 +107,7 @@ public class TemplateMatching {
return p; return p;
} }
private static Mat getPyramidDownAtLevel(Mat m, int level) { private static Mat getPyramidDownAtLevel(Mat m, int level) {
if (level == 0) { if (level == 0) {
return m; return m;
@@ -174,8 +172,8 @@ public class TemplateMatching {
int result_cols = img.cols() - temp.cols() + 1; int result_cols = img.cols() - temp.cols() + 1;
int result_rows = img.rows() - temp.rows() + 1; int result_rows = img.rows() - temp.rows() + 1;
Log.d(LOG_TAG, String.format("matchTemplate: rows = %d, cols = %d", result_rows, result_cols)); Log.d(LOG_TAG, String.format("matchTemplate: rows = %d, cols = %d", result_rows, result_cols));
Log.d(LOG_TAG, String.format("matchTemplate: img = %s, temp = %s", img.toString(), temp.toString()));
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1); Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
Log.d(LOG_TAG, String.format("matchTemplate: img = %s, temp = %s, result = %s", img.toString(), temp.toString(), result.toString()));
Imgproc.matchTemplate(img, temp, result, match_method); Imgproc.matchTemplate(img, temp, result, match_method);
return result; return result;
} }

View File

@@ -10,7 +10,6 @@ import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.util.Base64; import android.util.Base64;
import android.util.Log;
import android.view.Display; import android.view.Display;
import android.view.Surface; import android.view.Surface;
import android.view.WindowManager; import android.view.WindowManager;
@@ -18,6 +17,7 @@ import android.view.WindowManager;
import com.stardust.autojs.annotation.ScriptVariable; import com.stardust.autojs.annotation.ScriptVariable;
import com.stardust.autojs.core.image.ColorFinder; import com.stardust.autojs.core.image.ColorFinder;
import com.stardust.autojs.core.image.ImageWrapper; import com.stardust.autojs.core.image.ImageWrapper;
import com.stardust.autojs.core.image.OpenCVHelper;
import com.stardust.autojs.core.image.ScreenCaptureRequester; import com.stardust.autojs.core.image.ScreenCaptureRequester;
import com.stardust.autojs.core.image.ScreenCapturer; import com.stardust.autojs.core.image.ScreenCapturer;
import com.stardust.autojs.core.image.TemplateMatching; import com.stardust.autojs.core.image.TemplateMatching;
@@ -263,7 +263,7 @@ public class Images {
point.y = mScreenMetrics.scaleX((int) point.y); point.y = mScreenMetrics.scaleX((int) point.y);
} }
if (src != image.getMat()) { if (src != image.getMat()) {
src.release(); OpenCVHelper.release(src);
} }
return point; return point;
} }