转载自http://blog.csdn.net/jesse__zhong/article/details/24889709

.......省略包

public class Staticdetection2Activity extends Activity {

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.activity_staticdetection2);
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 ...");

// use the red paint
/*
* Paint paint = new Paint(); paint.setColor(Color.RED);
* paint.setStrokeWidth(Math.max(img.getWidth(),
* img.getHeight()) / 100f);
*
* // create a new canvas Bitmap bitmap =
* Bitmap.createBitmap(img.getWidth(), img.getHeight(),
* img.getConfig()); Canvas canvas = new Canvas(bitmap);
* canvas.drawBitmap(img, new Matrix(), null);
*
* float x, y, w, h;
*
* x = y = w = h = 100;
*
* // change percent value to the real size x = x / 100 *
* img.getWidth(); w = w / 100 * img.getWidth() * 0.7f; y = y /
* 100 * img.getHeight(); h = h / 100 * img.getHeight() * 0.7f;
*
* // draw the box to mark it out canvas.drawLine(x - w, y - h,
* x - w, y + h, paint); canvas.drawLine(x - w, y - h, x + w, y
* - h, paint); canvas.drawLine(x + w, y + h, x - w, y + h,
* paint); canvas.drawLine(x + w, y + h, x + w, y - h, paint);
*
* img = bitmap;
*/

String xmlfilePath = "/sdcard/FaceDetect/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 (Rect rect : faceDetections.toArray()) {
Core.rectangle(
testMat,
new Point(rect.x, rect.y),
new 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);

/*Staticdetection2Activity.this.runOnUiThread(new Runnable() {

public void run() {
// show the image
imageView.setImageBitmap(img);
// textView.setText("Finished, "+ count + " faces.");
textView.setText("Finished, " + " faces.");
// set edit text
// editText.setText(str);
}
});*/
}
});

imageView = (ImageView) this.findViewById(R.id.imageView1);
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.staticdetection2, menu);
return true;
}

@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.3.2_Manager_2.4_*.apk程序包,存在于OpenCV安装包的apk目录中
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this,
mLoaderCallback);
}
}

android opencv 人脸检测的更多相关文章

  1. Android+openCV人脸检测2(静态图片)

    前几篇文章中有提到对openCV环境配置,这里再重新梳理导入和使用openCV进行简单的人脸检测(包括使用级联分类器) 一 首先导入openCVLibrary320 二 设置gradle的sdk版本号 ...

  2. keras系列︱人脸表情分类与识别:opencv人脸检测+Keras情绪分类(四)

    引自:http://blog.csdn.net/sinat_26917383/article/details/72885715 人脸识别热门,表情识别更加.但是表情识别很难,因为人脸的微表情很多,本节 ...

  3. Android—基于OpenCV+Android实现人脸检测

    导读 OpenCV 是一个开源的跨平台计算机视觉库, 采C++语言编写,实现了图像处理和计算机视觉方面的很多通用算法,同时也提供对Python,Java,Android等的支持,这里利用Android ...

  4. opencv人脸检测分类器训练小结

    这两天在初学目标检测的算法及步骤,其中人脸检测作为最经典的算法,于是进行了重点研究.该算法最重要的是建立人脸检测分类器,因此我用了一天的时间来学习分类器的训练.这方面的资料很多,但是能按照一个资料运行 ...

  5. opencv人脸检测,旋转处理

    年会签到,拍自己的大头照,有的人可能会拍成横向的,需要旋转,用人脸检测并修正它(图片). 1. 无脑检测步骤为: 1. opencv 读取图片,灰度转换 2. 使用CascadeClassifier( ...

  6. OpenCV人脸检测并把图片写成avi视频

    读出某一个文件夹下“jpg”后缀的全部图片后,用的OpenCV自带的人脸检测检测图片中的人脸,调整图片的大小写成一个avi视频. 主要是要记录一下CvVideoWriter的用法和如何从文件夹中读取某 ...

  7. 人脸检测学习笔记(数据集-DLIB人脸检测原理-DLIB&OpenCV人脸检测方法及对比)

    1.Easily Create High Quality Object Detectors with Deep Learning 2016/10/11 http://blog.dlib.net/201 ...

  8. OpenCV——人脸检测

    OpenCV支持的目标检测的方法: 利用样本的Haar特征进行的分类器训练,得到的级联boosted分类器(Cascade Classification) 1.加载级联分类器 CascadeClass ...

  9. OpenCV: OpenCV人脸检测框可信度排序

    参考文章:http://blog.csdn.net/hua_007/article/details/45368607 使用OpenCV进行人脸识别时,使用 casecade.detectMultiSc ...

随机推荐

  1. 关于session更新的问题

    最近在学习用ssh框架做一个实习生招聘系统,已经做了大半.今天突然想到一个问题,在登录的时候我把用户的所有信息放到session中去,那么我不同用户同时登录的时候session中的信息是否会被覆盖掉( ...

  2. 通过java发送http请求

    通常的http请求都是由用户点击某个连接或者按钮来发起的,但是在一些后台的Java程序中需要发送一些get或这post请求,因为不涉及前台页面,该怎么办呢? 下面为大家提供一个Java发送http请求 ...

  3. centos 6.5下安装docker

    关于docker的更多信息,请移步度娘.以下两个链接也对docker有了具体的介绍: http://www.docker.org.cn/book/docker/what-is-docker-16.ht ...

  4. applicationContext.xml存放的位置

    web.xml中classpath:和classpath*:  有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件 ...

  5. Don't Repeat Yourself (不要重复你自己)

    DRY是指Don't Repeat Yourself特指在程序设计以及计算中避免重复代码,因为这样会降低灵活性.简洁性,并且可能导致代码之间的矛盾.<The Pragmatic Programm ...

  6. LumiSoft.Net邮件接收乱码问题解决

    本文非本人编写,转载自:http://www.cnblogs.com/youngerliu/archive/2013/05/27/3101488.html 今天遇到用LumiSoft.Net这个组件收 ...

  7. Jquery 回到顶部

    转:http://www.cnblogs.com/DemoLee/archive/2012/04/20/2459082.html 用jQuery实现渐隐渐显的返回顶部效果(附多图)   先来看几个图片 ...

  8. C语言面试题大汇总

    static有什么用途?(请至少说明两种)1.限制变量的作用域2.设置变量的存储域7. 引用与指针有什么差别?1) 引用必须被初始化,指针不必.2) 引用初始化以后不能被改变,指针能够改变所指的对象. ...

  9. #define使用方法

    1.简单的define定义 #define MAXTIME 1000 一个简单的MAXTIME就定义好了,它代表1000,假设在程序里面写 if(i<MAXTIME){.........} 编译 ...

  10. 初步认知MySQL metadata lock(MDL)

    http://blog.itpub.net/26515977/viewspace-1208250/ 概述 随着5.5.3引入MDL,更多的Query被“Waiting for table metada ...