不要让昨天 占据你的今天 夏午晴天

夏午晴天

人脸识别1:1对比 (二)

本项目采用Face++第三方接口,项目实现了两张图片的人脸识别和对比,得到相似度等信息

项目步骤如下:

一、所需权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

二、第三方app id app key

项目采用了Face++第三方接口,可自行注册获取

三、添加依赖和sdk

1. 将FaceppSDK.jar文件放到libs路径下。

2. gradle中添加一下依赖:

compile files('libs/FaceppSDK.jar')

四、布局文件

  

Face++ 1:1对比

页面两个ImageView 点击选择本地图片,button点击比对,比对结果在下方TextView 展示

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <ScrollView
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context="com.example.lifen.facecomparedemo.MainActivity">
12 
13     <LinearLayout
14         android:layout_width="match_parent"
15         android:layout_height="match_parent"
16         android:orientation="vertical">
17 
18         <LinearLayout
19             android:layout_width="match_parent"
20             android:layout_height="wrap_content"
21             android:orientation="horizontal">
22 
23             <ImageView
24                 android:id="@+id/img1"
25                 android:layout_width="0dp"
26                 android:layout_height="180dp"
27                 android:layout_weight="1"
28                 android:scaleType="centerCrop"
29                 android:src="@drawable/head"/>
30 
31             <TextView
32                 android:layout_width="wrap_content"
33                 android:layout_height="match_parent"
34                 android:gravity="center"
35                 android:text="VS"
36                 android:textColor="@android:color/black"
37                 android:textSize="20dp"/>
38 
39             <ImageView
40                 android:id="@+id/img2"
41                 android:layout_width="0dp"
42                 android:layout_height="180dp"
43                 android:layout_weight="1"
44                 android:scaleType="centerCrop"
45                 android:src="@drawable/head"/>
46 
47         </LinearLayout>
48 
49         <Button
50             android:id="@+id/compareBtn"
51             android:layout_width="match_parent"
52             android:layout_height="wrap_content"
53             android:layout_marginTop="@dimen/activity_horizontal_margin"
54             android:text="比对"/>
55 
56         <TextView
57             android:id="@+id/resultBtn"
58             android:layout_width="match_parent"
59             android:layout_height="wrap_content"
60             android:layout_marginTop="@dimen/activity_horizontal_margin"
61             android:background="#eeeeee"
62             android:padding="6dp"/>
63 
64     </LinearLayout>
65 </ScrollView>
View Code

五、页面 activity

  1 package com.example.lifen.facecomparedemo;
  2 
  3 import android.content.ContentResolver;
  4 import android.content.Intent;
  5 import android.graphics.Bitmap;
  6 import android.graphics.BitmapFactory;
  7 import android.net.Uri;
  8 import android.os.Bundle;
  9 import android.os.Handler;
 10 import android.os.Message;
 11 import android.support.v7.app.AlertDialog;
 12 import android.support.v7.app.AppCompatActivity;
 13 import android.text.TextUtils;
 14 import android.util.Base64;
 15 import android.util.Log;
 16 import android.view.View;
 17 import android.widget.Button;
 18 import android.widget.ImageView;
 19 import android.widget.TextView;
 20 import android.widget.Toast;
 21 
 22 import com.megvii.cloud.http.CommonOperate;
 23 import com.megvii.cloud.http.Response;
 24 
 25 import java.io.ByteArrayOutputStream;
 26 import java.io.FileNotFoundException;
 27 
 28 /**
 29  * 人脸对比 1:1
 30  *
 31  * @author LiFen
 32  */
 33 public class MainActivity extends AppCompatActivity {
 34     private static final String TAG = "MainActivity";
 35     private static final int REQUEST_CODE1 = 11;
 36     private static final int REQUEST_CODE2 = 12;
 37     ImageView mImageView1;
 38     ImageView mImageView2;
 39     Button mCompareBtn;
 40     TextView mResultText;
 41     private String mImgBase641;
 42     private String mImgBase642;
 43     String key = "";//api_key
 44     String secret = "";//api_secret
 45     private final static int i = 100;
 46     private Handler handler = new Handler(){
 47         @Override
 48         public void handleMessage(Message msg) {
 49             if(msg.what == i){
 50                 mResultText.setText((String)msg.obj);
 51             }
 52         }
 53     };
 54     @Override
 55     protected void onCreate(Bundle savedInstanceState) {
 56         super.onCreate(savedInstanceState);
 57         setContentView(R.layout.activity_main);
 58 
 59         mImageView1 = (ImageView) findViewById(R.id.img1);
 60         mImageView2 = (ImageView) findViewById(R.id.img2);
 61         mCompareBtn = (Button) findViewById(R.id.compareBtn);
 62         mResultText = (TextView) findViewById(R.id.resultBtn);
 63         if(TextUtils.isEmpty(key) || TextUtils.isEmpty(secret)){
 64             AlertDialog.Builder builder = new AlertDialog.Builder(this);
 65             builder.setMessage("please enter key and secret");
 66             builder.setTitle("");
 67             builder.show();
 68             return;
 69         }
 70         mImageView1.setOnClickListener(new View.OnClickListener() {
 71             @Override
 72             public void onClick(View v) {
 73                 startAlbumActivity(REQUEST_CODE1);
 74             }
 75         });
 76         mImageView2.setOnClickListener(new View.OnClickListener() {
 77             @Override
 78             public void onClick(View v) {
 79                 startAlbumActivity(REQUEST_CODE2);
 80             }
 81         });
 82         mCompareBtn.setOnClickListener(new View.OnClickListener() {
 83             @Override
 84             public void onClick(View v) {
 85                 startCompare();
 86             }
 87         });
 88     }
 89 
 90     private void startCompare() {
 91         if ("".equals(mImgBase641) || mImgBase641 == null || "".equals(mImgBase642) || mImgBase642 == null) {
 92             Toast.makeText(this, "请选择图片再比对", Toast.LENGTH_SHORT).show();
 93             return;
 94         }
 95         mResultText.setText("比对中...");
 96         new Thread(new Runnable() {
 97             @Override
 98             public void run() {
 99                 Log.i(TAG, "run() called");
100                 CommonOperate commonOperate = new CommonOperate(key, secret, false);
101                 try{
102                     Response compare = commonOperate.compare(null, null, null, mImgBase641,
103                             null, null, null, mImgBase642);
104                     String res = new String(compare.getContent());
105                     Message msg = new Message();
106                     msg.what = i;
107                     msg.obj = res;
108                     handler.sendMessage(msg);
109                 }catch (Exception e){
110                     Log.i(TAG, "startCompare: " +e.toString());
111                 }
112             }
113         }).start();
114     }
115 
116     private void startAlbumActivity(int requestCode) {
117         Intent intent = new Intent();
118         intent.setType("image/*");
119         intent.setAction(Intent.ACTION_GET_CONTENT);
120         startActivityForResult(intent, requestCode);
121     }
122 
123     @Override
124     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
125         if (data == null)
126             return;
127         Uri uri = data.getData();
128         Log.e("uri", uri.toString());
129         ContentResolver cr = this.getContentResolver();
130         Bitmap bitmap = null;
131         try {
132             bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
133                 /* 将Bitmap设定到ImageView */
134         } catch (FileNotFoundException e) {
135             Log.e("Exception", e.getMessage(), e);
136         }
137         if (resultCode == RESULT_OK && requestCode == REQUEST_CODE1) {
138             mImageView1.setImageBitmap(bitmap);
139             mImgBase641 = bitmapToBase64(bitmap);
140         } else if (resultCode == RESULT_OK && requestCode == REQUEST_CODE2) {
141             mImageView2.setImageBitmap(bitmap);
142             mImgBase642 = bitmapToBase64(bitmap);
143         }
144         super.onActivityResult(requestCode, resultCode, data);
145     }
146 
147     private String bitmapToBase64(Bitmap bitmap) {
148         ByteArrayOutputStream bStream = new ByteArrayOutputStream();
149         bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bStream);
150         return Base64.encodeToString(bStream.toByteArray(), 0);
151     }
152 }
View Code

代码中

key 和 secret 请自行注册,填入

 

项目源码地址:https://download.csdn.net/download/qq_36726507/10290076

posted on 2018-03-16 11:35  夏晴天  阅读(706)  评论(0编辑  收藏

导航

统计

Live2D