
特别注明:程序来自慕课学院 |
====================================== |
package com.example.fengke_how_old; |
import org.json.JSONArray; |
import org.json.JSONException; |
import org.json.JSONObject; |
import com.facepp.error.FaceppParseException; |
import android.content.Intent; |
import android.database.Cursor; |
import android.graphics.Bitmap; |
import android.graphics.BitmapFactory; |
import android.graphics.Canvas; |
import android.graphics.Paint; |
import android.net.Uri; |
import android.os.Bundle; |
import android.os.Handler; |
import android.os.Message; |
import android.provider.MediaStore; |
import android.support.v7.app.ActionBarActivity; |
import android.text.TextUtils; |
import android.view.View; |
import android.view.View.OnClickListener; |
import android.widget.Button; |
import android.widget.ImageView; |
import android.widget.TextView; |
public class MainActivity extends ActionBarActivity implements OnClickListener { |
|
private static final int PICK_CODE= 0X110; |
private ImageView mPhoto; |
private Button mGetImage; |
private Button mDelect; |
private TextView mTip; |
private View mWaitting; |
private String mCurrentPhotoStr; |
private Bitmap mPhotoImg; |
private Paint mPaint; |
@Override |
protected void onCreate(Bundle savedInstanceState) { |
super.onCreate(savedInstanceState); |
setContentView(R.layout.activity_main); |
|
inintViews(); |
inintEvents(); |
mPaint=new Paint(); |
|
|
} |
|
private void inintEvents(){ |
mGetImage.setOnClickListener(this); |
mDelect.setOnClickListener(this); |
|
} |
|
private void inintViews() |
{ |
mPhoto=(ImageView)findViewById(R.id.id_photo); |
mGetImage=(Button)findViewById(R.id.id_getImage); |
mDelect=(Button)findViewById(R.id.id_detect); |
mTip=(TextView)findViewById(R.id.id_tip); |
mWaitting=findViewById(R.id.id_waiting); |
} |
|
public void onClick(View v){ |
|
switch(v.getId()){ |
case R.id.id_getImage: |
|
Intent intent=new Intent(Intent.ACTION_PICK); |
intent.setType("image/*"); |
startActivityForResult(intent, PICK_CODE); |
break; |
|
case R.id.id_detect: |
|
mWaitting.setVisibility(View.VISIBLE); |
|
if(mCurrentPhotoStr!=null&&!mCurrentPhotoStr.trim().equals("")){ |
resizePhoto(); |
}else{ |
mPhotoImg=BitmapFactory.decodeResource(getResources(), R.drawable.image); |
|
} |
|
FaceppDetect.detect(mPhotoImg, new FaceppDetect.CallBack() { |
|
@Override |
public void success(JSONObject result) { |
Message msg=Message.obtain(); |
msg.what=MSG_SUCCESS; |
msg.obj=result; |
mHandler.sendMessage(msg); |
// TODO Auto-generated method stub |
|
} |
|
@Override |
public void error(FaceppParseException exception) { |
|
Message msg=Message.obtain(); |
msg.what=MSG_ERROR; |
msg.obj=exception.getErrorMessage(); |
mHandler.sendMessage(msg); |
// TODO Auto-generated method stub |
|
} |
}); |
break; |
|
|
} |
|
|
|
} |
|
protected void onActivityResult(int requestCode,int resultCode ,Intent intent){ |
|
if(requestCode==PICK_CODE){ |
if(intent!=null){ |
Uri uri=intent.getData(); |
Cursor cursor=getContentResolver().query(uri, null, null, null, null); |
cursor.moveToFirst(); |
int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); |
mCurrentPhotoStr=cursor.getString(idx); |
cursor.close(); |
|
//压缩照片 |
|
resizePhoto(); |
mPhoto.setImageBitmap(mPhotoImg); |
mTip.setText("Click Detect ==>"); |
} |
} |
super.onActivityResult(requestCode, requestCode, intent); |
} |
private void resizePhoto() { |
|
BitmapFactory.Options options=new BitmapFactory.Options(); |
options.inJustDecodeBounds=true; |
BitmapFactory.decodeFile(mCurrentPhotoStr, options); |
|
double ratio=Math.max(options.outWidth*1.0d/1024f, options.outHeight*1.0d/1024f); |
|
options.inSampleSize=(int)Math.ceil(ratio); |
|
options.inJustDecodeBounds=false; |
|
mPhotoImg=BitmapFactory.decodeFile(mCurrentPhotoStr, options); |
// TODO Auto-generated method stub |
|
} |
private static final int MSG_SUCCESS=0x111; |
private static final int MSG_ERROR=0x112; |
private Handler mHandler=new Handler(){ |
public void handleMessage(Message msg){ |
|
switch(msg.what){ |
|
case MSG_SUCCESS: |
mWaitting.setVisibility(View.GONE); |
JSONObject rs=(JSONObject)msg.obj; |
prepareRsBitmap(rs); |
mPhoto.setImageBitmap(mPhotoImg); |
|
|
break; |
case MSG_ERROR: |
mWaitting.setVisibility(View.GONE); |
String errormsg=(String)msg.obj; |
if(TextUtils.isEmpty(errormsg)){ |
mTip.setText("Error."); |
}else{ |
mTip.setText(errormsg); |
} |
|
|
|
break; |
|
} |
super.handleMessage(msg); |
} |
|
}; |
private void prepareRsBitmap(JSONObject rs) { |
|
Bitmap bitmap=Bitmap.createBitmap(mPhotoImg.getWidth(),mPhotoImg.getHeight(),mPhotoImg.getConfig()); |
Canvas canvas=new Canvas(bitmap); |
canvas.drawBitmap(mPhotoImg, 0,0, null); |
|
|
try { |
JSONArray faces=rs.getJSONArray("face"); |
int faceCount=faces.length(); |
mTip.setText("find" + faceCount); |
for(int i=0;i<faceCount;i++){ |
JSONObject face=faces.getJSONObject(i); |
JSONObject posObj=face.getJSONObject("position"); |
float x=(float) posObj.getJSONObject("center").getDouble("x"); |
float y=(float) posObj.getJSONObject("center").getDouble("y"); |
float w=(float) posObj.getDouble("width"); |
float h=(float) posObj.getDouble("height"); |
x=x/100*bitmap.getWidth(); |
y=y/100*bitmap.getHeight(); |
|
w=w/100*bitmap.getWidth(); |
h=h/100*bitmap.getHeight(); |
|
mPaint.setColor(0xffffffff); |
mPaint.setStrokeWidth(3); |
|
//box |
|
canvas.drawLine(x-w/2,y-h/2,x-w/2,y+h/2, mPaint); |
canvas.drawLine(x-w/2,y-h/2,x+w/2,y-h/2, mPaint); |
canvas.drawLine(x+w/2,y-h/2,x+w/2,y+h/2, mPaint); |
canvas.drawLine(x-w/2,y+h/2,x+w/2,y+h/2, mPaint); |
|
int age=face.getJSONObject("attribute").getJSONObject("age").getInt("value"); |
String gender=face.getJSONObject("attribute").getJSONObject("gender").getString("value"); |
|
Bitmap ageBitmap=buildAgeBitmp(age,"Male".equals(gender)); |
|
//缩放年龄图片 |
int ageWidth=ageBitmap.getWidth(); |
int ageHeight=ageBitmap.getHeight(); |
|
if(bitmap.getWidth()<mPhoto.getWidth()&&bitmap.getHeight()<mPhoto.getHeight()){ |
|
float ratio=Math.max(bitmap.getWidth()*1.0f/mPhoto.getWidth(), bitmap.getHeight()*1.0f/mPhoto.getHeight()); |
ageBitmap=Bitmap.createScaledBitmap(ageBitmap, (int)(ageWidth*ratio), (int)(ageHeight*ratio), false); |
|
} |
|
canvas.drawBitmap(ageBitmap, x-ageBitmap.getWidth()/2,y-h/2-ageBitmap.getHeight(),null); |
|
mPhotoImg=bitmap; |
} |
|
} catch (JSONException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
|
|
|
|
|
// TODO Auto-generated method stub |
|
} |
private Bitmap buildAgeBitmp(int age, boolean isMale) { |
TextView tv=(TextView)mWaitting.findViewById(R.id.id_age_and_gender); |
tv.setText(age+""); |
if(isMale){ |
tv.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.male), null, null, null); |
|
}else{ |
tv.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.female), null, null, null); |
} |
|
tv.setDrawingCacheEnabled(true); |
Bitmap bitmap=Bitmap.createBitmap(tv.getDrawingCache()); |
tv.destroyDrawingCache(); |
|
// TODO Auto-generated method stub |
return bitmap; |
} |
|
|
} |
===================================================== |
package com.example.fengke_how_old; |
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; |
public class FaceppDetect { |
|
|
public interface CallBack{ |
void success(JSONObject result); |
void error (FaceppParseException exception); |
|
} |
|
|
public static void detect(final Bitmap bm,final CallBack callBack){ |
|
new Thread( new Runnable(){ |
@Override |
public void run() { |
|
|
|
try { |
HttpRequests reuqests=new HttpRequests(Constant.KEY, Constant.SECRET, true, true); |
Bitmap bmSmall=Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight()); |
ByteArrayOutputStream stream=new ByteArrayOutputStream(); |
bmSmall.compress(Bitmap.CompressFormat.JPEG, 100, stream); |
byte[] arrays=stream.toByteArray(); |
PostParameters params=new PostParameters(); |
params.setImg(arrays); |
JSONObject jsonObject=reuqests.detectionDetect(params); |
Log.e("TAG", jsonObject.toString()); |
if(callBack!=null){ |
callBack.success(jsonObject); |
} |
|
|
|
|
} catch (FaceppParseException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
if(callBack!=null){ |
callBack.error(e); |
} |
} |
|
|
|
} |
|
|
|
|
|
}).start(); |
} |
|
} |
========================================================= |
package com.example.fengke_how_old; |
public class Constant { |
|
public static final String KEY="57dfae64b546253db219e7ed2d409f47"; |
public static final String SECRET="3RhWaggYPZ8ZsRYyG_LRxKJZ6q_f8zhF"; |
} |
======================================================== |
<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.example.how_old.MainActivity" > |
|
<Button |
android:layout_width="wrap_content" |
android:layout_height="wrap_content" |
android:text="Get Image" |
android:layout_alignParentRight="true" |
android:layout_alignParentBottom="true" |
android:layout_marginRight="10dp" |
android:id="@+id/id_getImage" |
|
/> |
|
<Button |
android:layout_width="wrap_content" |
android:layout_height="wrap_content" |
android:text="Detect" |
android:layout_alignParentBottom="true" |
android:layout_marginRight="10dp" |
android:id="@+id/id_detect" |
android:layout_toLeftOf="@id/id_getImage" |
|
/> |
<TextView |
android:layout_width="wrap_content" |
android:layout_height="wrap_content" |
android:id="@+id/id_tip" |
android:layout_marginLeft="10dp" |
android:layout_alignTop="@id/id_detect" |
android:layout_toLeftOf="@id/id_detect" |
android:gravity="center" |
/> |
|
<ImageView |
android:layout_width="wrap_content" |
android:layout_height="wrap_content" |
android:id="@+id/id_photo" |
android:layout_above="@id/id_detect" |
android:layout_alignParentLeft="true" |
android:layout_alignParentRight="true" |
android:layout_alignParentTop="true" |
android:src="@drawable/image" |
|
/> |
|
<FrameLayout |
android:layout_width="match_parent" |
android:layout_height="match_parent" |
android:visibility="gone" |
android:clickable="true" |
android:id="@+id/id_waiting" |
> |
<ProgressBar |
android:layout_width="wrap_content" |
android:layout_height="wrap_content" |
android:layout_gravity="center" |
/> |
<TextView |
android:layout_width="wrap_content" |
android:layout_height="wrap_content" |
android:id="@+id/id_age_and_gender" |
android:drawableLeft="@drawable/male" |
android:text="123" |
|
android:visibility="invisible" |
android:textColor="#fff00fff" |
android:textSize="22sp" |
android:gravity="center" |
|
/> |
|
</FrameLayout> |
</RelativeLayout> |
=============================================== |
<?xml version="1.0" encoding="utf-8"?> |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
package="com.example.fengke_how_old" |
android:versionCode="1" |
android:versionName="1.0" > |
<uses-permission android:name="android.permission.INTERNET"/> |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
<uses-sdk |
android:minSdkVersion="8" |
android:targetSdkVersion="21" /> |
<application |
android:allowBackup="true" |
android:icon="@drawable/tubiao" |
android:label="@string/app_name" |
android:theme="@style/AppTheme" > |
<activity |
android:name=".MainActivity" |
android:label="@string/app_name" > |
<intent-filter> |
<action android:name="android.intent.action.MAIN" /> |
<category android:name="android.intent.category.LAUNCHER" /> |
</intent-filter> |
</activity> |
</application> |
</manifest> |




by: 发表于:2017-12-21 09:49:16 顶(0) | 踩(0) 回复
??
回复评论