【问题标题】:Ionic - "camera cancelled" when allowEdit is trueIonic - 当 allowEdit 为真时“相机被取消”
【发布时间】:2017-05-12 16:14:00
【问题描述】:

我正在尝试在将图像上传到我的 Ionic 应用程序之前帮助裁剪图像。当我从图片库中选择图像时它工作正常,但是当直接从相机拍摄照片时它会失败,原因是“相机已取消”

我正在使用 Cordova 的基本相机插件 - cordova-plugin-camera。这是我的代码的相关sn-p:

$scope.openCamera = function () {
    navigator.camera.getPicture(
        onSuccess(),
        onFailure(),
        {
            allowEdit: true,
            quality: 100,
            sourceType: navigator.camera.PictureSourceType.CAMERA,
            destinationType: navigator.camera.DestinationType.FILE_URI,
            encodingType: Camera.EncodingType.JPEG,
            mediaType: Camera.MediaType.PICTURE,
            targetWidth: 100,
            targetHeight: 100,
            correctOrientation: true
        }
    );
}

相机可以正确捕捉照片并显示图像以让我选择要裁剪的区域。但是,它失败并进入 onFailure() 而不是 onSuccess()。
onFailure() 回调打印错误“相机已取消”。这个错误出现在我测试过的 5 部不同 Android 版本的手机中的 3 部。

任何帮助将不胜感激!

【问题讨论】:

  • 你找到答案了吗?我也有类似的问题。
  • @MarkA.Rupert 我最终使用了 cordova-plugin-crop 来做到这一点。基本上,我的 onSuccess() 函数调用了裁剪插件,它处理 targetWidth、targetHeight 等。

标签: cordova ionic-framework camera phonegap-plugins cordova-plugins


【解决方案1】:

我最终使用 cordova-plugin-crop 作为一种变通方法,以在所有 Android/iOS 设备上获得我想要的结果。这是我更新后的代码的 sn-p:

$scope.openCamera = function () {
    navigator.camera.getPicture(
        onSuccess(),
        onFailure(),
        {
            quality: 100,
            destinationType: destinationType.FILE_URI
        }
    );
}

function onSuccess(fileURI) {
            plugins.crop.promise(fileURI, {
                quality: 100,
                destinationType: destinationType.FILE_URI,
                encodingType: Camera.EncodingType.JPEG,
                mediaType: Camera.MediaType.PICTURE,
                allowEdit: true,
                targetWidth: 100,
                targetHeight: 100,
            }).then(function success(newPath) {
                // Call API to upload the file
            }).catch(function fail(err) {
                // Handle failure
            })
        }

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2014-11-29
    • 2015-02-18
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    相关资源
    最近更新 更多