Google的VisionAPI示例FaceTracker相机权限

时间:2017-04-21 02:01:07

标签: android android-camera google-vision

我正在尝试运行Google Vision FaceTracker,但我在CameraSourcePreview的一行代码中出错。

这是错误 - 调用需要用户可能拒绝的权限:代码应明确检查权限是否可用(使用checkPermission)或明确处理潜在的SecurityException

这就是功能 -

private void startIfReady() throws IOException {
    if (mStartRequested && mSurfaceAvailable) {

        mCameraSource.start(mSurfaceView.getHolder()); //Error

        //...other code

        mStartRequested = false;
    }
}

1 个答案:

答案 0 :(得分:0)

您似乎没有获得FaceTracker所需的正确权限。根据您的需要,只需将facetracker权限添加到以下实现和界面中。

在我们的应用程序中,我们添加了一个带回调的界面,该回调处理我们的所有权限:

public interface PermissionAndPackageAvailabilityChecker {

    void checkCameraPermission(final PermissionResult callback);

    void checkFileIoPermission(final PermissionResult callback);

    interface PermissionResult {
        void hasPermission();

        void noPermission();
    }
}

实施非常简单:

public class DefaultPermissionAndPackage implements PermissionAndPackageAvailabilityChecker {

    private final Context mContext;

    @Inject
    public DefaultPermissionAndPackage(Context context) {
        mContext = context;
    }

    @Override
    public void checkCameraPermission(final PermissionResult callback) {
        int cameraPermission = checkPermission(mContext, Manifest.permission.CAMERA);
        if (checkHasPermission(cameraPermission)) {
            callback.hasPermission();
        } else {
            callback.noPermission();
        }
    }

    @Override
    public void checkFileIoPermission(PermissionResult callback) {
        // Check if we have write permission
        int permission = checkPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (checkHasPermission(permission)) {
            callback.hasPermission();
        } else {
            callback.noPermission();
        }
    }

    private int getTargetSdk(int defaultVersion) {
        int targetSdkVersion = defaultVersion;
        try {
            PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            targetSdkVersion = packageInfo.applicationInfo.targetSdkVersion;

        } catch (PackageManager.NameNotFoundException ignored) {
            //Should not happen . . . I hope
        }
        return targetSdkVersion;
    }

    private int checkPermission(final Context context, final String permission) {
        int permissionResult = ActivityCompat.checkSelfPermission(context, permission);
        // this can probably be simplified but explains the logic around permissions nicely.
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1
                && getTargetSdk(Build.VERSION_CODES.LOLLIPOP_MR1) <= Build.VERSION_CODES.LOLLIPOP_MR1) {
            permissionResult = PermissionChecker.checkSelfPermission(context, permission);
            //Will check marshmallow here in the future
        }
        return permissionResult;
    }

    private boolean checkHasPermission(int permissionToCheck) {
        return permissionToCheck == PackageManager.PERMISSION_GRANTED;
    }
}