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

夏午晴天

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

本项目采用了eyekey 第三方接口,实现了自选图片人脸识别和 两张图片的1:1对比,可返回比对相似度信息,具体步骤如下:

一、所需权限

<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

<meta-data android:name="appkey" android:value=""/>
<meta-data android:name="appid" android:value=""/>

本应用采用的eyekey 第三方接口,可自行去注册,以上代码放到配置文件中

三、添加依赖

    compile "com.squareup.retrofit2:retrofit:2.1.0"
    compile "com.squareup.retrofit2:converter-gson:2.1.0"

retrofit使用参见:https://www.jianshu.com/p/308f3c54abdd

 导入sdk,如图放入文件路径下:

 

四、布局文件

1:1对比

 页面两个ImageView 一个button 一个textView,点击imageView 选择本地图片

 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.facecompareeyekey.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.facecompareeyekey;
  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.support.v7.app.AppCompatActivity;
 10 import android.util.Base64;
 11 import android.util.Log;
 12 import android.view.View;
 13 import android.widget.Button;
 14 import android.widget.ImageView;
 15 import android.widget.TextView;
 16 import android.widget.Toast;
 17 
 18 import com.example.lifen.facecompareeyekey.eyekeysdk.api.CheckAPI;
 19 import com.example.lifen.facecompareeyekey.eyekeysdk.entity.FaceAttrs;
 20 import com.example.lifen.facecompareeyekey.eyekeysdk.entity.MatchCompare;
 21 
 22 import java.io.ByteArrayOutputStream;
 23 import java.io.FileNotFoundException;
 24 
 25 import retrofit2.Call;
 26 import retrofit2.Callback;
 27 import retrofit2.Response;
 28 
 29 /**
 30  * eyekey人脸比对示例(matchCompare)
 31  *
 32  * @author wangzhi
 33  */
 34 public class MainActivity extends AppCompatActivity {
 35 
 36   private static final int REQUEST_CODE1 = 11;
 37   private static final int REQUEST_CODE2 = 12;
 38   ImageView mImageView1;
 39   ImageView mImageView2;
 40   Button mCompareBtn;
 41   TextView mResultText;
 42   private String mFaceId1;
 43   private String mFaceId2;
 44   private String mImgBase641;
 45   private String mImgBase642;
 46 
 47   @Override
 48   protected void onCreate(Bundle savedInstanceState) {
 49     super.onCreate(savedInstanceState);
 50     setContentView(R.layout.activity_main);
 51 
 52     // 初始化eyekey接口 (需在AndroidManifest.xml中添加appid和appkey)
 53     CheckAPI.init(this);
 54 
 55     mImageView1 = (ImageView) findViewById(R.id.img1);
 56     mImageView2 = (ImageView) findViewById(R.id.img2);
 57     mCompareBtn = (Button) findViewById(R.id.compareBtn);
 58     mResultText = (TextView) findViewById(R.id.resultBtn);
 59 
 60     mImageView1.setOnClickListener(new View.OnClickListener() {
 61       @Override
 62       public void onClick(View v) {
 63         startAlbumActivity(REQUEST_CODE1);
 64       }
 65     });
 66     mImageView2.setOnClickListener(new View.OnClickListener() {
 67       @Override
 68       public void onClick(View v) {
 69         startAlbumActivity(REQUEST_CODE2);
 70       }
 71     });
 72     mCompareBtn.setOnClickListener(new View.OnClickListener() {
 73       @Override
 74       public void onClick(View v) {
 75         startCompare();
 76       }
 77     });
 78   }
 79 
 80   private void startCompare() {
 81     if ("".equals(mImgBase641) || mImgBase641 == null || "".equals(mImgBase642) || mImgBase642 == null) {
 82       Toast.makeText(this, "请选择图片再比对", Toast.LENGTH_SHORT).show();
 83       return;
 84     }
 85     mResultText.setText("比对中...");
 86     getFaceId1();
 87   }
 88 
 89   void getFaceId1() {
 90     CheckAPI.checkingImageData(mImgBase641, null, null).enqueue(new Callback<FaceAttrs>() {
 91       @Override
 92       public void onResponse(Call<FaceAttrs> call, Response<FaceAttrs> response) {
 93         FaceAttrs faceAttrs = response.body();
 94         if (faceAttrs != null && "0000".equals(faceAttrs.getRes_code())) {
 95           mFaceId1 = faceAttrs.getFace().get(0).getFace_id();
 96           getFaceId2();
 97         } else {
 98           mResultText.setText("人脸检测失败...");
 99         }
100       }
101 
102       @Override
103       public void onFailure(Call<FaceAttrs> call, Throwable t) {
104         mResultText.setText("网络出错...");
105         Toast.makeText(MainActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
106       }
107     });
108   }
109 
110   void getFaceId2() {
111     CheckAPI.checkingImageData(mImgBase642, null, null).enqueue(new Callback<FaceAttrs>() {
112       @Override
113       public void onResponse(Call<FaceAttrs> call, Response<FaceAttrs> response) {
114         FaceAttrs faceAttrs = response.body();
115         if (faceAttrs != null && "0000".equals(faceAttrs.getRes_code())) {
116           mFaceId2 = faceAttrs.getFace().get(0).getFace_id();
117           compare();
118         } else {
119           mResultText.setText("人脸检测失败...");
120         }
121       }
122 
123       @Override
124       public void onFailure(Call<FaceAttrs> call, Throwable t) {
125         mResultText.setText("网络出错...");
126         Toast.makeText(MainActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
127       }
128     });
129   }
130 
131   void compare() {
132     CheckAPI.matchCompare(mFaceId1, mFaceId2).enqueue(new Callback<MatchCompare>() {
133       @Override
134       public void onResponse(Call<MatchCompare> call, Response<MatchCompare> response) {
135         MatchCompare compare = response.body();
136         if (compare != null && "0000".equals(compare.getRes_code())) {
137           mResultText.setText(compare.toString());
138         } else {
139           mResultText.setText("比对失败...");
140         }
141       }
142 
143       @Override
144       public void onFailure(Call<MatchCompare> call, Throwable t) {
145         mResultText.setText("网络出错...");
146         Toast.makeText(MainActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
147       }
148     });
149   }
150 
151   private void startAlbumActivity(int requestCode) {
152     Intent intent = new Intent();
153     intent.setType("image/*");
154     intent.setAction(Intent.ACTION_GET_CONTENT);
155     startActivityForResult(intent, requestCode);
156   }
157 
158   @Override
159   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
160     if (data == null)
161       return;
162     Uri uri = data.getData();
163     Log.e("uri", uri.toString());
164     ContentResolver cr = this.getContentResolver();
165     Bitmap bitmap = null;
166     try {
167       bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
168                 /* 将Bitmap设定到ImageView */
169     } catch (FileNotFoundException e) {
170       Log.e("Exception", e.getMessage(), e);
171     }
172     if (resultCode == RESULT_OK && requestCode == REQUEST_CODE1) {
173       mImageView1.setImageBitmap(bitmap);
174       mImgBase641 = bitmapToBase64(bitmap);
175     } else if (resultCode == RESULT_OK && requestCode == REQUEST_CODE2) {
176       mImageView2.setImageBitmap(bitmap);
177       mImgBase642 = bitmapToBase64(bitmap);
178     }
179     super.onActivityResult(requestCode, resultCode, data);
180   }
181 
182   private String bitmapToBase64(Bitmap bitmap) {
183     ByteArrayOutputStream bStream = new ByteArrayOutputStream();
184     bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bStream);
185     return Base64.encodeToString(bStream.toByteArray(), 0);
186   }
187 }
View Code

项目地址:https://download.csdn.net/download/qq_36726507/10289440

posted on 2018-03-15 22:50  夏晴天  阅读(1426)  评论(0编辑  收藏

导航

统计

Live2D