javascript 将画布流式传输到 imgITools 以制作 ico

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24251136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:27:56  来源:igfitidea点击:

Stream canvas to imgITools to make ico

javascriptcanvasfirefox-addonxpcom

提问by Noitidart

So on WinXP I have been having a hard time converting a PNG file to an ICO with canvas. I found this method encodeImage. I don't know if it works but it looks promising but I can't figure out how to feed the image I drew on a canvas into imgITools.decodeData.

因此,在 WinXP 上,我很难将 PNG 文件转换为带有画布的 ICO。我找到了这个方法encodeImage。我不知道它是否有效,但看起来很有希望,但我不知道如何将我在画布上绘制的图像输入imgITools.decodeData.

What should I use for aImageStreamand/or aMimeType?

我应该用什么aImageStream和/或aMimeType

imgTools.decodeImageData(aImageStream, aMimeType, imgContainer);

This is more of my code:

这是我的更多代码:

 img['asdf'].file = new FileUtils.File(myPathHere);

let iconStream;
try {
  let imgTools = Cc["@mozilla.org/image/tools;1"]
                    .createInstance(Ci.imgITools);
  let imgContainer = { value: null };

  imgTools.decodeImageData(aImageStream, aMimeType, imgContainer);
  iconStream = imgTools.encodeImage(imgContainer.value,
                                    "image/vnd.microsoft.icon",
                                    "format=bmp;bpp=32");
} catch (e) {
  alert('failure converting icon ' + e)
  throw("processIcon - Failure converting icon (" + e + ")");
}

let outputStream = FileUtils.openSafeFileOutputStream(img['asdf'].file);
NetUtil.asyncCopy(iconStream, outStream, netutilCallback);

回答by nmaier

Since you're having a canvas already(?), it would be probably easier to use either on of the following canvas methods:

由于您已经有一个画布(?),使用以下任一画布方法可能会更容易:

  • toDataURI/toDataURIHD
  • toBlob/toBlobHD
  • mozFetchAsStream
  • toDataURI/toDataURIHD
  • toBlob/toBlobHD
  • mozFetchAsStream

There is also the undocumented -moz-parse-options:, e.g -moz-parse-options:format=bmp;bpp=32. (ref-tests seem to depend on it, so it isn't going away anytime soon I'd think).

还有未记录的-moz-parse-options:,例如-moz-parse-options:format=bmp;bpp=32. (参考测试似乎取决于它,所以我认为它不会很快消失)。

So, here is an example for loading stuff into an ArrayBuffer.

因此,这是一个将内容加载到ArrayBuffer.

(canvas.toBlobHD || canvas.toBlob).call(canvas, function (b) {
    var r = new FileReader();
    r.onloadend = function () {
        // r.result contains the ArrayBuffer.
    };
    r.readAsArrayBuffer(b);
}, "image/vnd.microsoft.icon", "-moz-parse-options:format=bmp;bpp=32");

Here is a more complete example fiddlecreating a 256x256 BMP icon.

这是一个更完整的示例小提琴,可创建 256x256 BMP 图标。

Since you likely want to feed that data into js-ctypes, having an ArrayBuffer is great because you can create pointers from it directly, or write it to a file using OS.File.

由于您可能希望将该数据提供给 js-ctypes,因此拥有 ArrayBuffer 非常棒,因为您可以直接从它创建指针,或使用OS.File.