這篇文章主要介紹“如何在Android中實(shí)現(xiàn)一個(gè)裁剪人臉類”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“如何在Android中實(shí)現(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ù),并對(duì)每個(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;
}
}關(guān)于“如何在Android中實(shí)現(xiàn)一個(gè)裁剪人臉類”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
文章名稱:如何在Android中實(shí)現(xiàn)一個(gè)裁剪人臉類-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://www.chinadenli.net/article28/djcccp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、Google、營銷型網(wǎng)站建設(shè)、企業(yè)建站、微信公眾號(hào)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容