首页 > 移动平台 > 详细

安卓人人脸识别部分代码

时间:2016-01-15 14:33:25      阅读:201      评论:0      收藏:0      [点我收藏+]
技术分享
package cn.facecore.facecoredemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.facecore.facecoredemo.R;

public class MainActivity extends Activity implements OnClickListener {

    private Button detectButton;
    private Button compareButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        detectButton = (Button) findViewById(R.id.button_detect);
        compareButton = (Button) findViewById(R.id.button_compare);
        compareButton.setOnClickListener(this);
        detectButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
        case R.id.button_compare:
            startActivity(new Intent(this, CompareActivity.class));
            break;
        case R.id.button_detect:
            startActivity(new Intent(this, DetectActivity.class));
            break;
        default:
            break;
        }
    }

}
View Code
技术分享
  1 package cn.facecore.facecoredemo;
  2 
  3 import java.io.IOException;
  4 
  5 import com.alibaba.fastjson.JSON;
  6 
  7 import cn.facecore.facecoredemo.comm.BitmapUtil;
  8 import cn.facecore.facecoredemo.comm.Const;
  9 import cn.facecore.facecoredemo.entity.FaceDetectRequest;
 10 import cn.facecore.facecoredemo.entity.FaceDetectResult;
 11 import cn.facecore.facecoredemo.restfulclient.Client;
 12 import android.content.Intent;
 13 import android.database.Cursor;
 14 import android.graphics.Bitmap;
 15 import android.net.Uri;
 16 import android.os.Bundle;
 17 import android.os.Handler;
 18 import android.os.Message;
 19 import android.provider.MediaStore;
 20 import android.view.View;
 21 import android.view.View.OnClickListener;
 22 import android.widget.Button;
 23 import android.widget.ImageView;
 24 import android.widget.TextView;
 25 
 26 public class DetectActivity extends BasicActivity implements OnClickListener {
 27     private ImageView myPhoto;
 28     private Button selectBT;
 29     private Button compareBT;
 30     private Bitmap resbitmap;
 31     private TextView valueTV;
 32     private MyHandler myHandler;
 33     private String value;
 34     private String s;
 35 
 36     class MyHandler extends Handler {
 37         @Override
 38         public void handleMessage(Message msg) {
 39             // TODO Auto-generated method stub
 40             super.handleMessage(msg);
 41             FaceDetectResult parseObject = JSON.parseObject(value,
 42                     FaceDetectResult.class);
 43             System.out.println(parseObject.toString());
 44             valueTV.setText(parseObject.toString());
 45         }
 46     };
 47 
 48     @Override
 49     protected void onCreate(Bundle savedInstanceState) {
 50         super.onCreate(savedInstanceState);
 51         setContentView(R.layout.activity_detect);
 52         init();
 53     }
 54 
 55     private void init() {
 56         // TODO Auto-generated method stub
 57         myHandler = new MyHandler();
 58         myPhoto = (ImageView) findViewById(R.id.my_photo);
 59         selectBT = (Button) findViewById(R.id.button_select);
 60         compareBT = (Button) findViewById(R.id.button_compare);
 61         valueTV = (TextView) findViewById(R.id.textView_value);
 62         compareBT.setOnClickListener(this);
 63         selectBT.setOnClickListener(this);
 64     }
 65 
 66     @Override
 67     public void onClick(View arg0) {
 68         // TODO Auto-generated method stub
 69         switch (arg0.getId()) {
 70         case R.id.button_compare:
 71             valueTV.setText("");
 72             new Thread() {
 73                 public void run() {
 74                     // 实例化RESTFul客户端
 75                     Client client = new Client();
 76                     // 请求数据
 77                     try {
 78                         value = client.PostMethod(Const.FaceDeteiveUrl, s);
 79                         myHandler.sendEmptyMessage(1);
 80                     } catch (IOException e) {
 81                         // TODO Auto-generated catch block
 82                         e.printStackTrace();
 83                     }
 84 
 85                 }
 86             }.start();
 87 
 88             break;
 89         case R.id.button_select:
 90             show();
 91             break;
 92         default:
 93             break;
 94         }
 95     }
 96 
 97     @Override
 98     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 99         // TODO Auto-generated method stub
100         if (requestCode == 0) {
101             if (resultCode == -1) {
102                 resbitmap = BitmapUtil.saveBitmap(photo_path, photoFile);
103             }
104         } else if (requestCode == 1) {
105             try {
106                 Uri selectedImage = data.getData();
107                 String[] filePathColumn = { MediaStore.Images.Media.DATA };
108                 Cursor cursor = getContentResolver().query(selectedImage,
109                         filePathColumn, null, null, null);
110                 cursor.moveToFirst();
111                 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
112                 String picturePath = cursor.getString(columnIndex);
113                 cursor.close();
114                 resbitmap = BitmapUtil.getSmallBitmap(picturePath);
115             } catch (Exception e) {
116             }
117         }
118         if (resbitmap != null) {
119             myPhoto.setImageBitmap(resbitmap);
120             // 图片Base64编码
121             String faceImageBase64Str = BitmapUtil.bitmaptoString(resbitmap);
122             // 创建人脸检测对象
123             FaceDetectRequest faceDetectRequest = new FaceDetectRequest();
124             faceDetectRequest.setFaceImage(faceImageBase64Str);
125             // 序列化人脸检测对象
126             s = JSON.toJSONString(faceDetectRequest);
127         }
128     }
129 }
View Code
技术分享
  1 package cn.facecore.facecoredemo;
  2 
  3 import java.io.IOException;
  4 
  5 import com.alibaba.fastjson.JSON;
  6 
  7 import cn.facecore.facecoredemo.comm.BitmapUtil;
  8 import cn.facecore.facecoredemo.comm.Const;
  9 import cn.facecore.facecoredemo.entity.FaceCompareRequest;
 10 import cn.facecore.facecoredemo.entity.FaceCompareResult;
 11 import cn.facecore.facecoredemo.restfulclient.Client;
 12 
 13 import android.net.Uri;
 14 import android.os.Bundle;
 15 import android.os.Handler;
 16 import android.os.Message;
 17 import android.provider.MediaStore;
 18 import android.content.Intent;
 19 import android.database.Cursor;
 20 import android.graphics.Bitmap;
 21 import android.view.View;
 22 import android.view.View.OnClickListener;
 23 import android.widget.Button;
 24 import android.widget.ImageView;
 25 import android.widget.TextView;
 26 
 27 public class CompareActivity extends BasicActivity implements OnClickListener {
 28     private ImageView photo1;
 29     private ImageView photo2;
 30     private TextView valueTV;
 31     private Button select1BT;
 32     private Button select2BT;
 33     private Button compareBT;
 34     private String value;
 35     private String s;
 36     private MyHandler myHandler;
 37     private Bitmap resbitmap;
 38     private int select;
 39     private FaceCompareRequest faceCompareRequest;
 40 
 41     class MyHandler extends Handler {
 42         @Override
 43         public void handleMessage(Message msg) {
 44             // TODO Auto-generated method stub
 45             super.handleMessage(msg);
 46             FaceCompareResult parseObject = JSON.parseObject(value,
 47                     FaceCompareResult.class);
 48             valueTV.setText(parseObject.toString());
 49         }
 50     };
 51 
 52     @Override
 53     protected void onCreate(Bundle savedInstanceState) {
 54         super.onCreate(savedInstanceState);
 55         setContentView(R.layout.activity_compare);
 56         initView();
 57     }
 58 
 59     private void initView() {
 60         // TODO Auto-generated method stub
 61         myHandler = new MyHandler();
 62         faceCompareRequest = new FaceCompareRequest();
 63         photo1 = (ImageView) findViewById(R.id.photo_1);
 64         photo2 = (ImageView) findViewById(R.id.photo_2);
 65         valueTV = (TextView) findViewById(R.id.textView_value);
 66         select1BT = (Button) findViewById(R.id.button_select_1);
 67         select2BT = (Button) findViewById(R.id.button_select_2);
 68         compareBT = (Button) findViewById(R.id.button_compare);
 69         select1BT.setOnClickListener(this);
 70         select2BT.setOnClickListener(this);
 71         compareBT.setOnClickListener(this);
 72     }
 73 
 74     @Override
 75     public void onClick(View arg0) {
 76         // TODO Auto-generated method stub
 77         switch (arg0.getId()) {
 78         case R.id.button_select_1:
 79         case R.id.button_select_2:
 80             show();
 81             if (arg0.getId() == R.id.button_select_1) {
 82                 select = 0;
 83             } else if (arg0.getId() == R.id.button_select_2) {
 84                 select = 1;
 85             }
 86             break;
 87         case R.id.button_compare:
 88             valueTV.setText("");
 89             new Thread() {
 90 
 91                 public void run() {
 92                     // 实例化RESTFul客户端
 93                     Client client = new Client();
 94                     // 请求数据
 95                     try {
 96                         value = client.PostMethod(Const.FaceCompareUrl, s);
 97                         myHandler.sendEmptyMessage(1);
 98                     } catch (IOException e) {
 99                         // TODO Auto-generated catch block
100                         e.printStackTrace();
101                     }
102                 }
103             }.start();
104             break;
105         default:
106             break;
107         }
108     }
109 
110     @Override
111     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
112         // TODO Auto-generated method stub
113         if (requestCode == 0) {
114             if (resultCode == -1) {
115                 resbitmap = BitmapUtil.saveBitmap(photo_path, photoFile);
116             }
117         } else if (requestCode == 1) {
118             try {
119                 Uri selectedImage = data.getData();
120                 String[] filePathColumn = { MediaStore.Images.Media.DATA };
121                 Cursor cursor = getContentResolver().query(selectedImage,
122                         filePathColumn, null, null, null);
123                 cursor.moveToFirst();
124                 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
125                 String picturePath = cursor.getString(columnIndex);
126                 cursor.close();
127                 resbitmap = BitmapUtil.getSmallBitmap(picturePath);
128             } catch (Exception e) {
129             }
130         }
131         if (resbitmap != null) {
132             // 图片Base64编码
133             String faceImageBase64Str = BitmapUtil.bitmaptoString(resbitmap);
134             // 创建人脸检测对象
135             if (select == 0) {
136                 photo1.setImageBitmap(resbitmap);
137                 faceCompareRequest.setFaceimage1(faceImageBase64Str);
138             } else {
139                 photo2.setImageBitmap(resbitmap);
140                 faceCompareRequest.setFaceimage2(faceImageBase64Str);
141             }
142             // 序列化人脸检测对象
143             s = JSON.toJSONString(faceCompareRequest);
144         }
145     }
146 }
View Code
技术分享
 1 package cn.facecore.facecoredemo;
 2 
 3 import java.io.File;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 import android.app.Activity;
 8 import android.app.AlertDialog;
 9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.net.Uri;
12 import android.os.Environment;
13 import android.provider.MediaStore;
14 import android.widget.Toast;
15 
16 /**
17  * @author Administrator
18  * 
19  */
20 public class BasicActivity extends Activity {
21 
22     private File dir;
23     protected String photo_path;
24     protected File photoFile;
25     
26     /**
27      *方便所有的activity调用 AlertDialog
28      */
29     public void show() {
30         // TODO Auto-generated method stub
31         final AlertDialog.Builder builder = new AlertDialog.Builder(
32                 BasicActivity.this);
33         builder.setTitle("选择照片");
34         builder.setPositiveButton("相机", new DialogInterface.OnClickListener() {
35             @Override
36             public void onClick(DialogInterface
37 
38             dialog, int which) {
39                 takeFromCamera();
40             }
41         });
42         builder.setNegativeButton("相册", new DialogInterface.OnClickListener() {
43             @Override
44             public void onClick(DialogInterface
45 
46             dialog, int which) {
47                 // 跳转到相册
48                 Intent intent = new Intent(
49                         Intent.ACTION_PICK,
50                         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
51                 startActivityForResult(intent, 1);
52 
53             }
54         });
55         AlertDialog alert = builder.create();
56         alert.show();
57     }
58 
59     /**
60      * 相机获得照片
61      */
62     private void takeFromCamera() {
63         if (Environment.getExternalStorageState().equals(
64                 Environment.MEDIA_MOUNTED)) {
65             dir = new File(Environment.getExternalStorageDirectory()
66                     + "/ChinaFace/Camera");
67             if (!dir.exists()) {
68                 dir.mkdirs();
69             }
70             photo_path = dir + "/"
71                     + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
72                     + ".jpg";
73             photoFile = new File(photo_path);
74             Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
75             intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
76             startActivityForResult(intent, 0);
77         } else {
78             Toast.makeText(this, "SD卡不存在,无法启动相机", Toast.LENGTH_SHORT).show();
79         }
80     }
81 
82 }
View Code

 

安卓人人脸识别部分代码

原文:http://www.cnblogs.com/yuhuameng/p/5133173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!