将 PNG Canvas 图像保存到 HTML5 存储 (JAVASCRIPT)?

发布于 2024-11-16 18:49:38 字数 1434 浏览 9 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夕嗳→ 2024-11-23 18:49:39

我找到了一个将数据 URL 转换为 blob 的函数。

当您需要将画布图像保存到沙盒文件系统时非常有用。适用于 Chrome 13。

function dataURItoBlob(dataURI, callback) {
    // convert base64 to raw binary data held in a string
    // doesn't handle URLEncoded DataURIs
    var byteString = atob(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

    // write the bytes of the string to an ArrayBuffer
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }

    // write the ArrayBuffer to a blob, and you're done
    var bb = new window.WebKitBlobBuilder(); // or just BlobBuilder() if not using Chrome
    bb.append(ab);
    return bb.getBlob(mimeString);
};

用法:

window.requestFileSystem(window.PERSISTENT, 1024*1024, function(fs){
    fs.root.getFile("image.png", {create:true}, function(fileEntry) {
        fileEntry.createWriter(function(fileWriter) {
            fileWriter.write(dataURItoBlob(myCanvas.toDataURL("image/png")));
        }, errorHandler);
    }, errorHandler);
}, errorHandler);

源代码跟踪:

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.

function dataURItoBlob(dataURI, callback) {
    // convert base64 to raw binary data held in a string
    // doesn't handle URLEncoded DataURIs
    var byteString = atob(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

    // write the bytes of the string to an ArrayBuffer
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }

    // write the ArrayBuffer to a blob, and you're done
    var bb = new window.WebKitBlobBuilder(); // or just BlobBuilder() if not using Chrome
    bb.append(ab);
    return bb.getBlob(mimeString);
};

Usage:

window.requestFileSystem(window.PERSISTENT, 1024*1024, function(fs){
    fs.root.getFile("image.png", {create:true}, function(fileEntry) {
        fileEntry.createWriter(function(fileWriter) {
            fileWriter.write(dataURItoBlob(myCanvas.toDataURL("image/png")));
        }, errorHandler);
    }, errorHandler);
}, errorHandler);

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

冰火雁神 2024-11-23 18:49:39

现在有一个 canvas.toBlob() 填充: https://github.com/eligrey/canvas- toBlob.js
其他有趣的链接: https://github.com/eligrey/BlobBuilder.js

所以有了这些从画布保存图像变得容易:

<script type="text/javascript" src="https://raw.github.com/eligrey/BlobBuilder.js/master/BlobBuilder.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/eligrey/canvas-toBlob.js/master/canvas-toBlob.min.js"></script>
...
canvas.toBlob(function(blob) {
  fileWriter.write(blob);
}, "image/png");

这是一个使用 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:

<script type="text/javascript" src="https://raw.github.com/eligrey/BlobBuilder.js/master/BlobBuilder.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/eligrey/canvas-toBlob.js/master/canvas-toBlob.min.js"></script>
...
canvas.toBlob(function(blob) {
  fileWriter.write(blob);
}, "image/png");

Here is a little example using the FileSaver: http://jsfiddle.net/JR5XE/

尹雨沫 2024-11-23 18:49:39

您似乎直接将 Base64 表示形式写入磁盘。您需要先对其进行解码。

You seem to be directly writing the base64 representation to disk. You need to decode it first.

心如狂蝶 2024-11-23 18:49:39

我一直在想同样的事情,并试图找到答案。
据我所知,您必须将从 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

青巷忧颜 2024-11-23 18:49:39

您需要 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文