http://jingyan.baidu.com/article/948f592448183bd80ff5f9d1.html
搭好环境开始简单编程,尽管opencv在Android上检测速度确实有待提高还是贴上源码(本人于小米手机测试OK 三星手机貌似有点问题)
MainActivity.java
package com.example.facedec;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import android.os.Bundle;
import android.provider.MediaStore.Images.ImageColumns;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
final private static String TAG = "StaticrecognitionActivity";
final private int PICTURE_CHOOSE = 1;
private ImageView imageView = null;
private Bitmap img = null;
private Button buttonDetect = null;
private TextView textView = null;
private EditText editText = null;
// OpenCV类库加载并初始化成功后的回调函数,在此我们不进行任何操作
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// get a picture form your phone
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, PICTURE_CHOOSE);
}
});
//editText = (EditText) this.findViewById(R.id.editText1);
textView = (TextView) this.findViewById(R.id.textView1);
buttonDetect = (Button) this.findViewById(R.id.button2);
// buttonDetect.setVisibility(View.INVISIBLE);
buttonDetect.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
textView.setText("Waiting ...");
String xmlfilePath = "//sdcard//haarcascade_frontalface_alt2.xml";
CascadeClassifier faceDetector = new CascadeClassifier(
xmlfilePath);
// Bitmap bmptest = BitmapFactory.decodeResource(getResources(),
// R.drawable.lena);
Mat testMat = new Mat();
Utils.bitmapToMat(img, testMat);
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(testMat, faceDetections);
Log.i(String.format("Detected %s faces",
faceDetections.toArray().length), "");
int facenum = 0;
// Draw a bounding box around each face.
for (org.opencv.core.Rect rect : faceDetections.toArray()) {
Core.rectangle(
testMat,
new org.opencv.core.Point(rect.x, rect.y),
new org.opencv.core.Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(255, 0, 0));
++facenum;
}
// Save the visualized detection.
// Bitmap bmpdone = Bitmap.createBitmap(bmptest.getWidth(),
// bmptest.getHeight(), Config.RGB_565);
Utils.matToBitmap(testMat, img);
imageView.setImageBitmap(img);
textView.setText("Facecount:" + facenum);
}
});
imageView = (ImageView) this.findViewById(R.id.image_view);
imageView.setImageBitmap(img);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
// the image picker callback
if (requestCode == PICTURE_CHOOSE) {
if (intent != null) {
Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
Options options = new Options();
options.inJustDecodeBounds = true;
img = BitmapFactory.decodeFile(fileSrc, options);
options.inSampleSize = Math.max(1, (int) Math.ceil(Math.max(
(double) options.outWidth / 1024f,
(double) options.outHeight / 1024f)));
options.inJustDecodeBounds = false;
img = BitmapFactory.decodeFile(fileSrc, options);
textView.setText("Clik Detect. ==>");
imageView.setImageBitmap(img);
buttonDetect.setVisibility(View.VISIBLE);
} else {
Log.d(TAG, "idButSelPic Photopicker canceled");
}
}
}
@Override
public void onResume() {
super.onResume();
// 通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是
// OpenCV_2.4.10_Manager_2.4_*.apk程序包,存在于OpenCV安装包的apk目录中
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, this,
mLoaderCallback);
}
}
整体项目源码见