[英]How do I insert a PNG comment block when saving an HTML5 canvas using javascript toDataURL?
I have a compact canvas-to-png download saver function (see code below). 我有一个紧凑的canvas-to-png下载保护程序功能(请参见下面的代码)。 This code works very well and I am satisfied with its output... mostly.
这段代码非常好用,我对它的输出感到满意。 Would a second replace suffice?
再更换一次就足够了吗? What would that replace look like?
那会是什么样子呢? My only other option is to post-process the file with imagemagick.
我唯一的其他选择是使用imagemagick对文件进行后期处理。 Any ideas?
有任何想法吗?
More completely: I want to add metadata from javascript. 更完整地说:我想从javascript添加元数据。 I found this link http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_PNG_files which details the structures, and I may be able to figure it out with sufficient time.
我发现此链接http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_PNG_files详细介绍了结构,我也许可以花足够的时间弄清楚它。 If anyone has experience and can shorten this for me, I would appreciate it.
如果有人有经验并且可以为我缩短时间,我将不胜感激。
//------------------------------------------------------------------
function save () // has to be function not var for onclick to work.
//------------------------------------------------------------------
{
var element = document.getElementById("saver");
element.download = savename;
element.href = document.
getElementById(id.figure1a.canvas).
toDataURL("image/png").
replace(/^data:image\/[^;]/,'data:application/octet-stream');
}
The Base-64 representation has little to do with the internal chunks. Base-64表示与内部块无关。 It's just [any] binary data encoded as string so it can be transferred over string-only protocols (or displayed in a textual context).
它只是编码为字符串的[任何]二进制数据,因此可以通过仅字符串协议传输(或在文本上下文中显示)。
It's perhaps a bit broad to create an example, but hopefully showing the main steps will help to achieve what you're looking for: 创建示例可能有点宽泛,但是希望展示主要步骤将有助于实现您想要的目标:
toBlob()
). toBlob()
)。 new Uint8Array(arraybuffer, 0, position);
new Uint8Array(arraybuffer, 0, position);
- you can also use the subarray method. new Uint8Array(arraybuffer, position, length - position);
new Uint8Array(arraybuffer, position, length - position);
var newPng = new Blob(partArray, {type: "image/png"})
;). var newPng = new Blob(partArray, {type: "image/png"})
;)。 This will now contain the custom chunk. *) Chunk: *)块:
tEXt
be aware of it is limited to the Latin-1 charset which means you'll have to whitewash the string you want to use - use iTXt
for unicode (UTF-8) content - we'll use tEXt
here for simplicity. tEXt
注意它仅限于Latin-1字符集,这意味着您将必须粉刷您要使用的字符串-将iTXt
用于Unicode(UTF-8)内容-为了简单起见,我们在此使用tEXt
。 tEXt
chunk, and the keyword must be exactly typed as defined in the spec . tEXt
块中用NUL字节(0x00) tEXt
,并且关键字必须按照规范中的定义正确键入。 Uint32 - length of chunk (data only in number of bytes) Uint32 - "tEXt" as four-cc [...] - The data itself (copy byte-wise) Uint32 - CRC32* which includes the FourCC but not length and itself.
All data in a PNG is big-endian. PNG中的所有数据都是big-endian。
To calculate CRC-32 feel free to use this part of my pngtoy solution (the LUT is built this way ). 要计算CRC-32,请随意使用pngtoy解决方案的这一部分 (以这种方式构建LUT)。 Here is one way to format a four-cc:
这是格式化四抄送的一种方法:
function makeFourCC(n) { // n = "tEXt" etc., big-endian
var c = n.charCodeAt.bind(n);
return (c(0) & 0x7f) << 24 | (c(1) & 0x7f) << 16 | (c(2) & 0x7f) << 8 | c(3) & 0x7f
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.