离子 - 当allowEdit为真时“相机取消”

时间:2016-12-28 11:21:37

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

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

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

$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()回调打印错误“相机取消”。在我测试过的各种Android版本的5部手机中,有3个出现了这个错误。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

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

$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
            })
        }

希望这有帮助!

相关问题