将 PNG Canvas 图像保存到 HTML5 存储 (JAVASCRIPT)?
我正在开发一个 chrome 扩展。
我在画布中打开一个图像文件,对其应用一些更改,然后尝试将其保存到 HTML5 文件系统 api。
首先,我从画布中获取 dataURL:
var dataURL = canvas.toDataURL('image/png;base64');
然后只是数据:
var image64 = dataURL.replace(/data:image\/png;base64,/, '');
然后我制作一个 Blob。
var bb = new BlobBuilder();
bb.append(image64);
var blob = bb.getBlob('image/png');
然后我使用以下函数 onInitFs(); 请求文件系统;
function onInitFs(fs) {
fs.root.getFile('image.png', {create: true}, function(fileEntry) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
//WRITING THE BLOB TO FILE
fileWriter.write(blob);
}, errorHandler);
}, errorHandler);
}
window.requestFileSystem(window.PERSISTENT, 5*1024*1024, onInitFs, errorHandler);
这会导致将损坏的文件写入文件系统。
我不知道我还能做些什么来完成这项工作。
有人可以指导我正确的方向吗?
以下是我用来完成此任务的函数的一些来源。
http://dev.w3.org/html5 /canvas-api/canvas-2d-api.html#todataurl-method
http://www.html5rocks.com/en/tutorials/file/文件系统/#toc-file-creatingempty
谢谢!
I am developing a chrome extension.
I open an image file in canvas, I apply some changes to it, then I am trying to save it to the HTML5 filesystem api.
First I get the dataURL from the canvas:
var dataURL = canvas.toDataURL('image/png;base64');
Then just the data:
var image64 = dataURL.replace(/data:image\/png;base64,/, '');
Then I make a Blob.
var bb = new BlobBuilder();
bb.append(image64);
var blob = bb.getBlob('image/png');
Then I request the file system with the following function onInitFs();
function onInitFs(fs) {
fs.root.getFile('image.png', {create: true}, function(fileEntry) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
//WRITING THE BLOB TO FILE
fileWriter.write(blob);
}, errorHandler);
}, errorHandler);
}
window.requestFileSystem(window.PERSISTENT, 5*1024*1024, onInitFs, errorHandler);
This results in a corrupted file being written to the file system.
I don't know what else I can do to make this work.
Could someone please guide me in the right direction.
The following are some of the sources to the functions I am using to accomplish this task.
http://dev.w3.org/html5/canvas-api/canvas-2d-api.html#todataurl-method
http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-file-creatingempty
Thank You!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我找到了一个将数据 URL 转换为 blob 的函数。
当您需要将画布图像保存到沙盒文件系统时非常有用。适用于 Chrome 13。
用法:
源代码跟踪:
http://mustachified.com/master.js
通过 http://lists.whatwg.org/pipermail/whatwg-whatwg.org /2011-4月/031243.html
通过 https://bugs.webkit.org/show_bug.cgi?id=51652
通过 http://code.google.com/p/chromium/issues/detail?id=67587
I've found a function that converts a data URL to a blob.
Great for when you need to save a canvas image to the sandboxed FileSystem. Works in Chrome 13.
Usage:
Source trail:
http://mustachified.com/master.js
via http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-April/031243.html
via https://bugs.webkit.org/show_bug.cgi?id=51652
via http://code.google.com/p/chromium/issues/detail?id=67587
现在有一个 canvas.toBlob() 填充: https://github.com/eligrey/canvas- toBlob.js
其他有趣的链接: https://github.com/eligrey/BlobBuilder.js
所以有了这些从画布保存图像变得容易:
这是一个使用 FileSaver 的小示例: http://jsfiddle.net/JR5XE/< /a>
There is now a canvas.toBlob() polyfill: https://github.com/eligrey/canvas-toBlob.js
Other interesting link: https://github.com/eligrey/BlobBuilder.js
So with these it become easy to save image from canvas:
Here is a little example using the FileSaver: http://jsfiddle.net/JR5XE/
您似乎直接将 Base64 表示形式写入磁盘。您需要先对其进行解码。
You seem to be directly writing the base64 representation to disk. You need to decode it first.
我一直在想同样的事情,并试图找到答案。
据我所知,您必须将从 atob 获得的原始字符串转换为 unit8array,然后可以将其附加到 blob。
下面是一个示例,它将图像转换为画布,然后将画布中的数据转换为 blob,然后将第三个图像 src 设置为 blob 的 uri...您应该能够从那里添加 fs 内容。 ...
http://jsfiddle.net/PAEz/XfDUS/
..很抱歉没有将代码放在这里,但说实话,我不知道如何放;)无论如何,JSFiddle 是一种很好的使用方式。
参考文献
https://gist.github.com/1032746
http://code. google.com/p/html5wow/source/browse/src/demos/photo-gallery/js/utils.js
在 JavaScript 中获取图像数据?
http://jsdo.it/tsmallfield/typed_array
I had been wondering the same thing and had a look at finding an answer.
From what I can tell you have to convert the raw string you get from an atob to an unit8array and then you can append it to a blob.
Here's an example that converts an image to a canvas and then the data from the canvas to a blob and then the third images src is set to the uri of the blob....you should be able to add the fs stuff from there....
http://jsfiddle.net/PAEz/XfDUS/
..sorry to not put the code here, but to be honest I couldnt figure out how to ;) and anyways, JSFiddle is a nice way to play with it.
References
https://gist.github.com/1032746
http://code.google.com/p/html5wow/source/browse/src/demos/photo-gallery/js/utils.js
Get image data in JavaScript?
http://jsdo.it/tsmallfield/typed_array
您需要
HTMLCanvasElement.toBlob
,然后使用 W3C 文件系统 API 将 blob 写入文件系统,但大多数浏览器尚未实现它。You want
HTMLCanvasElement.toBlob
and then write the blob to a filesystem with the W3C filesystem API, but most browsers haven't implemented it yet.