How to use camera API and set image to canvas tag

I need to write html code that uses the camera API in phonegap and sets the image to a specific canvas. I found an online code that should work (also includes a watermark), but it does not return anything to the canvas. Can someone tell me what I am missing? I am new to html and javascript and just trying to figure out how this particular part works before I expand it to include other images in order to overlay on top of the image taken from the camera.

thanks

<!DOCTYPE html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-    1.3.2.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<script src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">

var canvas;
var watermark;

document.addEventListener("deviceready",onDeviceReady,false);  

function onDeviceReady() {
    canvasDOM = $("myCanvas")[0];
    canvas = canvasDOM.getContext("2d");

    watermark = new Image();
    watermark.src = "q1.jpg";    
} 

function cybershot() {
    navigator.camera.getPicture(camSuccess, camError, {quality: 75, targetWidth: 400,     targetHeight: 400, destinationType: Camera.DestinationType.FILE_URI});
}

function camError(e) {
    console.log("Camera Error");
    console.log(JSON.stringify(e));
}


function camSuccess(picuri) {
    console.log("Camera Success");

    var img = new Image();
    img.src = picuri;

    img.onload = function(e) {
        canvas.drawImage(img, 0, 0);
        canvas.drawImage(watermark, canvasDOM.width-watermark.width, canvasDOM.height - watermark.height);
    }  
} </script>


<style>
#myCanvas {
    width: 400px;
    height: 400px;
}
</style>


</head>
<body>
<h1>Watermark Camera</h1>


<button onclick="cybershot();">Capture Photo</button> <br>


<p>Canvas:</p>"

<canvas id="myCanvas"></canvas>
</body>
</html>
+4
source share
1 answer

, , fiddle:

function onSuccess(imageData) {
    var image = document.getElementById('myImage');
    image.src = "data:image/jpeg;base64," + imageData;
}

, , , , :

function onSuccess(imageData) {
    var image = new Image();
    image.src = "data:image/jpeg;base64," + imageData;
    image.onload = function () {
        var height = 100;
        var width = 100;
        context.drawImage(image, width, height);
    }
}

, :)

+3

All Articles