简体   繁体   English

使用 src blob 画布到 img

[英]Canvas to img with src blob

I'm creating a PNG file from a canvas, but before I want to display the canvas as an img element, using a blob as the src of the img.我正在从画布创建一个 PNG 文件,但在我想将画布显示为 img 元素之前,使用 blob 作为 img 的 src。 What I tried so far:到目前为止我尝试过的:

canvas = document.getElementById("canvas");
//Get data from canvas
img_b64 = canvas.toDataURL('image/png');
//Create PNG
png = img_b64.split(',')[1];
//Create new img
img = document.createElement("img");
img.src = img_b64;
//Append img to document
//Save png

This works, but instead of img_b64 I want a blob to be the src of the img, like this:这有效,但我想要一个 blob 作为 img 的 src,而不是img_b64 ,如下所示:

//Create blob from new PNG
blob = new Blob([window.atob(png)], { type: 'image/png', encoding: 'utf-8' });
//Create new img with blob as src
img = document.createElement("img");
img.src = URL.createObjectURL(blob);
//Append img to document
//Save png

The above code only shows me an empty img.上面的代码只显示了一个空的 img。 Is there a way to do this or do I have to create the png file first and afterwards create a blob for it?有没有办法做到这一点,还是我必须先创建 png 文件,然后再为它创建一个 blob?

I just found an solution using the answer to this question: Convert Data URI to File then append to FormData .我刚刚使用这个问题的答案找到了一个解决方案: Convert Data URI to File then append to FormData Using the dataURItoBlob function my code is:使用dataURItoBlob函数我的代码是:

canvas = document.getElementById("canvas");
//Get data from canvas
img_b64 = canvas.toDataURL('image/png');
//Create blob from DataURL
blob = dataURItoBlob(img_b64)
img = document.createElement("img");
img.src = URL.createObjectURL(blob);
//Insert image

dataURItoBlob function: dataURItoBlob 函数:

function dataURItoBlob(dataURI) {
    // convert base64/URLEncoded data component to raw binary data held in a string
    var byteString;
    if (dataURI.split(',')[0].indexOf('base64') >= 0)
        byteString = atob(dataURI.split(',')[1]);
    else
        byteString = unescape(dataURI.split(',')[1]);

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

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

    return new Blob([ia], {type:mimeString});
}

With toBlob this should be easy to solve from IE>=10.使用toBlob这应该很容易从 IE>=10 解决。

  const img = document.createElement("img");
  canvas.toBlob((blob) => {
    img.src =  URL.createObjectURL(blob);
  });

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

相关问题 canvas.toDataURL到img src - canvas.toDataURL to img src Canvas 闪烁,img.src 访问 - Canvas is flickering, img.src access 从文本输入更改canvas img.src - Change canvas img.src from text input html2canvas更改后保存图像img src - html2canvas save image after change img src 我可以将src img属性指定为Blob文件还是以base64编码的图像? - Can i apoint the src img attribute to a blob file or a base64 encoded image? 在JavaScript中根据img src路径下载图像并将其转换为File对象或Blob - Download and convert image based on img src path to File object or Blob in JavaScript 使用 Blob 作为 iframe 源和加载位于子目录中的 img src 图像时出现问题 - Using a Blob as iframe source and a trouble with loading img src images located in a subdirectory Img src=&quot;blob:http://localhost... 不起作用 - 无论是 createObjectURL 还是 readAsDataURL | Firebase | Vue.js - Img src="blob:http://localhost... doesn't work - neither with createObjectURL or readAsDataURL | Firebase | Vue.js 在将img.src绘制到画布时如何在不影响绘制对象的情况下替换img.src? - How to alternate an img.src when drawing it to canvas without affecting drawn objects? 您可以在JavaScript文件中加载img标签的src,然后将其绘制到画布上吗? - Can you load an img tag's src inside javascript file and then draw it to the canvas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM