人脸识别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

转载于:https://www.cnblogs.com/jxust-jiege666/p/8576977.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MICROSOFT 基础类库: 人脸相似度检测MFC 项目概述 =============================================================================== 应用程序向导已为您创建了这个 人脸相似度检测MFC 应用程序。此应用程序不仅演示 Microsoft 基础类的基本使用方法,还可作为您编写应用程序的起点。 本文件概要介绍组成 人脸相似度检测MFC 应用程序的每个文件的内容。 人脸相似度检测MFC.vcproj 这是使用应用程序向导生成的 VC++ 项目的主项目文件。 它包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。 人脸相似度检测MFC.h 这是应用程序的主要头文件。它包括其他项目特定的头文件(包括 Resource.h),并声明 C人脸相似度检测MFCApp 应用程序类。 人脸相似度检测MFC.cpp 这是包含应用程序类 C人脸相似度检测MFCApp 的主要应用程序源文件。 人脸相似度检测MFC.rc 这是程序使用的所有 Microsoft Windows 资源的列表。它包括 RES 子目录中存储的图标、位图和光标。此文件可以直接在 Microsoft Visual C++ 中进行编辑。项目资源位于 2052 中。 res\人脸相似度检测MFC.ico 这是用作应用程序图标的图标文件。此图标包括在主要资源文件 人脸相似度检测MFC.rc 中。 res\MFC.rc2 此文件包含不在 Microsoft Visual C++ 中进行编辑的资源。您应该将不可由资源编辑器编辑的所有资源放在此文件中。 ///////////////////////////////////////////////////////////////////////////// 应用程序向导创建一个对话框类: 人脸相似度检测MFCDlg.h,人脸相似度检测MFCDlg.cpp - 对话框 这些文件包含 C人脸相似度检测MFCDlg 类。该类定义应用程序主对话框的行为。该对话框的模板位于 人脸相似度检测MFC.rc 中,该文件可以在 Microsoft Visual C++ 中进行编辑。 ///////////////////////////////////////////////////////////////////////////// 其他功能: ActiveX 控件 应用程序包括对使用 ActiveX 控件的支持。 ///////////////////////////////////////////////////////////////////////////// 其他标准文件: StdAfx.h,StdAfx.cpp 这些文件用于生成名为 人脸相似度检测MFC.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。 Resource.h 这是标准头文件,它定义新的资源 ID。 Microsoft Visual C++ 读取并更新此文件。 人脸相似度检测MFC.manifest 应用程序清单文件供 Windows XP 用来描述应用程序 对特定版本并行程序集的依赖性。加载程序使用此 信息从程序集缓存加载适当的程序集或 从应用程序加载私有信息。应用程序清单可能为了重新分发而作为 与应用程序可执行文件安装在相同文件夹中的外部 .manifest 文件包括, 也可能以资源的形式包括在该可执行文件中。 ///////////////////////////////////////////////////////////////////////////// 其他注释: 应用程序向导使用“TODO:”指示应添加或自定义的源代码部分。 如果应用程序在共享的 DLL 中使用 MFC,则需要重新发布这些 MFC DLL;如果应用程序所用的语言与操作系统的当前区域设置不同,则还需要重新发布对应的本地化资源 MFC90XXX.DLL。有关这两个主题的更多信息,请参见 MSDN 文档中有关 Redistributing Visual C++ applications (重新发布 Visual C++ 应用程序)的章节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值