1 droidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.ydk.cv">
<!-- 摄像头权限 -->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- 往SDCard写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 在SDCard读取数据的权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
android:label="CVA"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2、activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<org.opencv.android.JavaCameraView
android:id="@+id/camera_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom|center_horizontal">
<Button
android:id="@+id/switch_camera"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:text="相机切换"/>
</RelativeLayout>
</FrameLayout>
```bash
3 MainActivity.java
```
```java
package edu.ydk.cv;
import java.io.File;
import java.lang.System;
import android.content.pm.ActivityInfo;
import android.util.Log;
import java.io.InputStream;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.core.Size;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.core.Core;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Context;
import android.view.WindowManager;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.objdetect.CascadeClassifier;
import java.io.FileOutputStream;
public class MainActivity extends Activity implements CvCameraViewListener2{
private String TAG = "OpenCV_Test";
private CameraBridgeViewBase cameraView;
private Mat mRgba,mTmp,mRgb;
private Button mButtonSwitchCamera;
private CascadeClassifier classifier;
private int mAbsoluteFaceSize = 0;
private boolean isFrontCamera = false;
static {System.loadLibrary("opencv_java3");}
BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
{
@Override
public void onManagerConnected(int status){
switch (status)
{
case LoaderCallbackInterface.SUCCESS:
Log.i(TAG,"OpenCV loaded successfully");
cameraView.enableView();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initClassifier();
initWindowSettings();
cameraView = (CameraBridgeViewBase) findViewById(R.id.camera_view);
isFrontCamera = false;
cameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_BACK);
cameraView.enableFpsMeter();
cameraView.setMaxFrameSize(1280,720);
cameraView.setCvCameraViewListener(this);
mButtonSwitchCamera = (Button) findViewById(R.id.switch_camera);
mButtonSwitchCamera.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(R.id.switch_camera == v.getId()){
cameraView.disableView();
if (isFrontCamera) {
cameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_BACK);
isFrontCamera = false;
} else {
cameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
isFrontCamera = true;
}
cameraView.enableView();
}
}
});
}
@Override
public void onResume()
{
super.onResume();
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);
}
};
@Override
public void onDestroy()
{
super.onDestroy();
if(cameraView!=null)
{
cameraView.disableView();
}
};
@Override
public void onCameraViewStarted(int width, int height)
{
mRgba = new Mat(height, width, CvType.CV_8UC4);
mRgb = new Mat(height, width, CvType.CV_8UC3);
mTmp = new Mat(height, width, CvType.CV_8UC4);
}
@Override
public void onCameraViewStopped()
{
mRgba.release();
mTmp.release();
}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
{
mRgba = DetectFace(inputFrame);
return mRgba;
}
public Mat DetectFace(CvCameraViewFrame inputFrame)
{
Mat _mRgba = inputFrame.rgba();
Mat _mGray = inputFrame.gray();
if (isFrontCamera)
{
Core.flip(_mRgba, _mRgba, 1);
Core.flip(_mGray, _mGray, 1);
}
float mRelativeFaceSize = 0.2f;
if (mAbsoluteFaceSize == 0)
{
int height = _mGray.rows();
if (Math.round(height * mRelativeFaceSize) > 0)
{
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
}
}
MatOfRect faces = new MatOfRect();
if (classifier != null)
classifier.detectMultiScale(_mGray, faces, 1.1, 2, 2,
new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
Rect[] facesArray = faces.toArray();
Scalar faceRectColor = new Scalar(0, 255, 0, 255);
for (Rect faceRect : facesArray)
Imgproc.rectangle(_mRgba, faceRect.tl(), faceRect.br(), faceRectColor, 3);
return _mRgba;
}
private void initWindowSettings() {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
private void initClassifier() {
try {
InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
File cascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
FileOutputStream os = new FileOutputStream(cascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
classifier = new CascadeClassifier(cascadeFile.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
|