Android 裁剪人脸类的实例代码
人脸裁剪类
publicfinalclassFaceCj{
privatestaticBitmapFactory.OptionsBitmapFactoryOptionsbfo;
privatestaticByteArrayOutputStreamout;
privatestaticbyte[]data;
privatestaticFaceDetector.Face[]myFace;
privatestaticFaceDetectormyFaceDetect;
privatestaticinttx=0;
privatestaticintty=0;
privatestaticintbx=0;
privatestaticintby=0;
privatestaticintwidth=0;
privatestaticintheight=0;
privatestaticfloatwuchax=0;
privatestaticfloatwuchay=0;
privatestaticFaceDetector.Faceface;
privatestaticPointFmyMidPoint;
privatestaticfloatmyEyesDistance;
privatestaticListfacePaths;
privatestaticStringfacePath;
publicstaticBitmapcutFace(Bitmapbitmap,Contextcontext){
facePaths=null;
BitmapFactoryOptionsbfo=newBitmapFactory.Options();
BitmapFactoryOptionsbfo.inPreferredConfig=Bitmap.Config.RGB_565;//构造位图生成的参数,必须为565。类名+enum
out=newByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,50,out);
data=out.toByteArray();
bitmap=BitmapFactory.decodeByteArray(data,0,data.length,
BitmapFactoryOptionsbfo);
try{
out.flush();
out.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
width=bitmap.getWidth();
height=bitmap.getHeight();
myFace=newFaceDetector.Face[5];//分配人脸数组空间
myFaceDetect=newFaceDetector(bitmap.getWidth(),bitmap.getHeight(),5);
intnumberOfFaceDetected=myFaceDetect.findFaces(bitmap,myFace);
if(numberOfFaceDetected<=0){//FaceDetector构造实例并解析人脸
bitmap.recycle();
returnnull;
}
facePaths=newArrayList();
for(inti=0;iwidth){//判断右边是否出界
bx=width;
}else{
bx=(int)(myMidPoint.x+wuchax);
}
if(myMidPoint.y-wuchay<0){//判断上边是否出界
ty=0;
}else{
ty=(int)(myMidPoint.y-wuchay);
}
if(myMidPoint.y+wuchay>height){//判断下边是否出界
by=height;
}else{
by=(int)(myMidPoint.y+wuchay);
}
try{
returnBitmap.createBitmap(bitmap,tx,ty,bx-tx,by-ty);//这里可以自行调整裁剪宽高
}catch(Exceptione){
e.printStackTrace();
}
}
bitmap.recycle();
returnbitmap;
}
}
总结
以上所述是小编给大家介绍的Android裁剪人脸的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!