public class FaceUtil{ private static final String TAG = FaceUtil.class.getSimpleName(); private static FaceUtil faceInstance = null; public FaceDB mFaceDB; private AFR_FSDKFace mAFR_FSDKFace; private static Context mContext = null; //face detect private AFT_FSDKVersion version = new AFT_FSDKVersion(); private AFT_FSDKEngine engine = new AFT_FSDKEngine(); private ASAE_FSDKVersion mAgeVersion = new ASAE_FSDKVersion(); private ASAE_FSDKEngine mAgeEngine = new ASAE_FSDKEngine(); private ASGE_FSDKVersion mGenderVersion = new ASGE_FSDKVersion(); private ASGE_FSDKEngine mGenderEngine = new ASGE_FSDKEngine(); private Listresult = new ArrayList<>(); private List ages = new ArrayList<>(); private List genders = new ArrayList<>(); private AFT_FSDKFace mAFT_FSDKFace = null; private boolean openFace = false; public static FaceUtil getInstance(Context context) { if(faceInstance == null) { faceInstance = new FaceUtil(context); } return faceInstance; } public FaceUtil(Context context) { mContext = context; } public void setFaceDB() { //face mFaceDB = new FaceDB(mContext.getExternalCacheDir().getPath()); Log.d(TAG,"getExternalCacheDir : "+mContext.getExternalCacheDir().getPath()); } public void initFaceDetect() { AFT_FSDKError err = engine.AFT_FSDK_InitialFaceEngine(FaceDB.appid, FaceDB.ft_key, AFT_FSDKEngine.AFT_OPF_0_HIGHER_EXT, 16, 5); Log.d(TAG, "AFT_FSDK_InitialFaceEngine =" + err.getCode()); err = engine.AFT_FSDK_GetVersion(version); Log.d(TAG, "AFT_FSDK_GetVersion:" + version.toString() + "," + err.getCode()); ASAE_FSDKError error = mAgeEngine.ASAE_FSDK_InitAgeEngine(FaceDB.appid, FaceDB.age_key); Log.d(TAG, "ASAE_FSDK_InitAgeEngine =" + error.getCode()); error = mAgeEngine.ASAE_FSDK_GetVersion(mAgeVersion); Log.d(TAG, "ASAE_FSDK_GetVersion:" + mAgeVersion.toString() + "," + error.getCode()); ASGE_FSDKError error1 = mGenderEngine.ASGE_FSDK_InitgGenderEngine(FaceDB.appid, FaceDB.gender_key); Log.d(TAG, "ASGE_FSDK_InitgGenderEngine =" + error1.getCode()); error1 = mGenderEngine.ASGE_FSDK_GetVersion(mGenderVersion); Log.d(TAG, "ASGE_FSDK_GetVersion:" + mGenderVersion.toString() + "," + error1.getCode()); } public void deInitFaceDetect() { AFT_FSDKError err = engine.AFT_FSDK_UninitialFaceEngine(); Log.d(TAG, "AFT_FSDK_UninitialFaceEngine =" + err.getCode()); ASAE_FSDKError err1 = mAgeEngine.ASAE_FSDK_UninitAgeEngine(); Log.d(TAG, "ASAE_FSDK_UninitAgeEngine =" + err1.getCode()); ASGE_FSDKError err2 = mGenderEngine.ASGE_FSDK_UninitGenderEngine(); Log.d(TAG, "ASGE_FSDK_UninitGenderEngine =" + err2.getCode()); } public boolean faceFeatureDetect(byte[] mYUVData,int width,int height) { boolean bDectectFace = false; AFT_FSDKError err =engine.AFT_FSDK_FaceFeatureDetect(mYUVData, width, height, AFT_FSDKEngine.CP_PAF_NV21, result); Log.d(TAG, "AFT_FSDK_FaceFeatureDetect =" + err.getCode()); Log.d(TAG, "Face=" + result.size()); if (!result.isEmpty()) { for (AFT_FSDKFace face : result) { Log.d(TAG, "Face:" + face.toString()); } mAFT_FSDKFace = result.get(0).clone(); bDectectFace = true; } else { Log.d(TAG,"没有检测到人脸"); bDectectFace = false; } result.clear(); return bDectectFace; } public AFT_FSDKFace getmAFT_FSDKFace() { return mAFT_FSDKFace; } public String getAge(byte[] mYUVData,int width,int height,List face1) { String age; ASAE_FSDKError error1 = mAgeEngine.ASAE_FSDK_AgeEstimation_Image(mYUVData, width, height, AFT_FSDKEngine.CP_PAF_NV21, face1, ages); Log.d("face", "ASAE_FSDK_AgeEstimation_Image:" + error1.getCode()); if(ages.size()== 0) { Log.d(TAG, "ages.size()== 0" ); age = "年龄未知"; } else { Log.d(TAG, "age:" + ages.get(0).getAge()); age = ages.get(0).getAge() == 0 ? "年龄未知" : ages.get(0).getAge() + "岁"; } return age; } public String getGender(byte[] mYUVData,int width,int height,List face2) { String gender; ASGE_FSDKError error2 = mGenderEngine.ASGE_FSDK_GenderEstimation_Image(mYUVData, width, height, AFT_FSDKEngine.CP_PAF_NV21, face2, genders); Log.d("face", ",ASGE_FSDK_GenderEstimation_Image:" + error2.getCode()); if(ages.size()== 0) { Log.d(TAG, "ages.size()== 0" ); gender = "性别未知"; } else { Log.d(TAG, "gender:" + genders.get(0).getGender()); gender = genders.get(0).getGender() == -1 ? "性别未知" : (genders.get(0).getGender() == 0 ? "男" : "女"); } return gender; } /** * @param path * @return */ public static Bitmap decodeImage(String path) { Bitmap res; try { ExifInterface exif = new ExifInterface(path); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); BitmapFactory.Options op = new BitmapFactory.Options(); op.inSampleSize = 1; op.inJustDecodeBounds = false; //op.inMutable = true; res = BitmapFactory.decodeFile(path, op); //rotate and scale. Matrix matrix = new Matrix(); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { matrix.postRotate(90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { matrix.postRotate(180); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { matrix.postRotate(270); } Bitmap temp = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), matrix, true); Log.d(TAG, "check target Image:" + temp.getWidth() + "X" + temp.getHeight()); if (!temp.equals(res)) { res.recycle(); } return temp; } catch (Exception e) { e.printStackTrace(); } return null; } public void bitmapDetect(String name,Bitmap mBitmap) { byte[] data = new byte[mBitmap.getWidth() * mBitmap.getHeight() * 3 / 2]; ImageConverter convert = new ImageConverter(); convert.initial(mBitmap.getWidth(), mBitmap.getHeight(), ImageConverter.CP_PAF_NV21); if (convert.convert(mBitmap, data)) { Log.d(TAG, "convert ok!"); } convert.destroy(); AFD_FSDKEngine engine = new AFD_FSDKEngine(); AFD_FSDKVersion version = new AFD_FSDKVersion(); List result = new ArrayList (); AFD_FSDKError err = engine.AFD_FSDK_InitialFaceEngine(FaceDB.appid, FaceDB.fd_key, AFD_FSDKEngine.AFD_OPF_0_HIGHER_EXT, 16, 5); Log.d(TAG, "AFD_FSDK_InitialFaceEngine = " + err.getCode()); if (err.getCode() != AFD_FSDKError.MOK) {// Message reg = Message.obtain();// reg.what = MSG_CODE;// reg.arg1 = MSG_EVENT_FD_ERROR;// reg.arg2 = err.getCode();// mUIHandler.sendMessage(reg); Log.d(TAG, "MSG_EVENT_FD_ERROR"); ToastUtil.showToast("sdk error :MSG_EVENT_FD_ERROR "+err.getCode()); } err = engine.AFD_FSDK_GetVersion(version); Log.d(TAG, "AFD_FSDK_GetVersion =" + version.toString() + ", " + err.getCode()); err = engine.AFD_FSDK_StillImageFaceDetection(data, mBitmap.getWidth(), mBitmap.getHeight(), AFD_FSDKEngine.CP_PAF_NV21, result); Log.d(TAG, "AFD_FSDK_StillImageFaceDetection =" + err.getCode() + "<" + result.size()); if (!result.isEmpty()) { AFR_FSDKVersion version1 = new AFR_FSDKVersion(); AFR_FSDKEngine engine1 = new AFR_FSDKEngine(); AFR_FSDKFace result1 = new AFR_FSDKFace(); AFR_FSDKError error1 = engine1.AFR_FSDK_InitialEngine(FaceDB.appid, FaceDB.fr_key); Log.d(TAG, "AFR_FSDK_InitialEngine = " + error1.getCode()); if (error1.getCode() != AFD_FSDKError.MOK) {// Message reg = Message.obtain();// reg.what = MSG_CODE;// reg.arg1 = MSG_EVENT_FR_ERROR;// reg.arg2 = error1.getCode();// mUIHandler.sendMessage(reg); Log.d(TAG, "MSG_EVENT_FR_ERROR"); ToastUtil.showToast("sdk error :MSG_EVENT_FR_ERROR "+error1.getCode()); } error1 = engine1.AFR_FSDK_GetVersion(version1); Log.d(TAG, "FR=" + version.toString() + "," + error1.getCode()); //(210, 178 - 478, 446), degree = 1 780, 2208 - 1942, 3370 error1 = engine1.AFR_FSDK_ExtractFRFeature(data, mBitmap.getWidth(), mBitmap.getHeight(), AFR_FSDKEngine.CP_PAF_NV21, new Rect(result.get(0).getRect()), result.get(0).getDegree(), result1); Log.d(TAG, "Face=" + result1.getFeatureData()[0] + "," + result1.getFeatureData()[1] + "," + result1.getFeatureData()[2] + "," + error1.getCode()); if(error1.getCode() == error1.MOK) { mAFR_FSDKFace = result1.clone();// int width = result.get(0).getRect().width();// int height = result.get(0).getRect().height();// Bitmap face_bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);// Canvas face_canvas = new Canvas(face_bitmap);// face_canvas.drawBitmap(mBitmap, result.get(0).getRect(), new Rect(0, 0, width, height), null);// Message reg = Message.obtain();// reg.what = MSG_CODE;// reg.arg1 = MSG_EVENT_REG;// reg.obj = face_bitmap;// mUIHandler.sendMessage(reg); mFaceDB.addFace(name, mAFR_FSDKFace); Log.d(TAG, "addFace MSG_EVENT_REG jxd"); ToastUtil.showToast(mContext.getString(R.string.register_face_success)+name); } else {// Message reg = Message.obtain();// reg.what = MSG_CODE;// reg.arg1 = MSG_EVENT_NO_FEATURE;// mUIHandler.sendMessage(reg); Log.d(TAG, "MSG_EVENT_NO_FEATURE"); ToastUtil.showToast(mContext.getString(R.string.no_feature)); } error1 = engine1.AFR_FSDK_UninitialEngine(); Log.d(TAG, "AFR_FSDK_UninitialEngine : " + error1.getCode()); } else {// Message reg = Message.obtain();// reg.what = MSG_CODE;// reg.arg1 = MSG_EVENT_NO_FACE;// mUIHandler.sendMessage(reg); Log.d(TAG, "MSG_EVENT_NO_FACE"); ToastUtil.showToast(mContext.getString(R.string.no_face)); } err = engine.AFD_FSDK_UninitialFaceEngine(); Log.d(TAG, "AFD_FSDK_UninitialFaceEngine =" + err.getCode()); } public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } public String getPath(Uri uri) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (DocumentsContract.isDocumentUri(mContext, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } // TODO handle non-primary volumes } else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(mContext, contentUri, null, null); } else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(mContext, contentUri, selection, selectionArgs); } } } String[] proj = { MediaStore.Images.Media.DATA }; Cursor actualimagecursor = mContext.getContentResolver().query(uri, proj, null, null, null); int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst(); String img_path = actualimagecursor.getString(actual_image_column_index); String end = img_path.substring(img_path.length() - 4); if (0 != end.compareToIgnoreCase(".jpg") && 0 != end.compareToIgnoreCase(".png")) { return null; } return img_path; } public void setFaceFunction() { openFace = SharedPreferenceUtil.getBoolean(OPEN_FACE, Constant.FACE, false); openFace = !openFace; SharedPreferenceUtil.putBoolean(OPEN_FACE, Constant.FACE, openFace); if(openFace) { ToastUtil.showToast(R.string.open_face); } else { ToastUtil.showToast(R.string.close_face); } } public boolean getFaceIsOpen() { return openFace; }}