javascript canvas.toDataURL 与原始数据 URI 相比发生变化
我目前正在开发一个 mozilla 扩展,尝试加载使用数据 URI 编码的图像(PNG),将其绘制在画布元素上,最终更改一些像素值并将其作为文件保存在磁盘上。
我注意到的奇怪的事情是,即使我没有更改图像上的任何内容,只是在画布上绘制图像并使用 canvas.toDataURL() 来查看生成的内容,此编码数据也与原始数据不同。
我用来查看这一点的代码非常基本:
var image = new Image();
image.onload = function() {
var canvas = document.createElement('canvas')
canvas.width = image.width;
canvas.height = image.height;
canvas.getContext('2d').drawImage(image, 0, 0);
var data = canvas.toDataURL(); // this right here is different from image.scr data!
}
image.src = "data:image/png;base64," + encodedData;
我假设存在某种压缩或者可能与正在进行的透明度相关。
我已检查文档但找不到解释。 我想我看到一些颜色被更改为其他类似的颜色,这可能会扰乱我想要实现的目标(即更改一些像素值)。
对正在发生的事情有什么想法吗?
如果没有,有人知道是否有其他方法可以在不使用 toDataURL 方法的情况下将画布图像保存到磁盘上的文件中? XPCOM 组件是否能够以 PNG 二进制格式保存所有像素?
谢谢
I'm currently developing a mozilla extension, trying to load an image (PNG) which is encoded with a data URI, draw it on a canvas element, to finally change some pixels values and save it back as a file on disk.
The strange thing I notice is that even if I don't change anything on the image and I just draw the image on the canvas and the use canvas.toDataURL() to see what is generated, this encoded data is different from the original.
The code I'm using to see this is pretty basic:
var image = new Image();
image.onload = function() {
var canvas = document.createElement('canvas')
canvas.width = image.width;
canvas.height = image.height;
canvas.getContext('2d').drawImage(image, 0, 0);
var data = canvas.toDataURL(); // this right here is different from image.scr data!
}
image.src = "data:image/png;base64," + encodedData;
I assume that there's some kind of compression or may be something related to transparencies going on.
I have checked the documentation but couldn't find the explanation.
I think I saw something about some colors being changed to some other similar, and that might be what is messing with what I want to achieve (that is changing some pixel values).
Any ideas on what is going on?
If not, does anyone know if there's any other way of saving the canvas image to a file on disk without using toDataURL method?
May be a XPCOM component capable of saving all the pixels in PNG binary format?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现在一种特殊情况下像素发生了变化。 可能还有更多案例。我无法提供深入的技术解释,因此,我不能说它实际上不会改变图像。
如果将 alpha 值设置为小于 255 (1.0),则该像素的其他组件似乎会发生变化。该问题的解决方法是简单地将所有 alpha 值设置为 255。
到目前为止,我成功地能够使用 ImageData 和 Image DOM 元素将精确的精度数据存储到 Canvas 中,然后通过设置检索完全相同的数据而不会造成任何损失将 alpha 分量设置为 255 并且不使用它来存储数据。
以下是我当前正在使用的例程:
然后获取数据的函数是:
There is one particular case that I have found where pixels have been changed. There may be more cases. I can not offer an in-depth technical explanation, thus, I can not say that it actually does not change the image.
If you set the alpha value to anything less than 255 (1.0) then the other components for that pixel seem to become altered. A workaround for the problem was to simply set all alpha values to 255.
As of this moment, I am successfully able to store exact precision data into Canvas using ImageData and an Image DOM Element and then retrieve this exact same data with no loss by setting the alpha component to 255 and not using it to store data.
Here are the routines that I am currently using:
Then the function to get the data back out is:
有多种合法方式无损过滤并编码 PNG。例如,请查看此图表,其中显示了使用不同格式对同一图像进行无损编码时可能存在的巨大文件大小差异。工具。
然而,如果您将 toDataURL 版本重新绘制到 Canvas 并获得新的 toDataURL 并且它有所不同,我会感到惊讶。我希望同一个浏览器每次都能对相同的图像进行相同的编码。
There are a variety of legal ways to losslessly filter and encode a PNG. For example, look at this chart showing the vast file size differences possible when losslessly encoding the same image with different tools.
I would be surprised, however, if you re-draw the toDataURL version to the Canvas and get a new toDataURL and it differs. I would expect the same browser to encode the same image identically each time.