首页 文章

Windows Phone上的相机插件会创建方形图像

提问于
浏览
1

我在我的Cordova应用程序中使用了camera plugin . targetWidthtargetHeight 可用于调整图像大小,但与the documentation says相反,它不能保持纵横比 . 指定800像素的目标宽度和高度应确保图像的一面为800px,另一面基于该面和纵横比计算 . 然而,情况并非如此 - 它只是创建一个800 x 800的方形图像 .

有没有人对如何解决这个问题有任何想法?它's looks like it'是issue for a while但它显然还没有修复 .

1 回答

  • 0

    我有一个解决方案,虽然这是一个黑客攻击 .

    1)在Windows Phone项目中,打开 www/plugins/cordova-plugin-camera/src/CameraProxy.js

    2)找到 resizeImage 方法并添加以下内容:

    function resizeImage(successCallback, errorCallback, file, targetWidth, targetHeight, encodingType) {
    
        ...
        image.onload = function() {
                var imageWidth = targetWidth,
                    imageHeight = targetHeight;
                var canvas = document.createElement('canvas');
                var storageFileName;
    
                /* ====== INSERT THIS CODE ==== */
    
                // Maintain aspect ratio (make sure image size is no bigger than target width/height).
                if (image.width < image.height) {
                    var aspectRatio = image.width / image.height;
                    imageWidth = imageHeight * aspectRatio;
                }
                else {
                    var aspectRatio = image.width / image.height;
                    imageHeight = imageWidth / aspectRatio;
                }
                /* ====== END OF CODE ADDITION ==== */
    
                canvas.width = imageWidth;
                canvas.height = imageHeight;
    

    注意:每次重新构建应用程序时,此更改都将丢失 . 您可以更改插件/ cordova-plugin-camera / windows / CameraProxy.js来解决此问题,但是,这可能会导致升级问题,因为Cordova可能无法更新已修改的文件 .

相关问题