Phonegap camera draw on canvas distorted

耗尽温柔 提交于 2020-01-16 11:25:10

问题


I'm using Phonegap Camera Plugin to draw the captured image on a Canvas.

<canvas id="canvas"></canvas>

And the JS:

            var canvas = document.getElementById('canvas');
            var context = canvas.getContext('2d');

            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;

            var x = 0;
            var y = 0;
            var width = window.innerWidth;
            var height = window.innerHeight;
            var imageObj = new Image();

            imageObj.onload = function() {
                context.drawImage(imageObj, x, y, width, height);
            };
            imageObj.src = imageURI;

The projected images look distorted (streched, width is bigger than real).

How can I display the image ok, like in css width:100%; height:auto?


回答1:


If your image doesn't have the same ratio as your screen, it'll become stretched since you tell it to stretch to width and height: context.drawImage(imageObj, x, y, width, height);. You can try removing the width, height parameters.

You might have to calculate the scaling ratio:

var ratio;
if (imageObj.width>width) {
    ratio = width/imageObj.width;
}
else if (imageObj.height > height) {
    ratio = height/imageObj.height;
}
else {
    ratio = 1;
}
context.drawImage(imageObj, x, y, imageObj.width*ratio, imageObj.height*ratio);


来源:https://stackoverflow.com/questions/29151943/phonegap-camera-draw-on-canvas-distorted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!