package com.safeluck.facedetect.mycamera;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.FaceDetector;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.safeluck.facedetect.mycamera.cpp.CustomInterface;
import com.safeluck.facedetect.mycamera.cpp.MyInterface;
import com.safeluck.facedetect.mycamera.cpp.SumDemo;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* aaa
* Created by lzw on 2018/5/25. 13:17:31
* 邮箱:632393724@qq.com
* All Rights Saved! Chongqing AnYun Tech co. LTD
*/
public class SecondActivity extends Activity implements MyInterface {
private static final int REQUECT_CODE_SEL_PIC = 120;
private static final int MAX_FACES = 4;
private Bitmap bm;
@BindView(R.id.iv) ImageView image;
private boolean hasDetected = false; //标记是否检测到人脸
private ProgressDialog progressDialog;
private Paint paint;
private int i = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
ButterKnife.bind(this);
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("提示");
progressDialog.setMessage("正在检测,请稍等");
initPaint();
}
private void initPaint() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(2);
//设置画出的是空心方框而不是实心
paint.setStyle(Paint.Style.STROKE);
}
@OnClick({R.id.btn_select,R.id.btn_detect})
public void clickDDemo(View view) {
switch (view.getId()) {
case R.id.btn_detect:
detectFace();
getIpWhere("123.145.106.87");
break;
case R.id.btn_select:
// selectPicture();
if (i<3){
image.setImageLevel(i++);
}else{
i=0;
}
SumDemo sumDemo = new SumDemo();
sumDemo.CallRet(this);
// Toast.makeText(this, SumDemo.helloFromJni(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
private void detectFace() {
if (bm == null) {
Toast.makeText(this, R.string.pls_select, Toast.LENGTH_SHORT).show();
return;
}
if (hasDetected) {
Toast.makeText(this, R.string.has_detected, Toast.LENGTH_SHORT).show();
return;
}
new futureTask().execute();
}
@Override
public void ShowMsg(String msg) {
Log.i(TAG,"收到回调:"+msg);
}
class futureTask extends AsyncTask {
@Override
protected FaceDetector.Face[] doInBackground(Void... voids) {
FaceDetector faceDetector = new FaceDetector(bm.getWidth(), bm.getHeight(), MAX_FACES);
FaceDetector.Face[] faces = new FaceDetector.Face[MAX_FACES];
faceDetector.findFaces(bm, faces);
if (faces.length > 0) {
return faces;
}else{
return null;
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
@Override
protected void onPostExecute(FaceDetector.Face[] faces) {
super.onPostExecute(faces);
progressDialog.dismiss();
if (faces == null){
Toast.makeText(SecondActivity.this, R.string.sorry,Toast.LENGTH_SHORT).show();
}else{
drawFacesArea(faces);
}
}
}
private void drawFacesArea(FaceDetector.Face[] faces) {
Canvas canvas = new Canvas(bm);
float eyesDistance = 0f;
for (int i=0; i
FaceDetector.Face face = faces[i];
if (face != null){
PointF pointF = new PointF();
face.getMidPoint(pointF);
eyesDistance = face.eyesDistance();
canvas.drawRect(pointF.x - eyesDistance,pointF.y-eyesDistance,
pointF.x+eyesDistance,pointF.y+eyesDistance,paint);
}
}
}
private static final String TAG = "SecondActivity";
}