android 下配置opencv,Imgproc等应用正常,但imread文件出错
No implementation found for long org.opencv.imgcodecs.Imgcodecs.imread_1
初以为是imgcodecs.imread_1问题,实际上缺少jni库
载入System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 系统库
回调函数
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
// TODO Auto-generated method stub
switch (status){
case BaseLoaderCallback.SUCCESS:
Log.i("opencv BaseLoaderCallback ", " ok!");
break;
default:
super.onManagerConnected(status);
Log.i("opencv BaseLoaderCallback ", " error!");
break;
}
}
};
重载函数onResume
@Override
protected void onResume(){//Android Studio 接入OpenCV 并不使用OpenCV_Manager
super.onResume();
if (!OpenCVLoader.initDebug()) {
Log.d("OpenCVLoader", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback);
} else {
Log.d("OpenCVLoader", "OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
计算图像面积
public static void areaCal() {//计算图像的面积 String[] args
// 1. 加载由libname参数指定的系统库
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 2. 打开图片
Mat src = Imgcodecs.imread(dirPath+"/1.jpg");
if (src.dataAddr()==0){
System.out.println("open file error!");
}
Mat mat = src;
Imgcodecs.imwrite(dirPath+"/1-0.jpg", mat);
//图片转换成灰度
Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY,0);
Imgcodecs.imwrite(dirPath+"/1-1.jpg", src);
//高斯模糊
Imgproc.GaussianBlur(src, src, new Size(15, 15), 0);
Imgcodecs.imwrite(dirPath+"/1-2.jpg", src);
//二值化操作
Imgproc.threshold(src,src,0,255,THRESH_BINARY | THRESH_TRIANGLE);
Imgcodecs.imwrite(dirPath+"/1-3.jpg", src);
//轮廓发现--得到contours和hierarchy
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(-1, -1));
Imgcodecs.imwrite(dirPath+"/1-4.jpg", hierarchy);
//相当于创建和原图尺寸相同一张黑色的图,用于后面画线作图
Mat contoursImg = Mat.zeros(mat.size(), CV_8UC3);
for(int i=0;i<contours.size();i++){
Rect rect = boundingRect(contours.get(i));
// if (rect.width < mat.cols() / 2)
// continue;
//在contoursImg上绘制最大的轮廓
Imgproc.drawContours(contoursImg,contours,i, new Scalar(0,0,255),
2,8,hierarchy,0,new Point(0, 0));
double area = contourArea(contours.get(i));
System.out.println("The "+i+"img area is "+area);
}
Imgcodecs.imwrite(dirPath+"/1-5.jpg", contoursImg);
}
注意
public class Core {
// these constants are wrapped inside functions to prevent inlining
private static String getVersion() { return "3.4.12"; }
private static String getNativeLibraryName() { return "opencv_java3"; }//412
private static int getVersionMajorJ() { return 3; }
private static int getVersionMinorJ() { return 4; }
private static int getVersionRevisionJ() { return 12; }
private static String getVersionStatusJ() { return ""; }
public static final String VERSION = getVersion();
public static final String NATIVE_LIBRARY_NAME = getNativeLibraryName();
}
与jnilibs 目录目录libopencv_java3.so对应
build.gradle
sourceSets {
main {
// Android demo app sources.
java {
srcDir 'src'
}
manifest.srcFile 'AndroidManifest.xml'
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = [project.ext.ASSET_DIR]
jniLibs.srcDir 'libs'
}
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}