zoukankan      html  css  js  c++  java
  • 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 List<String> 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。类名+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<String>();
            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;
        }
    
    }

     

  • 相关阅读:
    2021,6,10 xjzx 模拟考试
    平衡树(二)——Treap
    AtCoder Beginner Contest 204 A-E简要题解
    POJ 2311 Cutting Game 题解
    Codeforces 990G GCD Counting 题解
    NOI2021 SDPTT D2T1 我已经完全理解了 DFS 序线段树 题解
    第三届山东省青少年创意编程与智能设计大赛总结
    Luogu P6042 「ACOI2020」学园祭 题解
    联合省选2021 游记
    Codeforces 1498E Two Houses 题解 —— 如何用结论吊打标算
  • 原文地址:https://www.cnblogs.com/94xiyang/p/9407544.html
Copyright © 2011-2022 走看看