Android 裁剪人臉類的實(shí)例代碼
人臉裁剪類
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; } }
總結(jié)
以上所述是小編給大家介紹的Android 裁剪人臉的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android用PopupWindow實(shí)現(xiàn)新浪微博的分組信息實(shí)例
PopupWindow可以實(shí)現(xiàn)浮層效果,而且可以自定義顯示位置,本篇文章主要介紹Android用PopupWindow實(shí)現(xiàn)新浪微博的分組信息,有需要的可以了解一下。2016-11-11Android 6.0 藍(lán)牙搜索不到設(shè)備原因,MIUI權(quán)限申請(qǐng)機(jī)制方法
今天小編就為大家分享一篇Android6.0 藍(lán)牙搜索不到設(shè)備原因,MIUI權(quán)限申請(qǐng)機(jī)制方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android Wear計(jì)時(shí)器開發(fā)
這篇文章主要介紹了Android Wear計(jì)時(shí)器開發(fā),需要的朋友可以參考下2014-11-11詳解Android創(chuàng)建Handler的必備知識(shí)點(diǎn)
本篇文章主要介紹Handler中需要了解的幾個(gè)必備知識(shí)點(diǎn),比如Handler創(chuàng)建、異步Handler是個(gè)啥及如何創(chuàng)建,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下2022-10-10Android利用SurfaceView實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了Android利用SurfaceView實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android 自定義Switch開關(guān)按鈕的樣式實(shí)例詳解
本文主要講的是在Android原生Switch控件的基礎(chǔ)上進(jìn)行樣式自定義,內(nèi)容很簡(jiǎn)單,但是在實(shí)現(xiàn)的過程中還是遇到了一些問題,在此記錄下來,需要的朋友參考下吧2017-12-12Android listview動(dòng)態(tài)加載列表項(xiàng)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android listview動(dòng)態(tài)加載列表項(xiàng)實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android實(shí)現(xiàn)在xml文件中引用自定義View的方法分析
這篇文章主要介紹了Android實(shí)現(xiàn)在xml文件中引用自定義View的方法,結(jié)合實(shí)例形式分析了Android自定義view的實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06Android實(shí)現(xiàn)語音合成與識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)語音合成與識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07