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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- Android 7.0中拍照和圖片裁剪適配的問(wèn)題詳解
- Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫(kù)與裁剪圖片功能
- Android圖片裁剪功能實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- 解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無(wú)法裁剪問(wèn)題的方法
相關(guān)文章
DownloadManager實(shí)現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了DownloadManager實(shí)現(xiàn)文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android高手進(jìn)階教程(二十二)之Android中幾種圖像特效處理的集錦匯總!!
本篇文章主要介紹了Android中幾種圖像特效處理,比如圓角,倒影,還有就是圖片縮放,Drawable轉(zhuǎn)化為Bitmap,Bitmap轉(zhuǎn)化為Drawable等,有需要的可以了解一下。2016-11-11
Android編程獲取網(wǎng)絡(luò)時(shí)間實(shí)例分析
這篇文章主要介紹了Android編程獲取網(wǎng)絡(luò)時(shí)間,結(jié)合實(shí)例形式對(duì)比分析了Android通過(guò)訪問(wèn)網(wǎng)絡(luò)及通過(guò)GPS獲取網(wǎng)絡(luò)時(shí)間的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01
Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android登錄記住多個(gè)密碼的實(shí)現(xiàn)方法
通過(guò)在popouWindow里面加上ListView,數(shù)據(jù)是把List以字符串按照J(rèn)SON的樣式存入本地,下面通過(guò)實(shí)例代碼給大家介紹android登錄記住多個(gè)密碼的實(shí)現(xiàn)方法,需要的的朋友參考下吧2017-07-07
Android調(diào)試神器stetho使用詳解和改造
今天小編就為大家分享一篇關(guān)于Android調(diào)試神器stetho使用詳解和改造,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
Android端使用Modbus協(xié)議的簡(jiǎn)單方法
Modbus協(xié)議是全球第一個(gè)用于工業(yè)現(xiàn)場(chǎng)的總線協(xié)議,與外設(shè)交互可以采用串口通信,tcp等方式,這篇文章主要給大家介紹了關(guān)于Android端使用Modbus協(xié)議的簡(jiǎn)單方法,需要的朋友可以參考下2021-11-11

