简体   繁体   English

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

[英]Save PNG Canvas Image to HTML5 Storage (JAVASCRIPT)?

I am developing a chrome extension. 我正在开发一个chrome扩展。

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. 我在画布中打开一个图像文件,我对它应用了一些更改,然后我试图将它保存到HTML5文件系统API。

First I get the dataURL from the canvas: 首先,我从画布中获取dataURL:

    var dataURL = canvas.toDataURL('image/png;base64'); 

Then just the data: 然后只是数据:

    var image64 = dataURL.replace(/data:image\/png;base64,/, '');

Then I make a Blob. 然后我做一个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(); 然后我使用以下函数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://dev.w3.org/html5/canvas-api/canvas-2d-api.html#todataurl-method

http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-file-creatingempty http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-file-creatingempty

Thank You! 谢谢!

I've found a function that converts a data URL to a blob. 我找到了一个将数据URL转换为blob的函数。

Great for when you need to save a canvas image to the sandboxed FileSystem. 非常适合在需要将画布图像保存到沙盒文件系统时。 Works in Chrome 13. 适用于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 http://mustachified.com/master.js
via http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-April/031243.html 通过http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-April/031243.html
via https://bugs.webkit.org/show_bug.cgi?id=51652 通过https://bugs.webkit.org/show_bug.cgi?id=51652
via http://code.google.com/p/chromium/issues/detail?id=67587 通过http://code.google.com/p/chromium/issues/detail?id=67587

There is now a canvas.toBlob() polyfill: https://github.com/eligrey/canvas-toBlob.js 现在有一个canvas.toBlob()polyfill: https//github.com/eligrey/canvas-toBlob.js
Other interesting link: https://github.com/eligrey/BlobBuilder.js 其他有趣的链接: 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/ 以下是使用FileSaver的一个小例子: http//jsfiddle.net/JR5XE/

You seem to be directly writing the base64 representation to disk. 您似乎直接将base64表示写入磁盘。 You need to decode it first. 您需要先解码它。

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. 从我可以告诉你必须将你从atob获得的原始字符串转换为unit8array然后你可以将它附加到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.... 这是一个将图像转换为画布,然后将画布中的数据转换为blob然后将第三个图像src设置为blob的uri的示例....您应该能够从那里添加fs内容。 ...
http://jsfiddle.net/PAEz/XfDUS/ 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. ..不接受把代码放在这里,但说实话,我无法弄清楚如何;)无论如何,JSFiddle是一个很好的方式来玩它。

References 参考
https://gist.github.com/1032746 https://gist.github.com/1032746
http://code.google.com/p/html5wow/source/browse/src/demos/photo-gallery/js/utils.js http://code.google.com/p/html5wow/source/browse/src/demos/photo-gallery/js/utils.js
Get image data in JavaScript? 用JavaScript获取图像数据?
http://jsdo.it/tsmallfield/typed_array http://jsdo.it/tsmallfield/typed_array

您需要HTMLCanvasElement.toBlob ,然后使用W3C文件系统API将blob写入文件系统,但大多数浏览器尚未实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM