1. <legend id='dsOwi'><style id='dsOwi'><dir id='dsOwi'><q id='dsOwi'></q></dir></style></legend>
      <i id='dsOwi'><tr id='dsOwi'><dt id='dsOwi'><q id='dsOwi'><span id='dsOwi'><b id='dsOwi'><form id='dsOwi'><ins id='dsOwi'></ins><ul id='dsOwi'></ul><sub id='dsOwi'></sub></form><legend id='dsOwi'></legend><bdo id='dsOwi'><pre id='dsOwi'><center id='dsOwi'></center></pre></bdo></b><th id='dsOwi'></th></span></q></dt></tr></i><div id='dsOwi'><tfoot id='dsOwi'></tfoot><dl id='dsOwi'><fieldset id='dsOwi'></fieldset></dl></div>

      <small id='dsOwi'></small><noframes id='dsOwi'>

        <tfoot id='dsOwi'></tfoot>
          <bdo id='dsOwi'></bdo><ul id='dsOwi'></ul>

      1. Javascript:将存储为 Uint8Array 的 PNG 渲染到没有数据 URI 的 Canvas 元素上

        Javascript: Render PNG stored as Uint8Array onto Canvas element without Data URI(Javascript:将存储为 Uint8Array 的 PNG 渲染到没有数据 URI 的 Canvas 元素上)

            <tbody id='mnNuE'></tbody>
          <i id='mnNuE'><tr id='mnNuE'><dt id='mnNuE'><q id='mnNuE'><span id='mnNuE'><b id='mnNuE'><form id='mnNuE'><ins id='mnNuE'></ins><ul id='mnNuE'></ul><sub id='mnNuE'></sub></form><legend id='mnNuE'></legend><bdo id='mnNuE'><pre id='mnNuE'><center id='mnNuE'></center></pre></bdo></b><th id='mnNuE'></th></span></q></dt></tr></i><div id='mnNuE'><tfoot id='mnNuE'></tfoot><dl id='mnNuE'><fieldset id='mnNuE'></fieldset></dl></div>
        • <tfoot id='mnNuE'></tfoot>

            • <bdo id='mnNuE'></bdo><ul id='mnNuE'></ul>

                  <legend id='mnNuE'><style id='mnNuE'><dir id='mnNuE'><q id='mnNuE'></q></dir></style></legend>

                  <small id='mnNuE'></small><noframes id='mnNuE'>

                • 本文介绍了Javascript:将存储为 Uint8Array 的 PNG 渲染到没有数据 URI 的 Canvas 元素上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试渲染存储在 javascript Uint8Array 中的 PNG 图像.我最初尝试的代码如下:

                  I'm trying to render a PNG image that is stored in a javascript Uint8Array. The code that I originally tried was the following:

                  var imgByteStr = String.fromCharCode.apply(null, this.imgBytes);
                  
                  var pageImg = new Image();
                  pageImg.onload = function(e) {
                  
                      // Draw onto the canvas
                      canvasContext.drawImage(pageImg, 0, 0);
                  
                  };
                  
                  // Attempt to generate the Data URI from the binary
                  // 3rd-party library adds toBase64() as a string function
                  pageImg.src="data:image/png;base64,"+encodeURIComponent(imgByteStr.toBase64());
                  

                  但是,我发现由于某种原因,onload 函数从未运行(我在 chrome 中设置了断点,但它根本没有命中).我发现如果我将 src 设置为 URL 而不是 Data URI,它可以正常工作.但是,由于一些限制,我不能直接引用图像的 URL,即它必须从 UInt8Array 加载.

                  However, I found that for some reason the onload function was never running (I had breakpoints set up in chrome and it simply never hit). I found that if I set the src to a URL instead of Data URI, it worked correctly. However, due to some constraints, I cannot directly refer to the image's URL, i.e., it must be loaded from the UInt8Array.

                  此外,更复杂的是,这些图像的大小可能是兆字节.根据我的阅读,尝试将数据 URI 用于非常大的图像存在兼容性问题.因此,我非常犹豫要不要使用它们.

                  Also, to complicate things slightly more, these images may be megabytes in size. From what I have read, there are compatibility issues with attempting to use Data URIs for very large images. Because of this, I am extremely hesitant to utilize them.

                  因此,问题是 如何在不使用数据 URI 或直接引用图像 URL 的情况下将此字节数组作为 PNG 呈现到画布上下文中?

                  我还尝试使用类似以下的方法直接使用 putImageData 函数来操作图像数据.请记住,我不是 100% 了解此功能的工作原理

                  I've also tried using something like the following to manipulate the image data directly using the putImageData function. Keep in mind that I don't 100% understand how this function works

                  var imgdata = canvasContext.createImageData(this.baseHeight, this.baseWidth);
                  var imgdatalen = imgdata.data.length;
                  for(var i=0; i<imgdatalen; i++) {
                      imgdata.data[i] = _this.imgBytes[i];
                  }
                  canvasContext.putImageData(imgdata, 0, 0);
                  

                  它来自一个我已经关闭标签的博客,因此对于没有给予适当信任的灵感表示歉意.

                  It was lifted from a blog whose tab I have since closed, so apologies to the inspiration for not giving appropriate credit.

                  另外,在慢慢地敲打这件事时,我在 Chrome SECURITY_ERR: DOM Exception 18 中遇到了一个错误.事实证明,一旦使用 drawImage 将图像加载到画布中,如果没有一些额外的解决方法,就无法检索它.Chromium 博客文章这个话题特别有用

                  Also, while slowly hammering away at this thing, I ran into an error in Chrome SECURITY_ERR: DOM Exception 18. Turns out that, as soon as an image is loaded inot a canvas using drawImage, it cannot be retrieved without some additional workarounds. A Chromium Blog post on the topic was especially useful

                  推荐答案

                  如果内存中有 PNG 数据(我认为这就是您所描述的),那么您可以从中创建图像.在 HTML 中它看起来像:

                  If you have the PNG data in memory (which I think is what you're describing) then you can create an image from it. In HTML it would look like:

                   <img src="data:image/png;base64,KSjs9JdldhAlisflAkshf==" />
                  

                  在 JavaScript 中你也可以这样做:

                  In JavaScript you can do the same:

                  var image = document.createElement('img');
                      image.src = 'data:image/png;base64,' + base64Data;
                  

                  请注意,如果您没有 PNG 数据,则可以更改 MIME 类型.

                  Note that you can change the MIME type if you don't have PNG data.

                  然后您可以使用 context.drawImage(image, 0, 0) 或类似方法将 image 绘制到画布上.

                  You could then draw the image onto your canvas using context.drawImage(image, 0, 0) or similar.

                  所以剩下的部分就是将 Uint8Array 的内容编码为 Base 64 数据.

                  So the remaining part of the puzzle is to encode the contents of your Uint8Array into Base 64 data.

                  var array = new Uint8Array(),
                      base64Data = btoa(String.fromCharCode.apply(null, array));
                  

                  btoa 函数不是标准的,因此某些浏览器可能不支持它.然而,似乎 大多数.否则,您可能会发现 我使用的一些代码很有帮助.

                  The function btoa isn't standard, so some browsers might not support it. However it seems that most do. Otherwise you might find some code I use helpful.

                  我自己没有测试过这些!

                  I haven't tested any of this myself!

                  这篇关于Javascript:将存储为 Uint8Array 的 PNG 渲染到没有数据 URI 的 Canvas 元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                  <i id='04lXe'><tr id='04lXe'><dt id='04lXe'><q id='04lXe'><span id='04lXe'><b id='04lXe'><form id='04lXe'><ins id='04lXe'></ins><ul id='04lXe'></ul><sub id='04lXe'></sub></form><legend id='04lXe'></legend><bdo id='04lXe'><pre id='04lXe'><center id='04lXe'></center></pre></bdo></b><th id='04lXe'></th></span></q></dt></tr></i><div id='04lXe'><tfoot id='04lXe'></tfoot><dl id='04lXe'><fieldset id='04lXe'></fieldset></dl></div>

                  <small id='04lXe'></small><noframes id='04lXe'>

                  1. <legend id='04lXe'><style id='04lXe'><dir id='04lXe'><q id='04lXe'></q></dir></style></legend>

                          <tbody id='04lXe'></tbody>

                        • <bdo id='04lXe'></bdo><ul id='04lXe'></ul>

                          <tfoot id='04lXe'></tfoot>