Java tutorial
package com.sportsapp; import android.*; import android.Manifest; import android.annotation.TargetApi; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import com.baidu.idl.face.platform.FaceConfig; import com.baidu.idl.face.platform.FaceEnvironment; import com.baidu.idl.face.platform.FaceSDKManager; import com.baidu.idl.face.platform.LivenessTypeEnum; import com.facebook.react.ReactActivity; import com.sportsapp.faceDetect.ExampleApplication; import java.io.File; public class MainActivity extends ReactActivity { private static final int MY_PERMISSION_REQUEST_CODE = 10000; /** * Returns the name of the main component registered from JavaScript. * This is used to schedule rendering of the component. */ @Override protected String getMainComponentName() { return "sportsApp"; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyActivityManager.addActivity(this);//Activity // ? ExampleApplication.livenessList.clear(); ExampleApplication.livenessList.add(LivenessTypeEnum.Eye); ExampleApplication.livenessList.add(LivenessTypeEnum.Mouth); ExampleApplication.livenessList.add(LivenessTypeEnum.HeadUp); ExampleApplication.livenessList.add(LivenessTypeEnum.HeadDown); ExampleApplication.livenessList.add(LivenessTypeEnum.HeadLeft); ExampleApplication.livenessList.add(LivenessTypeEnum.HeadRight); ExampleApplication.livenessList.add(LivenessTypeEnum.HeadLeftOrRight); requestGranted(); //requestPermissions(99, android.Manifest.permission.CAMERA); initLib(); } @Override protected void onDestroy() { super.onDestroy(); MyActivityManager.removeActivity(this);//Activity } //?SDK private void initLib() { FaceSDKManager.getInstance().initialize(this, "sportsApp-face-android", "idl-license.face-android"); FaceConfig config = FaceSDKManager.getInstance().getFaceConfig(); config.setLivenessTypeList(ExampleApplication.livenessList); config.setLivenessRandom(ExampleApplication.isLivenessRandom); config.setBlurnessValue(FaceEnvironment.VALUE_BLURNESS); config.setBrightnessValue(FaceEnvironment.VALUE_BRIGHTNESS); config.setCropFaceValue(FaceEnvironment.VALUE_CROP_FACE_SIZE); config.setHeadPitchValue(FaceEnvironment.VALUE_HEAD_PITCH); config.setHeadRollValue(FaceEnvironment.VALUE_HEAD_ROLL); config.setHeadYawValue(FaceEnvironment.VALUE_HEAD_YAW); config.setMinFaceSize(FaceEnvironment.VALUE_MIN_FACE_SIZE); config.setNotFaceValue(FaceEnvironment.VALUE_NOT_FACE_THRESHOLD); config.setOcclusionValue(FaceEnvironment.VALUE_OCCLUSION); config.setCheckFaceQuality(true); config.setLivenessRandomCount(2); config.setFaceDecodeNumberOfThreads(2); config.setSound(true); FaceSDKManager.getInstance().setFaceConfig(config); } private void requestGranted() { boolean isAllGranted = checkPermissionAllGranted(new String[] { Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE }); // 3??, ? if (isAllGranted) { //doBackup(); return; } /** * 2 : ?? */ // ??, ??? ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE, }, MY_PERMISSION_REQUEST_CODE); } private boolean checkPermissionAllGranted(String[] permissions) { for (String permission : permissions) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { // ????, false return false; } } return true; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == MY_PERMISSION_REQUEST_CODE) { boolean isAllGranted = true; // ???? for (int grant : grantResults) { if (grant != PackageManager.PERMISSION_GRANTED) { isAllGranted = false; break; } } if (isAllGranted) { // ??, ? //doBackup(); } else { // ????, ????? openAppDetails(); } } } private void openAppDetails() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage( "? Camera? ? ? -> ??? ?"); builder.setPositiveButton("?", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("package:" + getPackageName())); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); startActivity(intent); } }); builder.setNegativeButton("?", null); builder.show(); } }