后面可以叠加物体检测,参考:https://github.com/kongqw/OpenCVForAndroid
1.0 需求
在安卓开发板上实现视频监控功能,并能后台监控。
由于后期可能跑视频识别,所以考虑用OpenCV实现:通过OpenCV Manager进行动态库的链接,实现帧的预览和保存。
android版本:5.0.1
开发平台:Android Studio 3.1.2
OpenCV版本:opencv4android 3.2.0
OpenCV Manager:OpenCV_3.2.0_Manager_3.20_armeabi.apk
2.0 步骤
2.1 下载opencv-3.2.0-android-sdk.zip
可以直接去官网下载。如果下载很慢,可以切换源试试
解压后,会看到OpenCV-android-sdk目录下有如下目录:
2.2 开发板安装对应的OpenCV Manage版本
OpenCV_3.2.0_Manager_3.20_armeabi.apk
2.3 新建工程VideoMonitor并导入Opencv Module
具体步骤: File --> New --> Import Module选择opencv-3.2.0-android-sdk下的java目录
选择步骤2.1下载的opencv-3.2.0-android-sdk目录下的如下目录:
工程导入完会看到project下有openCVLibrary320:
2.4 添加工程依赖(opencv)
点击Project Structure:
点击Dependencies --> 点击+ --> 选择module dependency
2.5 把opencv要用到的动态库放到项目中
将下载解压得到的文件中的“sdk\native\libs\”复制一份到项目的“app\src\main\”并重命名为jniLibs:
2.6 修改布局文件
布局修改如下:
这里的JavaCameraView就是OpenCV里的预览部件,然后我们再添加了一个拍照的按键。
注意:之前忘记添加opencv,导致R文件生成失败,MainActivity中R类就找不到了。
2.7 添加权限
打开AndroidManifest.xml,添加我们需要的权限,摄像头和内部储存,这个也十分重要:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.8 正式编写MainActivity
具体代码如下:
package cn.com.suredata.videomonitor;
import android.content.pm.ActivityInfo;
import android.icu.text.SimpleDateFormat;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.io.File;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
private String TAG = "OpenCV_Test";
//OpenCV的相机接口
private CameraBridgeViewBase mCVCamera;
//缓存相机每帧输入的数据
private Mat mRgba;
private Button button;
/**通过OpenCV管理Android服务,初始化OpenCV**/
BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
Log.i(TAG, "OpenCV loaded successfully");
mCVCamera.enableView();
break;
default:break;
}
}
};
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
if(msg.what == 1){
button.performClick();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化并设置预览部件
mCVCamera = (CameraBridgeViewBase) findViewById(R.id.camera_view);
mCVCamera.setCvCameraViewListener(this);
//拍照按键
button = (Button) findViewById(R.id.deal_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mRgba != null) {
if(!mRgba.empty()) {
Mat inter = new Mat(mRgba.width(), mRgba.height(), CvType.CV_8UC4);
Log.e("Mat","...............1...............");
//将四通道的RGBA转为三通道的BGR,重要!!
Imgproc.cvtColor(mRgba, inter, Imgproc.COLOR_RGBA2BGR);
Log.e("Mat","...............2...............");
File sdDir = null;
//判断是否存在机身内存
boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(sdCardExist) {
//获得机身储存根目录
sdDir = Environment.getExternalStorageDirectory();
Log.e("Mat","...............3...............");
}
//将拍摄准确时间作为文件名
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String filename = sdf.format(new Date());
String savepath=sdDir + "/Pictures/OpenCV/";
File f=new File(savepath);
if(!f.exists()){
f.mkdirs();
}
String filePath = sdDir + "/Pictures/OpenCV/" + filename + ".png";
Log.e("Mat","..............."+filePath+"...............");
//将转化后的BGR矩阵内容写入到文件中
Imgcodecs.imwrite(filePath, inter);
Toast.makeText(MainActivity.this, "图片保存到: "+ filePath, Toast.LENGTH_SHORT).show();
}
}
}
});
// moveTaskToBack(true);
// new test().start();
}
private class test extends Thread{
@Override
public void run() {
super.run();
while (true){
try {
Thread.sleep(2000);
Log.e("begin","kaishi.........");
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}catch (Exception e){
Log.e("error",e.getMessage());
}
}
}
}
@Override
protected void onResume() {
/***强制横屏***/
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
//横屏后才加载部件,否则会FC
if(!OpenCVLoader.initDebug()) {
Log.d(TAG, "OpenCV library not found!");
} else {
Log.d(TAG, "OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
super.onResume();
}
@Override
protected void onDestroy() {
if(mCVCamera != null) {
mCVCamera.disableView();
}
super.onDestroy();
}
//对象实例化及基本属性的设置,包括长度、宽度和图像类型标志
public void onCameraViewStarted(int width, int height) {
Log.e("Mat","...............4...............");
mRgba = new Mat(height, width, CvType.CV_8UC4);
}
/**图像处理都写在这里!!!**/
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba(); //一定要有!!!不然数据保存不进MAT中!!!
//直接返回输入视频预览图的RGB数据并存放在Mat数据中
Log.e("Mat","...............5...............");
return mRgba;
}
//结束时释放
@Override
public void onCameraViewStopped() {
Log.e("Mat","...............6...............");
mRgba.release();
// mTmp.release();
}
}