繁体   English   中英

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

[英]How to check camera permissions before invoking camera.getPicture

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

之所以需要这样做,是因为当用户意外拒绝对摄像机的许可时,他将不得不导航到操作系统本身中的应用程序许可才能更改许可。 大多数用户可能对此一无所知,因此我想让他们知道。

在下面的示例中,我想检查应用程序是否有权访问相机。 如果不是,请通知用户。
我怎样才能做到这一点?

        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([]);
            }
        }

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

我做了以下事情:

  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

之后,我在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);
    }
}

我拒绝了摄像头的访问并关闭了该应用程序。 重新打开它,然后按文本以启动相机后,我收到一条消息,直接告诉我,无法访问相机。 插件进一步询问我是要打开设置还是只想关闭相机。 请参见下面的屏幕截图。

问题必须在代码内的任何地方,否则您可能正在使用不赞成使用的相机插件? 您是否尝试更新它?

在此处输入图片说明

要在使用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();

        }
    });

希望这可以为某人节省时间。

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM