Android面部检测器出错

“返回0面,因为错误存在btk_facefinder_putdcr”Android面部检测器出错(“return 0 faces because error exists btk_facefinder_putdcr” Error in Android face detector

传入的图片的宽度必须的英文偶数的

Bitmap srcImg = BitmapFactory.decodeFile(imgUrl);Bitmap srcFace = srcImg.copy(Bitmap.Config.RGB_565, true);srcImg = null;int w = srcFace.getWidth();int h = srcFace.getHeight();if (w % 2 == 1) {w++;srcFace = Bitmap.createScaledBitmap(srcFace,srcFace.getWidth()+1, srcFace.getHeight(), false);}if (h % 2 == 1) {h++;srcFace = Bitmap.createScaledBitmap(srcFace,srcFace.getWidth(), srcFace.getHeight()+1, false);}

http://blog.csdn.net/devilkin64/article/details/8509767 传入的图片的宽度必须是偶数的

Bitmap srcImg = BitmapFactory.decodeFile(imgUrl);Bitmap srcFace = srcImg.copy(Bitmap.Config.RGB_565, true);srcImg = null;int w = srcFace.getWidth();int h = srcFace.getHeight();if (w % 2 == 1) {w++;srcFace = Bitmap.createScaledBitmap(srcFace,srcFace.getWidth()+1, srcFace.getHeight(), false);}if (h % 2 == 1) {h++;srcFace = Bitmap.createScaledBitmap(srcFace,srcFace.getWidth(), srcFace.getHeight()+1, false);}
public final class FaceCj {private static BitmapFactory.Options BitmapFactoryOptionsbfo;private static ByteArrayOutputStream out;private static byte[] data;private static FaceDetector.Face[] myFace;private static FaceDetector myFaceDetect;private static int tx = 0;private static int ty = 0;private static int bx = 0;private static int by = 0;private static int width = 0;private static int height = 0;private static float wuchax = 0;private static float wuchay = 0;private static FaceDetector.Face face;private static PointF myMidPoint;private static float myEyesDistance;private static List facePaths;private static String facePath;public static Bitmap cutFace(Bitmap bitmap, Context context) {facePaths = null;BitmapFactoryOptionsbfo = new BitmapFactory.Options();BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; // 构造位图生成的参数,必须为565。类名+enumout = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);data = out.toByteArray();bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,BitmapFactoryOptionsbfo);try {out.flush();out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}width = bitmap.getWidth();height = bitmap.getHeight();//重新测算if (width % 2 == 1) {width++;bitmap = Bitmap.createScaledBitmap(bitmap,bitmap.getWidth()+1, bitmap.getHeight(), false);}if (height % 2 == 1) {height++;bitmap = Bitmap.createScaledBitmap(bitmap,bitmap.getWidth(), bitmap.getHeight()+1, false);}myFace = new FaceDetector.Face[5]; // 分配人脸数组空间myFaceDetect = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 5);int numberOfFaceDetected = myFaceDetect.findFaces(bitmap, myFace);if (numberOfFaceDetected <= 0) {// FaceDetector构造实例并解析人脸bitmap.recycle();return null;}facePaths = new ArrayList();for (int i = 0; i < numberOfFaceDetected; i++) {face = myFace[i];myMidPoint = new PointF();face.getMidPoint(myMidPoint);myEyesDistance = face.eyesDistance();   //得到人脸中心点和眼间距离参数,并对每个人脸进行画框wuchax = myEyesDistance / 2 + myEyesDistance;wuchay = myEyesDistance * 2 / 3 + myEyesDistance;if (myMidPoint.x - wuchax < 0) {//判断左边是否出界tx = 0;} else {tx = (int) (myMidPoint.x - wuchax);}if (myMidPoint.x + wuchax > width) {//判断右边是否出界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 {return Bitmap.createBitmap(bitmap, tx, ty, bx - tx, by - ty);//这里可以自行调整裁剪宽高} catch (Exception e) {e.printStackTrace();}}bitmap.recycle();return bitmap;}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部