简体   繁体   English

如何将base64转换为RGBA8888

[英]How to convert base64 to RGBA8888

I'm trying to convert and image to base 64 and then to RGBA8888 to feed camera buffer, I'm stuck when trying to convert from base64 to rgba.我正在尝试将图像转换为 base 64,然后再转换为 RGBA8888 以提供相机缓冲区,但在尝试从 base64 转换为 rgba 时我被卡住了。

function encodeImageFileAsURL(element) {
  var file = element.files[0];
  var reader = new FileReader();
  reader.onloadend = function() {
    console.log('RESULT', reader.result)
  }
  reader.readAsDataURL(file);
}
<input type="file" onchange="encodeImageFileAsURL(this)" />

Canvas - I use canvas because it has ways to get at the underlying data, and ways to render any kind of image format onto itself.画布 - 我使用画布是因为它可以获取底层数据,并且可以将任何类型的图像格式呈现到自身上。

Html image tag - not sure if this was necessary, but it provides an easy way to draw an image file onto a canvas. Html 图像标签 - 不确定这是否必要,但它提供了一种将图像文件绘制到画布上的简单方法。

I take the file from your reader code, insert it into an img, render that img onto a canvas, and then extract the raw RGBA frame from the canvas.我从您的阅读器代码中获取文件,将其插入到 img 中,将该 img 渲染到画布上,然后从画布中提取原始 RGBA 帧。 You can find out about the data format here (data property).您可以在此处了解数据格式(数据属性)。 I hope that is a suitable format for your application.我希望这是适合您的应用程序的格式。

 const canvas = document.createElement("canvas"); const ctx = canvas.getContext('2d'); function encodeImageFileAsURL(element) { var file = element.files[0]; var reader = new FileReader(); reader.onloadend = function() { const img = document.createElement("img"); img.onload = () => { const w = img.naturalWidth, h = img.naturalHeight; ctx.drawImage(img, 0, 0); const imgData = ctx.getImageData(0, 0, w, h); const arr = Uint8ClampedArray.from(imgData.data); // do something with arr console.log(`image width: ${w}, image height: ${h}, expected raw data size (wxhx 4): ${w * h * 4}, actual size: ${arr.length}`); }; img.src = reader.result; }; reader.readAsDataURL(file); }
 <input type="file" onchange="encodeImageFileAsURL(this)" />

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

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