基本信息
源码名称:人脸年龄识别
源码大小:1.53M
文件格式:.rar
开发语言:Java
更新时间:2015-07-06
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

最近How-Old很火呀,正好又看到了hongyang大大的视频,于是写了一个小demo,加入了一些自己的想法。

public class MainActivity extends Activity implements OnClickListener {
private ImageView iv_photo;
private Button btn_select, btn_take, btn_detect;
private TextView tv_age;
private String photoPath;
private Bitmap photo;
private ProgressDialog dialog;
private Paint paint;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0x1:// 检测成功
JSONObject result = (JSONObject) msg.obj;
handleResult(result);
iv_photo.setImageBitmap(photo);
break;
case 0x2:// 检测失败
String error = (String) msg.obj;
Toast.makeText(MainActivity.this, "检测失败:" error,
Toast.LENGTH_LONG).show();
break;
}
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
};
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

btn_detect = (Button) findViewById(R.id.btn_detect);
btn_select = (Button) findViewById(R.id.btn_select);
btn_take = (Button) findViewById(R.id.btn_take);
iv_photo = (ImageView) findViewById(R.id.iv_photo);
btn_detect.setOnClickListener(this);
btn_select.setOnClickListener(this);
btn_take.setOnClickListener(this);
tv_age = (TextView) findViewById(R.id.tv_age);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3);
}

protected void handleResult(JSONObject result) {
Bitmap bitmap = Bitmap.createBitmap(photo.getWidth(),
photo.getHeight(), photo.getConfig());
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(photo, 0, 0, null);
try {
JSONArray faces = result.getJSONArray("face");
int faceCount = faces.length();
if (faceCount == 0) {
Toast.makeText(this, "没有检测到人脸~", Toast.LENGTH_LONG).show();
} else {
for (int i = 0; i < faceCount; i ) {
JSONObject face = faces.getJSONObject(i);
JSONObject position = face.getJSONObject("position");
float cx = (float) position.getJSONObject("center")
.getDouble("x");
float cy = (float) position.getJSONObject("center")
.getDouble("y");
float width = (float) position.getDouble("width");
float height = (float) position.getDouble("height");
cx = cx / 100 * bitmap.getWidth();
cy = cy / 100 * bitmap.getHeight();
width = width / 100 * bitmap.getWidth();
height = height / 100 * bitmap.getHeight();

canvas.drawLine(cx - width / 2, cy - height / 2, cx - width
/ 2, cy height / 2, paint);
canvas.drawLine(cx - width / 2, cy - height / 2, cx width
/ 2, cy - height / 2, paint);
canvas.drawLine(cx width / 2, cy - height / 2, cx width
/ 2, cy height / 2, paint);
canvas.drawLine(cx - width / 2, cy height / 2, cx width
/ 2, cy height / 2, paint);

int age = face.getJSONObject("attribute")
.getJSONObject("age").getInt("value");
String gender = face.getJSONObject("attribute")
.getJSONObject("gender").getString("value");

Bitmap ageBm = buildAgeBm(age, gender);
int ageWidth = ageBm.getWidth();
int ageHeight = ageBm.getHeight();
if (bitmap.getWidth() < iv_photo.getWidth()
&& bitmap.getHeight() < iv_photo.getHeight()) {
float ratio = Math.max(bitmap.getWidth() * 1.0f
/ iv_photo.getWidth(), bitmap.getHeight()
* 1.0f / iv_photo.getWidth());
ageBm = Bitmap.createScaledBitmap(ageBm,
(int) (ageWidth * ratio),
(int) (ageHeight * ratio), false);
}
canvas.drawBitmap(ageBm, cx - ageBm.getWidth() / 2, cy
- height / 2 - ageBm.getHeight(), null);
photo = bitmap;
}
}
} catch (JSONException e) {
e.printStackTrace();
}

}

private Bitmap buildAgeBm(int age, String gender) {
tv_age.setText(age "");
if ("Female".equals(gender)) {
tv_age.setCompoundDrawablesWithIntrinsicBounds(getResources()
.getDrawable(R.drawable.female), null, null, null);
} else {
tv_age.setCompoundDrawablesWithIntrinsicBounds(getResources()
.getDrawable(R.drawable.male), null, null, null);
}
tv_age.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(tv_age.getDrawingCache());
tv_age.destroyDrawingCache();
return bm;
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_detect:
if (photo == null) {
btn_select.performClick();
} else {
dialog = ProgressDialog
.show(MainActivity.this, null, "正在检测...");
dialog.setCancelable(false);
Util.detect(photo, new Callback() {

@Override
public void onSuccess(JSONObject result) {
Message msg = Message.obtain();
msg.what = 0x1;
msg.obj = result;
mHandler.sendMessage(msg);
}

@Override
public void onFail(FaceppParseException e) {
Message msg = Message.obtain();
msg.what = 0x2;
msg.obj = e.getErrorMessage();
mHandler.sendMessage(msg);
}
});
}

break;
case R.id.btn_select:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 101);
break;
case R.id.btn_take:
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent getImageByCamera = new Intent(
"android.media.action.IMAGE_CAPTURE");
File dir = new File(Util.SaveDir);
if (!dir.exists()) {
dir.mkdirs();
}
photoPath = Util.SaveDir System.currentTimeMillis() ".jpg";
getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(photoPath)));
getImageByCamera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(getImageByCamera, 102);
System.out.println(photoPath);
} else {
Toast.makeText(getApplicationContext(), "请确认已经插入SD卡",
Toast.LENGTH_LONG).show();
}
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 101) {// 从相册中选取
if (data != null) {
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null,
null, null);
if (cursor.moveToFirst()) {
int index = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
photoPath = cursor.getString(index);
cursor.close();
getPhoto();
iv_photo.setImageBitmap(photo);
}
}
} else if (requestCode == 102) {// 拍照获取照片
getPhoto();
iv_photo.setImageBitmap(photo);
// Uri uri = data.getData();
// if (uri == null) {// 兼容机型的不同
// Bundle bundle = data.getExtras();
// if (bundle != null) {
// photo = (Bitmap) bundle.get("data");
// iv_photo.setImageBitmap(photo);
// }
// } else {
// Cursor cursor = getContentResolver().query(uri, null, null,
// null, null);
// if (cursor.moveToFirst()) {
// int index = cursor
// .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
// photoPath = cursor.getString(index);
// cursor.close();
// getPhoto();
// iv_photo.setImageBitmap(photo);
//
// }
// }
}
super.onActivityResult(requestCode, resultCode, data);
}

private void getPhoto() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photoPath, options);
options.inJustDecodeBounds = false;
float h = iv_photo.getHeight();
float w = iv_photo.getWidth();
// int ratio = 1;
double ratio = Math.max(options.outWidth * 1.0d / 1024f,
options.outHeight * 1.0d / 1024f);
// System.out.println(options.outWidth ":" options.outHeight "::"
// w ":" h);
// if (options.outWidth > options.outHeight && options.outWidth > w) {//
// 如果宽度大的话,则根据宽度缩放图片
// ratio = (int) (options.outWidth / w);
// } else if (options.outHeight > options.outWidth
// && options.outHeight > h) {
// ratio = (int) (options.outHeight / h);
// }
options.inSampleSize = (int) Math.ceil(ratio);
photo = BitmapFactory.decodeFile(photoPath, options);
}
}