刷脸

使用Face++第三方SDK。

首先新建一个布局 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/face_swiping_img"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    
    <TextView
        android:id="@+id/face_swiping_tv" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#ff0000"
        />

    <Button
        android:id="@+id/face_swiping_ok"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:paddingTop="10dp"
        android:text="识别" />

    <Button
        android:id="@+id/face_swiping_get_img"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:paddingTop="10dp"
        android:text="选择图片" />

</LinearLayout>


package com.example.faceswiping;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.faceswiping.FaceUlis.CallBack;
import com.facepp.error.FaceppParseException;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;

/**
 * @author xuming
 *
 */
public class MainActivity extends Activity implements OnClickListener {

	private int PICK_TYPE = 0X110;
	private ImageView img;  // 图片
	private Button ok, getImg;  // 按钮
	private TextView tv;   // 显示文字的
	private String imgUri;   // 图片的地址
	private Bitmap imgBitmap;  //图片的Bitmap

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			Log.e("", "hand=" + msg.obj);
		Log.e("", "a1="+msg.arg1);
			if (msg.arg1 == 1) {
				tv.setText("没有测到脸,请换张图");
			} else {
				tv.setText(String.valueOf(msg.obj));
			}
		}
	};

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

		initView();

		initclick();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == PICK_TYPE) {
			if (data != null) {
				Uri uri = data.getData();
				Cursor cursor = getContentResolver().query(uri, null, null,
						null, null);
				cursor.moveToFirst();
				int id = cursor
						.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
				imgUri = cursor.getString(id);
				cursor.close();

				resizePhoto();

				img.setImageBitmap(imgBitmap);
				Toast.makeText(MainActivity.this, "图片加载成功,请点识别",
						Toast.LENGTH_SHORT).show();

			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 压缩图片大小
	 */
	private void resizePhoto() {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(imgUri, opts);
		double ratio = Math.max(opts.outWidth * 1.0d / 1024f,
				opts.outHeight * 1d / 1024f);
		opts.inSampleSize = (int) Math.ceil(ratio);
		opts.inJustDecodeBounds = false;
		imgBitmap = BitmapFactory.decodeFile(imgUri, opts);
	}

	private void initclick() {
		ok.setOnClickListener(this);
		getImg.setOnClickListener(this);

	}

	private void initView() {
		img = (ImageView) findViewById(R.id.face_swiping_img);
		ok = (Button) findViewById(R.id.face_swiping_ok);
		getImg = (Button) findViewById(R.id.face_swiping_get_img);
		tv = (TextView) findViewById(R.id.face_swiping_tv);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.face_swiping_ok:
			FaceUlis.delete(imgBitmap, new CallBack() {

				@Override
				public void rejson(JSONObject json) {
					try {
						JSONArray array = json.getJSONArray("face");
						if(array.length() == 0){
							Message msg = new Message();
							msg.arg1 = 1;
							handler.sendMessage(msg);
						}
						JSONObject obj = array.getJSONObject(0);
							JSONObject attri = obj.getJSONObject("attribute");
							String age = attri.getJSONObject("age").getString(
									"value");
							String gender = attri.getJSONObject("gender")
									.getString("value");
							String race = attri.getJSONObject("race")
									.getString("value");

							Message msg = new Message();
							msg.obj = "年龄: " + age + "性别: " + gender + "地区: "
									+ race;
							handler.sendMessage(msg);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

				@Override
				public void error(FaceppParseException e) {
				}
			});

			break;
		case R.id.face_swiping_get_img:
			Intent intent = new Intent(Intent.ACTION_PICK);
			intent.setType("image/*");
			startActivityForResult(intent, PICK_TYPE);

			break;
		}

	}

}

package com.example.faceswiping;

import java.io.ByteArrayOutputStream;

import org.json.JSONObject;

import android.graphics.Bitmap;
import android.util.Log;

import com.facepp.error.FaceppParseException;
import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters;

/**
 * @author xuming
 *传入图片进行网络请求返回值
 */
public class FaceUlis {
	
	private static String apiKey ="4fa28d148bb06f0fc46a3d61592f3a4e";  //开发者KEY
	private static String apiSecret = "GbvsoXf14-VSLeCAEfKWTOkwedSXdZYI "; //开发者Secret
	
	public interface CallBack{
		void rejson(JSONObject json);
		
		void error(FaceppParseException e);
	}

	/**
	 * 
	 * @param bt 要查询的图片的Bitmap 
	 * @param callBack 返回值接口
	 */
	public static void delete(final Bitmap bt , final CallBack callBack ){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
				HttpRequests requests = new HttpRequests(apiKey, apiSecret, true, true);
				Bitmap btSmall = Bitmap.createBitmap(bt , 0, 0, bt.getWidth() , bt.getHeight());
				ByteArrayOutputStream stream = new ByteArrayOutputStream(); //把bitmap转成流
				btSmall.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
				byte [ ] arrays = stream.toByteArray(); //把流转成字节数组
				PostParameters params = new PostParameters();
				params.setImg(arrays);
				JSONObject	json = requests.detectionDetect(params);//读取返回值
				//Log.e("", ""+json);
				if(callBack != null){
					callBack.rejson(json);
				}
				} catch (FaceppParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					if(callBack != null){
						callBack.equals(e);
					}
				}
			}
		}).start();
	}
	
	
}

加入权限



    <!-- 往SDCard写入数据权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 从SDCard读取数据权限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值