注册Face++申请key和secret,导入SDK,有不足缺陷的地方还望指出。
public classMainActivityextendsAppCompatActivity {
privateStringkey="yourKey";//api_key
privateStringsecret="youreApi_secret";//api_secret
longstart=System.currentTimeMillis();
privateImageViewimg;
privateBitmapbitmap;
privateBitmapCanvasbitmacanvas;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mains);
img= (ImageView) findViewById(R.id.img);
bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.timg3).copy(Bitmap.Config.RGB_565,true);
bitmacanvas=newBitmapCanvas(bitmap);
img.setImageBitmap(bitmap);
newThread(newRunnable() {
@Override
public voidrun() {
post();
}
}).start();
}
public voidpost(){
ByteArrayOutputStream byteArrayOutputStream =newByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
bytes= byteArrayOutputStream.toByteArray();
if(bytes.length>0){
start=System.currentTimeMillis();
handler.sendEmptyMessage(0);
}
}
private byte[]bytes;
privateHandlerhandler=newHandler(Looper.getMainLooper()){
@Override
public voidhandleMessage(Message msg) {
if(msg.what==2){
img.setImageBitmap(bitmap);
return;
}
newThread(newRunnable() {
@Override
public voidrun() {
// ArrayList faces = new ArrayList<>();
CommonOperate commonOperate =newCommonOperate(key,secret,false);
try{
Response response1 = commonOperate.detectByte(bytes,0,"gender,age");
if(getFaceToken(response1)){
bitmacanvas.setMessageList(facemessages,newCanvasFinish() {
@Override
public voidcanvasfinish() {
handler.sendEmptyMessage(2);
}
});
}
}catch(Exception e) {
e.printStackTrace();
}
}
}).start();
}
};
private booleangetFaceToken(Response response)throwsJSONException {
if(response.getStatus() !=200){
return false;
}
String res =newString(response.getContent());
jsonParse(res);
return true;
}
privateListfacemessages;
private voidjsonParse(String value){
if(facemessages==null){
facemessages=newArrayList<>();
}else{
facemessages.clear();
}
try{
JSONObject jsonObject =newJSONObject(value);
JSONArray faces = jsonObject.getJSONArray("faces");
for(inti =0; i
LogShow.logShow(faces.get(i).toString());
FaceMessage faceonew=newFaceMessage();
faceonew.setGender( faces.getJSONObject(i).getJSONObject("attributes").getJSONObject("gender").getString("value"));
faceonew.setAge(faces.getJSONObject(i).getJSONObject("attributes").getJSONObject("age").getInt("value"));
intsize=faces.getJSONObject(i).getJSONObject("face_rectangle").getInt("width");
inttop= faces.getJSONObject(i).getJSONObject("face_rectangle").getInt("top");
intleft= faces.getJSONObject(i).getJSONObject("face_rectangle").getInt("left");
faceonew.setRectF(size,top,left);
facemessages.add(faceonew);
}
}catch(JSONException e) {
e.printStackTrace();
}
}
@Override
protected voidonDestroy() {
super.onDestroy();
if(bitmap!=null){
bitmap.recycle();
}
if(facemessages!=null){
facemessages.clear();
}
if(handler!=null){
handler.removeCallbacksAndMessages(null);
}
}
}
public classBitmapCanvas {
privateBitmapbitmap;
privateListmessageList;
publicBitmap getBitmap() {
returnbitmap;
}
public voidsetBitmap(Bitmap bitmap) {
this.bitmap= bitmap;
}
publicBitmapCanvas(Bitmap bitmap) {
this.bitmap= bitmap;
}
publicList getMessageList() {
returnmessageList;
}
public voidsetMessageList(List messageList,CanvasFinish c) {
this.messageList= messageList;
draw(c);
}
public voiddraw(CanvasFinish c){
if(messageList!=null){
Canvas canvas =newCanvas(bitmap);
// canvas.drawRect(getRectF(),getpaint());
for(inti =0; i
canvas.drawRect(messageList.get(i).getRectF(),getpaint());
canvas.drawText(messageList.get(i).getGender()+"年龄"+messageList.get(i).getAge(),messageList.get(i).getPoint().x,messageList.get(i).getPoint().y,textPaint());
}
c.canvasfinish();
}
}
privatePaint getpaint(){
Paint paint =newPaint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5f);
returnpaint;
}
privatePaint textPaint(){
Paint paint =newPaint();
paint.setColor(Color.BLUE);
paint.setTextSize(40f);
returnpaint;
}
}
public classFaceMessage {
privateStringgender;
private intage;
privateRectFrectF;
publicString getGender() {
returngender;
}
public voidsetGender(String gender) {
this.gender=gender.equals("Male")?"男":"女";
}
public intgetAge() {
returnage;
}
public voidsetAge(intage) {
this.age= age;
}
publicRectF getRectF() {
returnrectF;
}
public voidsetRectF(RectF rectF) {
this.rectF= rectF;
}
public voidsetRectF(intsize,inttop,intleft) {
this.rectF=newRectF(left,top,left+size,top+size);
}
publicPointF getPoint(){
if(rectF!=null){
PointF point =newPointF(rectF.left,rectF.top);
returnpoint;
}
return null;
}
}
public interfaceCanvasFinish{
voidcanvasfinish();
}
网友评论