溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中實(shí)現(xiàn)一個(gè)裁剪人臉類

發(fā)布時(shí)間:2021-06-08 16:48:17 來源:億速云 閱讀:192 作者:Leah 欄目:移動(dòng)開發(fā)

怎么在Android中實(shí)現(xiàn)一個(gè)裁剪人臉類?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

人臉裁剪類

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; // 構(gòu)造位圖生成的參數(shù),必須為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]; // 分配人臉數(shù)組空間
    myFaceDetect = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 5);
    int numberOfFaceDetected = myFaceDetect.findFaces(bitmap, myFace);
    if (numberOfFaceDetected <= 0) {// FaceDetector構(gòu)造實(shí)例并解析人臉
      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();  //得到人臉中心點(diǎn)和眼間距離參數(shù),并對每個(gè)人臉進(jìn)行畫框
      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);//這里可以自行調(diào)整裁剪寬高
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    bitmap.recycle();
    return bitmap;
  }
}

看完上述內(nèi)容,你們掌握怎么在Android中實(shí)現(xiàn)一個(gè)裁剪人臉類的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI