1.工程的配置比较简单,导入jar包,然后添加如下权限就行,
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
首先制作简易的界面,一个ImageView,两个Button.
2.点击get按钮,进入到相册获取要检测图片.代码如下:
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(intent, choiceCode);
3.得到点击的照片.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==choiceCode){
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
Cursor cursor=cr.query(uri, null, null, null, null);
// try {
// Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
// /* 将Bitmap设定到ImageView */
// showimg.setImageBitmap(bitmap);
// } catch (FileNotFoundException e) {
// Log.e("Exception", e.getMessage(),e);
// }
cursor.moveToFirst();
int idex=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
mCurrentImage = cursor.getString(idex);
cursor.close();
resizePhoto();
showimg.setImageBitmap(mPhotoImage);
showtext.setText("click deptach-->");
}
}
super.onActivityResult(requestCode, resultCode, data);
}
//为了确保得到的照片小于3MB 所以要进行压缩
private void resizePhoto() {
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(mCurrentImage, options);
double ratio=Math.max(options.outWidth*1.0f/1024,options.outHeight*1.0f/1024);
options.inSampleSize=(int) Math.ceil(ratio);
options.inJustDecodeBounds=false;
mPhotoImage=BitmapFactory.decodeFile(mCurrentImage, options);
}
4.得到了照片以后,需要将照片转换bytes数组,同时下面的代码封装了获取返回json数组的内容
public class Depatch {
public interface Callback{
void success (JSONObject object);
void error(FaceppParseException string);
}
public static void depatch(final Bitmap bitmap,final Callback callback){
new Thread(new Runnable(){
@Override
public void run() {
try {
HttpRequests httpRequests=new HttpRequests(Constant.KEY, Constant.SECRETC, true, true);
Bitmap bmsmall=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight());
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bmsmall.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] arr=stream.toByteArray();
PostParameters parameters=new PostParameters();
parameters.setImg(arr);
JSONObject object;
object = httpRequests.detectionDetect(parameters);
System.out.println(object.toString());
if(callback!=null){
callback.success(object);
}
} catch (FaceppParseException e) {
// TODO Auto-generated catch block
if(callback!=null){
callback.error(e);
}
}
}
}).start();
}
5.对返回的json数据进行处理,在原先的图片上绘画age和人脸框内容:注意:文字可以作为一个textview进行绘制.
private void prepareAsBitmap(JSONObject result) {
Bitmap bitmap=Bitmap.createBitmap(mPhotoImage.getWidth(), mPhotoImage.getHeight(), mPhotoImage.getConfig());
Canvas canvas=new Canvas(bitmap);
canvas.drawBitmap(mPhotoImage, 0, 0, null);
Paint paint=new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(2);
try {
JSONArray faces=result.getJSONArray("face");
int facecount=faces.length();
showtext.setText("find--"+facecount+"--face");
for(int i=0;i<faces.length();i++){
JSONObject face=faces.getJSONObject(i);
JSONObject position=face.getJSONObject("position");
float x=(float) position.getJSONObject("center").getDouble("x");
float y=(float) position.getJSONObject("center").getDouble("y");
float w=(float) position.getDouble("width");
float h=(float) position.getDouble("height");
x=x/100*bitmap.getWidth();
y=y/100*bitmap.getHeight();
w=w/100*bitmap.getWidth();
h=h/100*bitmap.getHeight();
canvas.drawLine(x-w/2, y-h/2, x-w/2, y+h/2,paint);
canvas.drawLine(x-w/2, y-h/2, x+w/2, y-h/2,paint);
canvas.drawLine(x+w/2, y-h/2, x+w/2, y+h/2,paint);
canvas.drawLine(x-w/2, y+h/2, x+w/2, y+h/2,paint);
int age=face.getJSONObject("attribute").getJSONObject("age").getInt("value");
String gender=face.getJSONObject("attribute").getJSONObject("gender").getString("value");
Bitmap ageBitmap=buildAgeBitmap(age,"Male".equals(gender));
//scale
int ageWidth=ageBitmap.getWidth();
int ageHeight=ageBitmap.getHeight();
if(bitmap.getWidth()<mPhotoImage.getWidth()&&bitmap.getHeight()<mPhotoImage.getHeight()){
float scale=Math.max(bitmap.getWidth()*1.0f/mPhotoImage.getWidth(), bitmap.getHeight()*1.0f/mPhotoImage.getHeight());
ageBitmap=Bitmap.createScaledBitmap(ageBitmap, (int)(ageWidth*scale),(int)(ageHeight*scale), false);
}
canvas.drawBitmap(ageBitmap, x-ageBitmap.getWidth()/2, y-h/2-ageBitmap.getHeight(), paint);
mPhotoImage=bitmap;
}
} catch (JSONException e) {
e.printStackTrace();
}
};
private Bitmap buildAgeBitmap(int age, boolean isMale) {
TextView showAge=(TextView) this.findViewById(R.id.age_gender_msg);
showAge.setText("age:"+age);
if(isMale){
showAge.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.male), null, null, null);
}else{
showAge.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.female), null, null, null);
}
showAge.setDrawingCacheEnabled(true);
Bitmap bitmap=Bitmap.createBitmap(showAge.getDrawingCache());
showAge.destroyDrawingCache();
return bitmap;
}
这样,简单的工程内容就介绍完了。