2017-04-21 52 views
2

我正在嘗試運行Google Vision FaceTracker,但我在CameraSourcePreview的一行代碼中有錯誤。Google的VisionAPI示例FaceTracker相機權限

這是錯誤 - 呼叫需要可以由用戶被拒絕權限:代碼應明確檢查,看是否許可可用(與checkPermission)或明確處理潛在SecurityException

這是功能 -

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

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

     //...other code 

     mStartRequested = false; 
    } 
} 
+0

嗨。看看FaceTracker需要什麼權限,然後通過執行int permission = checkPermission(mContext,Manifest.permission.WRITE_EXTERNAL_STORAGE)來請求它們。 (我的例子是文件寫作,你可能會用於其他的東西)。檢查權限方法如下所示int permissionResult = ActivityCompat.checkSelfPermission(context,permission); – Smashing

+0

謝謝!解決了這個問題,但是當我運行它的應用程序,只是給我一個部分黑屏:( – Kek

+0

你也檢查了CAMERA權限嗎? –

回答

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; 
    } 
}