您的位置:首页 > 移动开发

【Android】faceplusplusapp TEST

2016-08-25 10:58 211 查看
FacePlusPlus的博客

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.duanjiwei.faceplusplusapp">

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


package com.duanjiwei.faceplusplusapp;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.provider.MediaStore;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.database.Cursor;
import android.provider.MediaStore.Images.ImageColumns;
import com.faceplusplus.api.FaceDetecter;

public class MainActivity extends AppCompatActivity {
final private String TAG = "Face++";
final private int PICTURE_CHOOSE = 1;

private ImageView imageView = null;
private Bitmap img = null;
private Button buttonDetect = null;
private Button buttonFirst = null;
private TextView textView = null;
private TextView textViewDebug = null;

//-------------------
private Bitmap curBitmap = null;
HandlerThread detectThread = null;
Handler detectHandler = null;
FaceDetecter detecter = null;
//HttpRequests request = null;// 在线api
//--------------------------------------//

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

textView = (TextView)this.findViewById(R.id.textView1);
textViewDebug = (TextView)this.findViewById(R.id.textView2);

imageView = (ImageView)this.findViewById(R.id.imageView1);

buttonFirst = (Button)findViewById(R.id.button1);
buttonFirst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You Clicked Button1!", Toast.LENGTH_SHORT).show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, PICTURE_CHOOSE);
}
});

detectThread = new HandlerThread("detect");
detectThread.start();
detectHandler = new Handler(detectThread.getLooper());
detecter = new FaceDetecter();
detecter.init(this, "b3d7ad109e28fd45f261b7a43b7be8f2");  //账号Key

curBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.demo);  //加载测试图

buttonDetect = (Button)findViewById(R.id.button2);
buttonDetect.setVisibility(View.INVISIBLE);
buttonDetect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You Clicked Button2!", Toast.LENGTH_SHORT).show();
textView.setText("Waiting ...");

//-------------------添加人脸识别算法-----------------------
detectHandler.post(new Runnable() {
@Override
public void run() {
FaceDetecter.Face[] faceinfo = detecter.findFaces(img);// 进行人脸检测

if (faceinfo == null)
{
textView.setText("Waiting faceinfo ...");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "未发现人脸信息", Toast.LENGTH_LONG).show();
}
});
return;
}

final Bitmap bit = getFaceInfoBitmap(faceinfo, img);
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bit);
System.gc();
textView.setText("Waiting runOnUiThread ...");
}
});
}
});

//--------------------------------------------------------------//
}
});//buttonDetect.setOnClickListener

}//Create

public static Bitmap getFaceInfoBitmap(FaceDetecter.Face[] faceinfos, Bitmap oribitmap) {
Bitmap tmp;
tmp = oribitmap.copy(Bitmap.Config.ARGB_8888, true);

Canvas localCanvas = new Canvas(tmp);
Paint localPaint = new Paint();
localPaint.setColor(0xffff0000);
localPaint.setStyle(Paint.Style.STROKE);
for (FaceDetecter.Face localFaceInfo : faceinfos) {
RectF rect = new RectF(oribitmap.getWidth() * localFaceInfo.left, oribitmap.getHeight()
* localFaceInfo.top, oribitmap.getWidth() * localFaceInfo.right,
oribitmap.getHeight()
* localFaceInfo.bottom);
localCanvas.drawRect(rect, localPaint);
}
return tmp;
}

//缩放图片大小
public static Bitmap getScaledBitmap(Bitmap m_img, float sx, float sy) {
Matrix matrix = new Matrix();
matrix.postScale(sx, sy);
Bitmap rst = Bitmap.createBitmap(m_img, 0, 0, m_img.getWidth(), m_img.getHeight(), matrix, true);
return rst;
}

//保存图片
public static void saveBitmap(Bitmap bm, String picName) {
File f = new File("/storage/emulated/0/Pictures/", picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
protected void onDestroy(){
super.onDestroy();
detecter.release(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

if (requestCode == PICTURE_CHOOSE) {
if (intent != null) {
try {
//加载图片并显示
ContentResolver resolver = getContentResolver();
Uri originalUri = intent.getData();//获得图片的uri
Bitmap imgTemp = MediaStore.Images.Media.getBitmap(resolver, originalUri);
img = getScaledBitmap(imgTemp, 0.3f, 0.3f);   //缩小图片
imageView.setImageBitmap(img);

//获取图片的路径
Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
textViewDebug.setText("Picture:" + fileSrc);

textView.setText("图片加载成功!");
buttonDetect.setVisibility(View.VISIBLE);

saveBitmap(img, "MyScaleImg.png");  //保存图片
}catch (IOException e) {
textView.setText("TAG-->Error" + e.toString());
}
}
else {
textViewDebug.setText("intent == null");
}
}
}//onActivityResult()

}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.duanjiwei.faceplusplusapp.MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载图片"
android:id="@+id/button1"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人脸识别"
android:id="@+id/button2"
android:layout_alignTop="@+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NewTextView"
android:id="@+id/textView1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NewTextDebug"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: