问题描述
我正在尝试运行 Google Vision FaceTracker 但是CameraSourcePreview
中的一行代码出现错误.
I'm trying to run Google Vision FaceTracker but I have an error on one line of code in CameraSourcePreview
.
这是错误-呼叫需要许可,而该许可可能会被用户拒绝:代码应显式检查以查看许可是否可用(使用checkPermission
)或显式处理潜在的SecurityException
This is the error - Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission
) or explicitly handle a potential SecurityException
这是功能-
private void startIfReady() throws IOException {
if (mStartRequested && mSurfaceAvailable) {
mCameraSource.start(mSurfaceView.getHolder()); //Error
//...other code
mStartRequested = false;
}
}
推荐答案
似乎没有获得FaceTracker所需的正确权限.根据您的需要,只需将facetracker权限添加到下面的实现和界面中即可.
在我们的应用程序中,我们添加了一个带有回调的接口,该接口可处理我们对我们的所有权限:
In our app we added an interface with a callback which handles all our permissions for us :
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;
}
}
这篇关于Google的VisionAPI示例FaceTracker Camera权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!