科尔多瓦takePhoto无法正常工作

时间:2017-01-04 03:59:41

标签: ios cordova mobile cordova-plugins

我正在开发一款可以拍照的应用,并且我正在使用Cordova学习。我用过这个js:

var takePhoto = function() {
    navigator.camera.getPicture(onSuccess, onError, { quality: 30,
        destinationType: Camera.destinationType.FILE_URI,
        saveToPhotoAlbum: true,
        targetWidth: 1500,
        targetHeight: 1500
    });

    function onSuccess(imageData) {
        $('.photoPreview').show();
        var image = document.getElementById('photoSrc');
        image.src = "data:image/jpeg;base64," + imageData;
        photoData = imageData;
        $('#photoMessage').html('');
    }

    function onError(message) {
    }
}

每当我点击按钮时,它都不起作用,没有任何反应。我用来选择现有照片的功能也是如此:

var selectPhoto = function() {
    navigator.camera.getPicture(onSuccess, onFail, { quality: 30,
        destinationType: Camera.destinationType.FILE_URI,
        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
        targetWidth: 1500,
        targetHeight: 1500
    });

    function onSuccess(imageData) {
        $('.photoPreview').show();
        var image = document.getElementById('photoSrc');
        image.src = "data:image/jpeg;base64," + imageData;
        photoData = imageData;
        $('#photoMessage').html('');
    }

    function onFail(message) {
    }
}

并使用此

向我添加信息
$('#selectPhoto').on('click', function() {
    selectPhoto();
});

$('#takePhoto').on('click', function() {
    takePhoto();
});

和这个HTML

 <button id="selectPhoto" class="photo-button">Select Existing Photo</button>
 <button id="takePhoto" class="photo-button">Take New Photo</button>

我使用最新的Cordova 6.4.0,cordova-ios@4.3.1以及来自https://github.com/apache/cordova-plugin-camera的最新cordova-plugin-camera

请告诉我应该怎么做。谢谢!

1 个答案:

答案 0 :(得分:0)

看起来您正在获取返回的imageData并将其作为base64数据直接注入DOM。如果您使用DATA_URL作为选项将相机选项更改为返回基数64,那将可以正常工作。总结:

navigator.camera.getPicture(onSuccess, onError, { quality: 30,
    destinationType: Camera.destinationType.DATA_URL,
    saveToPhotoAlbum: true,
    targetWidth: 1500,
    targetHeight: 1500
});

而不是:

navigator.camera.getPicture(onSuccess, onError, { quality: 30,
    destinationType: Camera.destinationType.FILE_URI,
    saveToPhotoAlbum: true,
    targetWidth: 1500,
    targetHeight: 1500
});

编辑: Jaze发现了真正的原因,“destinationType”需要大写。这很容易错过!请参阅下面的正确解决方案:

navigator.camera.getPicture(onSuccess, onError, { quality: 30,
    destinationType: Camera.DestinationType.DATA_URL,
    saveToPhotoAlbum: true,
    targetWidth: 1500,
    targetHeight: 1500
});