我有HTML画布,它可以很好地显示图像。
我有这个jquery代码下载图像:
$(".img-download").click(function(){
var data = canvas.toDataURL();
download.href = data; -- I tried this to download image, but PNG file generated cannot be opened in photoshop or any other image manipulation apps
alert (data); -- then, I tried to see what's actually the data content
});
这是我的下载按钮:
<a href="" class="img-download btn btn-lg btn-default" download="image.png">Download</a>
这里有data
内容:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjAAAAH0CAYAAAAjYBdQAAAgAElEQVR4XrS9d7Cm6Vnm93Sf0DlMTp0naZSFTCokYNf2er2mbBZvFcG4WGxClQtThVibUpk/EFCLjQoEJmgUkECkrRImLMqiKEmIIIE0EsrSaKIm9/R0DqfP6fb1u677ft9vpJGE7Nqv58w53/e97xPu5w7XHZ7n3fSrv/rblzdt2jzys2lsbKyPy5suD16X9WvT2DyWNm8em/WT1yW+GZs36a/Ll/QXf2/yvZcv5T7uujT9vVnt6WL9LC/pzkvr4+LauXHu7Olx/vxpt7Okpi9d2lB/G2lf/V9W5xuXLo3N+vKy2qNNfjbWL42lpSX/MED6vKzrGIcG6b8Z3yY1TBvctbx5eawzL413WR/Q58rW1bFtxw5dR/uZg+9byn28mPP6+rr/XmIcG/pedPLXvp5xp2/aps/QSWPf2DDdGLPpwk2Mx9MwYUMzN3bZ7fC+vpjm6zXQ55sZtNYia+JWTR/ebFZ7lzZ0v77XSrh9+qflzZtFJ415aWnZffV85vWk+9Dssm7lms36Y/OmpbFrx65x1dXXjOUtq15r+twsuq+vb4wTJ06MCxcujJ07d+hn59gmerImyysr7tN9aU039MPvZfXP2OAvj19jUqPVt9ZoZXXs3rt3bN22bZw4eXI8dfwpX3uJ8XC9aE9/58+fdz9bt24ND7it8FfIDIHCrxsaM/0v+f2SyJI1oM26JRRjLPpv06ZL4r5L4pWN0JzPWWOuh59MUfGxaAOx6MNcU/wCDTOerNNl+vXa5Tro7DH4ojG2bN0ydu/eM5aXV8fpM2fGGf2wjh4jvK8+t4r220STVdOXdjMG5Gvt4vo4d+68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7ytBeBgAAAABJRU5ErkJggg==
为什么它会成功生成PNG文件,但为什么它无法打开?为什么PNG文件已损坏?
更新:这是我完整的jquery脚本
$(".pepe-thumbnail").click(function(){
var pepe_src = $(this).attr("src");
$("#canvasimg").attr("src", pepe_src);
});
var canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
var deviceWidth = window.innerWidth;;
canvasWidth = deviceWidth - 40;
canvasHeight = deviceWidth - 40;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
if (canvas.width > 500 || canvas.height > 500){
canvas.width = 560;
canvas.height = 500;
}
var img = document.getElementById("canvasimg");
//imgx = canvas.width/2 - img.width/2;
//imgy = canvas.height/2 - img.height/2;
imgx = 0;
imgy = 0;
function runLoop(){
ctx.lineWidth = 8;
ctx.font = "26pt Lato";
ctx.strokeStyle = "black";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.lineJoin = "round";
var text1 = document.getElementById("canvastext-top").value;
//text1 = text1.toUpperCase();
var text2 = document.getElementById("canvastext-bottom").value;
//text2 = text2.toUpperCase();
x = canvas.width/2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, imgx, imgy, canvas.width, canvas.height);
ctx.strokeText(text1, x, 50);
ctx.fillText(text1, x, 50);
ctx.strokeText(text2, x, (canvas.height - 40));
ctx.fillText(text2, x, (canvas.height - 40));
window.setTimeout(runLoop, 14);
};
runLoop();
$(".img-download").click(function(){
var data = canvas.toDataURL();
alert (data);
});
答案 0 :(得分:2)
试试这个:
$(".img-download").click(function(){
var data = canvas.toDataURL();
$(this).attr("href",data)
$(this).attr("download","imgName.png");
});
答案 1 :(得分:0)
https://{...}/_apis/wit/classificationNodes/iterations?$depth=2