任何版本的安卓应用,所需的权限都要首先在AndroidManifest.xml声明
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.front.autofocus"
android:required="false" />
由于安卓版本关系,Android6之后需要运行时获取权限,这里使用第三方库permissionsdispatcher,
Github:https://github.com/permissions-dispatcher/PermissionsDispatcher
使用教程:
1.依赖PermissionsDispatcher库,在build.gradle(app)里添加:
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation project(path: ':OpenCV')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//引入PermissionsDispatcher库
implementation 'com.github.hotchemi:permissionsdispatcher:3.2.0'
annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:3.2.0'
}
2.在需要获取动态权限的Activity或者Fragment,添加必要注解:
@RuntimePermissions
@NeedsPermission将所需的权限获取后,再进行下一步操作。
编写布局文件,加入摄像头预览控件JavaCameraView,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<org.opencv.android.JavaCameraView
android:id="@+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"
opencv:camera_id="any" />
</androidx.constraintlayout.widget.ConstraintLayout>
在AndroidManifest.xml声明activity,设置横屏
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
添加libc++_shared.so,这个我在网上的其他博客并没有看到,但我这边集成时,确实需要,应该是其内部封装通过一些jni接口调用c++算法时需要集成的一个so,我上传了armeabi-v7a的libc++_shared.so,下面是下载地址:
https://download.csdn.net/download/u013370255/12609010
加入模型文件lbpcascade_frontalface_improved.xml
文件来自于…\opencv-4.3.0-android-sdk\OpenCV-android-sdk\sdk\etc\lbpcascades
主显示界面mainActivity
package com.zhb.opencvdemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.objdetect.CascadeClassifier;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;
@RuntimePermissions
public class MainActivity extends AppCompatActivity {
//预览图
CameraBridgeViewBase mOpenCvCameraView;
//模型文件
File mCascadeFile;
//级联分类器
private CascadeClassifier cascadeClassifier = null;
//4通道的图片矩阵,彩图
Mat mRgba;
//单通道的图片矩阵,灰图
Mat mGray;
//摄像头索引:-1/0后置摄像头,1前置摄像头
private int mCameraIndex = 1;
// 使用opencv自带的分类器文件初始化
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
try {
InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface_improved);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface_improved.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
// 加载cascadeClassifier
cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());
cascadeDir.delete();
} catch (IOException e) {
e.printStackTrace();
}
//启用相机
mOpenCvCameraView.setCameraPermissionGranted();
mOpenCvCameraView.enableView();
}
break;
default: {
super.onManagerConnected(status);
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOpenCvCameraView = findViewById(R.id.cameraView);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
//摄像头索引-1/0:后置双摄1:前置
mOpenCvCameraView.setCameraIndex(mCameraIndex);
//openCV预览界面监听
mOpenCvCameraView.setCvCameraViewListener(new CameraBridgeViewBase.CvCameraViewListener2() {
@Override
public void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
mRgba.release();
mGray.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
return mRgba;
}
});
}
@Override
public void onPause() {
super.onPause();
disableCamera();
}
@Override
public void onResume() {
super.onResume();
MainActivityPermissionsDispatcher.showCameraWithPermissionCheck(this);
}
@Override
public void onDestroy() {
super.onDestroy();
disableCamera();
}
public void disableCamera() {
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// NOTE: delegate the permission handling to generated method
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
@NeedsPermission({
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE})
void showCamera() {
//OpenCV初始化
if (!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback);
} else {
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
}