如何在调用camera.getPicture之前检查摄像机权限

时间:2015-07-24 09:55:19

标签: javascript android ios cordova camera

我想确保应用程序在实际打开之前有权访问摄像头,这样如果应用程序没有权限,则可以通知用户他们需要更改操作系统中的权限。

这是必要的原因是因为当用户意外拒绝对摄像机的许可时,他必须导航到OS自身内的应用程序权限以更改权限。大多数用户可能不知道这一点,因此我想让他们知道。

在下面的示例中,我想检查应用程序是否有权访问摄像头。如果没有,请通知用户 我怎么能这样做?

        fromCamera: function (callback) {

            // PERMISSION CHECK HERE -> if camera permission is FALSE show an alert to notify the user
            navigator.notification.alert(
                "This app does not have access to the camera. Blabla do this blabla",
                ["Ok"]
            );

            if (callback === undefined) throw 'undefined callback parameter!';

            navigator.camera.getPicture(onCameraSuccess, onCameraFail, {
                quality: 90,
                encodingType: Camera.EncodingType.JPEG,
                saveToPhotoAlbum: true,
                allowEdit: false,
                correctOrientation: true,
                destinationType: Camera.DestinationType.FILE_URI
            });

            function onCameraSuccess(imageUri) {
                app.log('onCameraSuccess: ' + imageUri);
                callback([imageUri]);
            }
            function onCameraFail(message) {
                app.log('Failed because: ' + message);
                callback([]);
            }
        }

2 个答案:

答案 0 :(得分:1)

所以...我有点遗憾地说,但这不是相机插件的问题。

我做了以下事情:

  1. cordova create cameracheck com.example.com cameracheck
  2. cd cameracheck
  3. cordova platform add ios
  4. cordova plugin add cordova-plugin-camera
  5. cordova plugin add cordova-plugin-console
  6. cordova build
  7. 之后我在XCode中打开了应用程序并将代码编辑为标准代码。

    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received" onclick="openCamera()">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
    

    openCamera()功能

    function openCamera() {
        navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
                                    destinationType: Camera.DestinationType.FILE_URI });
    
        function onSuccess(imageURI) {
            var image = document.getElementById('myImage');
            image.src = imageURI;
        }
    
        function onFail(message) {
            alert('Failed because: ' + message);
        }
    }
    

    我拒绝了相机访问并关闭了应用程序。重新打开后,按下文本启动相机,我收到一条消息,直接告诉我,没有相机的访问权限。此外,插件问我,是否要打开设置或我是否只想关闭相机。请参见下面的截图。

    问题必须在您的代码中的任何位置,或者您可能使用不推荐的相机插件?你有没有尝试更新它?

    enter image description here

答案 1 :(得分:1)

要在使用cordova-camera-roll之前检​​查相机胶卷的许可,请使用:https://github.com/berliner/cordova-picture-access

使用cli安装:

cordova插件添加https://github.com/berliner/cordova-picture-access.git

然后在代码中:

window.plugins.pictureAccess.checkAccess(
  function() {
    // Go ahead and access the camera roll here



  },
  function() {
    // Inform the user that he has to give permission for access.
    // Ideally, ask for permission and try again.

    $cordovaDialogs.confirm('Access to the camera roll is switched off, please enable it in app settings to continue.', 'Whoops', ['OK','Settings'])
    .then(function(result) {

        if(result == 1) {
            // ok tapped no action

        }
        else if (result == 2) {
            // settings tapped, redirect
            cordova.plugins.settings.open();

        }
    });

由此我创建了一个用于检查相机权限的仓库......

要在使用捕获或视频捕获等之前检查相机的权限,请使用:https://github.com/antonfire/cordova-camera-access.git

使用cli安装:

cordova插件添加https://github.com/antonfire/cordova-camera-access.git

然后在代码中:

window.plugins.cameraAccess.checkAccess(
  function() {
    // Go ahead and access the camera here



  },
  function() {
    // Inform the user that he has to give permission for access.
    // Ideally, ask for permission and try again.

    $cordovaDialogs.confirm('Access to the camera is switched off, please enable it in app settings to continue.', 'Whoops', ['OK','Settings'])
    .then(function(result) {

        if(result == 1) {
            // ok tapped no action

        }
        else if (result == 2) {
            // settings tapped, redirect
            cordova.plugins.settings.open();

        }
    });

希望这为某人节省时间。

由于