转载自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. Sql CLR

    using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsof ...

  2. linux信号量超过系统限制

    部署一台新服务器,信号量报错,观察也没有key冲突,错误分析及解决如下: 创建一个不存在的信号量集返回参数错误的报错,因为信号量集的信号量数量超过了系统限制. 系统默认 /home/poc#ipcs ...

  3. hadoop 关闭进程时报错no 进程 to stop

    前两天和朋友李天王吃饭的时候,聊到了一个hadoop的运维的很简单问题,感觉很有意思,以前也没有注意过,现在加以重现和整理.   感谢李天王的分享....   翻看了yarn-deamon.sh st ...

  4. Flex友好提示、警告

    Flex 自带的Alert带给用户的体验并不好,对于一些简单的提示来说,这个小题大作了. <?xml version="1.0" encoding="utf-8&q ...

  5. c# winform 把彩色图片转换为灰色的图片,变灰,灰度图片,速度很快,safe,unsafe

    把彩色图片转换为灰色的图片,直接用.net接口遍历每个像素点转换的效率非常低,800K的图片65万像素我的电脑要用5分钟,而用了unsafe,速度提高了几千倍,同样的图片只用了0.几秒 附一个常用的遍 ...

  6. c# 自己制作一个简单的项目倒计时器

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. SlideLayout

    https://github.com/rey5137/SlideLayout

  8. Nginx Rewrite 实现匹配泛域名规则

    Nginx 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru ...

  9. Build类

    在开发中 我们有时候会需要获取当前手机的系统版本来进行判断,或者需要获取一些当前手机的硬件信息. android.os.Build类中.包括了这样的一些信息.我们可以直接调用 而不需要添加任何的权限和 ...

  10. Scala中的构造器

    Scala中的构造器 Scala中的构造器分为两类,主构造器和辅助构造器 主构造器是通过类名后面跟的括号里加参数列表来定义 辅助构造器是通过关键字this定义 定义一个无参主构造器 class rec ...