重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍“如何在Android中实现一个裁剪人脸类”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“如何在Android中实现一个裁剪人脸类”文章能帮助大家解决问题。
专业成都网站建设公司,做排名好的好网站,排在同行前面,为您带来客户和效益!成都创新互联公司为您提供成都网站建设,五站合一网站设计制作,服务好的网站设计公司,成都网站设计、做网站负责任的成都网站制作公司!人脸裁剪类
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 ListfacePaths; 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。类名+enum out = 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 block e.printStackTrace(); } width = bitmap.getWidth(); height = bitmap.getHeight(); 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; } }
关于“如何在Android中实现一个裁剪人脸类”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。