我需要编写在phonegap中使用相机API的html代码,并将图像设置为定义的画布。我在网上发现了一些应该有效的代码(也包括水印),但它不会向画布返回任何内容。有人能告诉我我错过了什么吗?我是html和javascript的新手,只是试图弄清楚这个特定的部分是如何工作的,然后再扩展它以包含其他图像叠加在从相机拍摄的照片的顶部。
由于
<!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>
答案 0 :(得分:3)
如果你的代码不需要画布,你可以使用这个fiddle中概述的简单干净的方法:
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
或者,如果你确实需要使用画布,请参考this fiddle,这表明你可以拍摄一个程序化的图像元素并将其绘制到画布上:
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);
}
}
让我知道你如何继续:)