fix(images): src or template image may be recycled on findImage
This commit is contained in:
@@ -251,7 +251,7 @@ module.exports = function (runtime, global) {
|
||||
});
|
||||
if (typeof (view.setOnCheckedChangeListener) == 'function') {
|
||||
view.setOnCheckedChangeListener(function (v, isChecked) {
|
||||
emit("check", isChecked, view);
|
||||
emit("check", isChecked == true ? true : false, view);
|
||||
});
|
||||
}
|
||||
view._id = function (id) {
|
||||
|
||||
@@ -75,7 +75,7 @@ public class ColorFinder {
|
||||
if (rect != null) {
|
||||
Mat m = new Mat(image.getMat(), rect);
|
||||
Core.inRange(m, lowerBound, upperBound, bi);
|
||||
m.release();
|
||||
OpenCVHelper.release(m);
|
||||
} else {
|
||||
Core.inRange(image.getMat(), lowerBound, upperBound, bi);
|
||||
}
|
||||
@@ -87,8 +87,8 @@ public class ColorFinder {
|
||||
} else {
|
||||
result = new MatOfPoint(nonZeroPos);
|
||||
}
|
||||
bi.release();
|
||||
nonZeroPos.release();
|
||||
OpenCVHelper.release(bi);
|
||||
OpenCVHelper.release(nonZeroPos);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +1,20 @@
|
||||
package com.stardust.autojs.core.image;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
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.os.Build;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
import com.stardust.autojs.runtime.api.Images;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import org.opencv.android.Utils;
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfInt;
|
||||
import org.opencv.highgui.Highgui;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/11/25.
|
||||
@@ -128,18 +116,9 @@ public class ImageWrapper {
|
||||
mBitmap = null;
|
||||
}
|
||||
if (mMat != null) {
|
||||
mMat.release();
|
||||
OpenCVHelper.release(mMat);
|
||||
mMat = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
try {
|
||||
recycle();
|
||||
} finally {
|
||||
super.finalize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,9 +14,6 @@ import org.opencv.core.Rect;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/11/25.
|
||||
@@ -67,8 +64,7 @@ public class TemplateMatching {
|
||||
break;
|
||||
}
|
||||
// FIXME: 2018/3/31 此处的matchResult.release()某些情况下会导致currentTemplate被释放?
|
||||
// if (matchResult != null)
|
||||
// matchResult.release();
|
||||
OpenCVHelper.release(matchResult);
|
||||
matchResult = matchTemplate(src, currentTemplate, matchMethod);
|
||||
Pair<Point, Double> bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold);
|
||||
p = bestMatched.first;
|
||||
@@ -76,11 +72,10 @@ public class TemplateMatching {
|
||||
} else {
|
||||
//根据上一轮的匹配点,计算本次匹配的区域
|
||||
Rect r = getROI(p, src, currentTemplate);
|
||||
// if (matchResult != null)
|
||||
// matchResult.release();
|
||||
OpenCVHelper.release(matchResult);
|
||||
Mat m = new Mat(src, r);
|
||||
matchResult = matchTemplate(m, currentTemplate, matchMethod);
|
||||
m.release();
|
||||
OpenCVHelper.release(m);
|
||||
Pair<Point, Double> bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold);
|
||||
//不满足弱阈值,返回null
|
||||
if (bestMatched.second < weakThreshold) {
|
||||
@@ -92,8 +87,10 @@ public class TemplateMatching {
|
||||
p.x += r.x;
|
||||
p.y += r.y;
|
||||
}
|
||||
src.release();
|
||||
currentTemplate.release();
|
||||
if (src != img)
|
||||
OpenCVHelper.release(src);
|
||||
if (currentTemplate != template)
|
||||
OpenCVHelper.release(currentTemplate);
|
||||
//满足强阈值,返回当前结果
|
||||
if (similarity >= strictThreshold) {
|
||||
pyrUp(p, level);
|
||||
@@ -110,6 +107,7 @@ public class TemplateMatching {
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
private static Mat getPyramidDownAtLevel(Mat m, int level) {
|
||||
if (level == 0) {
|
||||
return m;
|
||||
@@ -174,8 +172,8 @@ public class TemplateMatching {
|
||||
int result_cols = img.cols() - temp.cols() + 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: img = %s, temp = %s", img.toString(), temp.toString()));
|
||||
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);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
import android.view.Display;
|
||||
import android.view.Surface;
|
||||
import android.view.WindowManager;
|
||||
@@ -18,6 +17,7 @@ import android.view.WindowManager;
|
||||
import com.stardust.autojs.annotation.ScriptVariable;
|
||||
import com.stardust.autojs.core.image.ColorFinder;
|
||||
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.ScreenCapturer;
|
||||
import com.stardust.autojs.core.image.TemplateMatching;
|
||||
@@ -263,7 +263,7 @@ public class Images {
|
||||
point.y = mScreenMetrics.scaleX((int) point.y);
|
||||
}
|
||||
if (src != image.getMat()) {
|
||||
src.release();
|
||||
OpenCVHelper.release(src);
|
||||
}
|
||||
return point;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user