为什么生成渲染空图像文件的画布?
Why produce the canvas rendering an empty image file?
我的目标:
如果我的网站上的相机按钮被点击>打开智能手机相机>从qr-code拍一张照片>用js调整它的大小>在我的服务器上加载图片>读取qr-code>给我从qr-code返回的值
我的问题是,我的代码产生一个空的图像文件(jpg),并给我一个错误。
我用上传一个现有的qr码图像来测试它,它在第二次尝试上传它时有效。在第一次尝试,我得到相同的错误,如果我试图上传从智能手机拍摄的照片。
我html<form id="qr-code-upload-form" class="form-group" action="" method="post" enctype="multipart/form-data">
<label>
<span id="qr-pic" class="glyphicon glyphicon-camera"></span>
<input type="file" capture="camera" accept="image/*" id="cameraInput" name="cameraInput" style="display: none;">
</label>
<canvas id="cnvsForFormat" width="400" height="266" style="display:none;"></canvas>
</form>
我的js/jquery
$(document).ready(function(){
$('body').on("change", "#cameraInput", readCode);
function readCode() {
var cnvs=document.getElementById("cnvsForFormat");
var input = document.getElementById('cameraInput').files[0];
var img = new Image();
if (input) {
var reader = new FileReader();
reader.onload = function (e) {
//Get the input image file
img.src = e.target.result;
var newHeight = img.height/2;
var newWidth = img.width/2;
//reduce the size until its lower than 600px
while (newWidth>600 || newHeight>600) {
newHeight = newHeight/2;
newWidth = newWidth/2;
}
$("#cnvsForFormat").attr("height",newHeight);
$("#cnvsForFormat").attr("width",newWidth);
//Draw the input image in a canvas with the right size for the upload
var ctx=cnvs.getContext("2d");
ctx.drawImage(img,0,0, newWidth, newHeight);
var resizedImg = cnvs.toDataURL("image/jpeg");
//Send the resized Image to my php-request-file
$.post("?cmd=ajax¶m=qr_verarbeiten",{photo:resizedImg},function(e) {
alert(e);
});
};
reader.readAsDataURL(input);
}
};
});
</script>
我的PHP读取qr-code
if (isset($_GET['param']) && $_GET['param'] == "qr_verarbeiten" && isset($_POST['photo'])) {
$img = base64_decode(substr($_POST['photo'],23));
file_put_contents("img/upload/qr.jpg", $img);
$qrcode = new QrReader("img/upload/qr.jpg");
echo $qrcode->text();
}
我在这个项目中使用了jQuery, Bootstrap和SMARTY
我真的希望有人能帮助我!
感谢你花时间阅读这篇文章,我为我的糟糕英语道歉。
问题在js中。我已经添加了js/MegaPixImage库来处理我从iPhone相机获得的大文件-工作代码是:
$(document).ready(function(){
$('body').on("change", "#cameraInput", readCode);
function readCode() {
// var cnvs=document.getElementById("cnvsForFormat");
var input = document.getElementById('cameraInput').files[0];
if (input) {
var img = new Image();
var reader = new FileReader();
reader.onload = function (e) {
img.onload = function() {
var count=0;
while (img.width<1) {
count++;
}
var newHeight = img.height;
var newWidth = img.width;
while (newWidth>600 || newHeight>600) {
newHeight = newHeight/2;
newWidth = newWidth/2;
}
var renderImg = new Image();
var mpImg = new MegaPixImage(img);
mpImg.render(renderImg, { width: newWidth, height: newHeight });
$.post("?cmd=ajax¶m=qr_verarbeiten",{photo:renderImg.src},function(e) {
alert(e);
});
};
img.src = e.target.result;
};
reader.readAsDataURL(input);
}
};
});
我明白了。这是因为浏览器加载了img。src asynchron。所以我要加一个img。加载img之前的Onload函数。SRC以确保img已经渲染。: -)
我已经将工作代码添加到我的问题中。
相关文章:
- 如何使用谷歌应用程序脚本从电子邮件中提取内联文件/图像
- 当我在操作属性中写入页面名称时,文件图像不会上传
- 将已编辑的文件(图像)数组发送到php脚本
- 预览上传的文件(图像)
- 使用 src 在 PHP 中链接文件(图像,js)
- 通过javascript处理默认配置文件图像的更好方法
- 使我的想法成为可能,为画布输入文件图像命名
- 跨原点图像加载(twitter配置文件图像)
- 输入文件(图像)显示空数组,如果数组中有数据的话
- 如何通过tastypie上传文件/图像
- 上传到nodejs时损坏的文件/图像
- 获取上载的文件/图像的名称
- Django:除主页模板外,静态文件图像URL路径中断
- Parsley验证文件图像没有'不起作用
- 如何在单击'时设置文件/图像的名称;将图像另存为'
- 上载要分析的配置文件图像
- 将数据库mysql中的文件图像插入excel
- 如何使用twitter api(js)获取用户配置文件图像
- 在javascript中从本地驱动器读取文件/图像
- 使用ajax上传文件图像时页面冻结