将svg转换为画布并获得base64字符串(画布)
Converting svg to canvas and get the base64 String (canvg)
我们在Java中使用canvas https://code.google.com/p/canvg/的本地方法将SVG字符串从画布转换为png图像。我们需要一个renderCallback,因为SVG字符串中的图像需要时间来渲染,否则我们会得到一个错误的构造。我们的代码是:
private native String PNGExport(String SVGString)/*-{
var canvas = $doc.createElement('canvas');
canvas.id = "testcanvas";
var img;
$wnd.canvg(canvas, SVGString, {
renderCallback : function() {
img = canvas.toDataURL("image/png");
var window = $wnd.open();
window.document.write('<img src="' + img + '"/>');
},
});
return img;
}-*/;
因此浏览器将打开一个包含正确png图像的选项卡。但是是否有可能得到这个字符串img = canvas.toDataURL("image/png");在回调之外工作?我能想到的唯一方法是在函数()中调用Java方法,它将img var作为参数。我希望有更好的解决办法。
我们通过调用java方法来解决这个问题:
private native void PNGExport(String SVGString)/*-{
var canvas = $doc.createElement('canvas');
canvas.id = "testcanvas";
var theInstance = this;
var img;
$wnd.canvg(canvas, SVGString, {
renderCallback : function() {
img = canvas.toDataURL("image/png");
( function b(p) {
theInstance.@de.user.client.component.graphic.UsERGraphicEditor::saveMedium(Ljava/lang/String;)(p);
})(img);
},
});
}-*/;
必须通过方法的路径调用函数。
相关文章:
- 如何在jquery、javascript、HTML5中以base64字符串保存为(PDF、doc、xls、png.)
- 如何在JavaScript/Node中计算SHA256哈希和Base64字符串编码
- 将Byte数组转换为Angularjs中的Base64字符串
- 转义URL中的Base64字符串
- base64字符串使用Node.JS被写为无效图像
- 图像大小与其base64字符串转换长度之间的比率是多少
- 将内联SVG转换为Base64字符串
- 如何使用 javascript 将 png 转换为 base64 字符串
- Phonegap - 如何从base64字符串生成图像文件
- 从本地存储 base64 字符串设置背景图像
- 如何将 Base64 字符串转换为 javascript 文件对象,就像从文件输入表单一样
- 带有 base64 字符串(数据 URI)的 HTML 图像标记
- 将 Base64 字符串从 AS3 返回到 Javascript.这是一个错误或功能(还是我错了?
- 在javascript中将base64图像转换为文件,或者如何使用jquery ajax传递一个大的base64字符串
- Javascript:从图像URL获取Base64字符串
- 从 Base64 字符串保存图像不会打开
- 从图像 URL 获取 Base64 字符串
- 我无法从画布获取 Base64 字符串
- JavaScript:从图像中删除字节(base64 字符串)
- JSZip 不会从 base64 字符串加载 zip