简体   繁体   English

如何更新 React 组件的“src”并将 Base64 对象显示为 React 组件中的图像?

[英]How do I update the “src” of a React component and display a Base64 object as an image in a React component?

I'm (relatively) new to software dev and am stuck with trying to display a Base64 Binary image in a React component.我是(相对)软件开发新手,并且一直在尝试在 React 组件中显示 Base64 二进制图像。

Problem: I'm ultimately trying to display a Buffer JPEG Image in an <img /> tag with a React component.问题:我最终尝试在带有 React 组件的<img />标签中显示缓冲 JPEG 图像。

Using screenshot-desktop npm package ( https://www.npmjs.com/package/screenshot-desktop ) to capture desktop.使用 screenshot-desktop npm 包( https://www.npmjs.com/package/screenshot-desktop )来捕获桌面。

screenshot() returns a Promise which exposes a Buffer JPEG object. screenshot() 返回一个公开 Buffer JPEG 对象的 Promise。

Based on numerous posts, I have tried to convert this to a Base64 format and then update state so that I can subsequently access the Base64 image to include as a src in the image tag.根据大量帖子,我尝试将其转换为 Base64 格式,然后更新状态,以便我随后可以访问 Base64 图像以将其作为 src 包含在图像标签中。

Here are the key parts of my code:-以下是我的代码的关键部分:-

const screenshot = require("screenshot-desktop");

function ScreenCapturer(props) {

  const [image, setImage] = useState("");

  function captureCurrentScreen() {
    screenshot().then((bufferedImage) => {
      let base64Image = btoa(bufferedImage);
      setImage(base64Image);
    });
  }


  return (
    <div >

      <img className="image-holder" alt="" src={`data:image/jpeg;base64,${image}`}/>

      <h3>Image Capturer</h3>

      <h4>Base64Image : {image.substring(1, 20)}</h4>

      <button
        onClick={() => {
          captureCurrentScreen();
        }}
      >
        Capture
      </button>
    </div>
  );
}

Key Issues:-关键的问题:-

  1. With the above code, I realise that the initial src value won't display anything.通过上面的代码,我意识到初始src值不会显示任何内容。

  2. On clicking the button, I'm able to display the first 20 characters of the Base64 image, so I believe that the conversion process is working.单击该按钮后,我可以显示 Base64 图像的前 20 个字符,因此我相信转换过程正在起作用。 After pressing the button, no image is shown nor is there any error shown.按下按钮后,没有图像显示,也没有显示任何错误。

  3. Note: On checking devTools in the chrome Bowser, the object that screenshot() produces is stated as "Uint8 array"注意:在 chrome Bowser 中检查 devTools 时,screenshot() 生成的对象表示为“Uint8 数组”

Question: My understanding is that the component should re-render on setting a new state.问题:我的理解是组件应该在设置新状态时重新渲染。 Where am I going wrong ?我哪里错了? Any help would be much appreciated.任何帮助将非常感激。

I don't believe btoa() is what you want.我不相信 btoa() 是你想要的。 That will try to convert your binary to ascii text: https://www.w3schools.com/JSREF/met_win_btoa.asp这将尝试将您的二进制文件转换为 ascii 文本: https : //www.w3schools.com/JSREF/met_win_btoa.asp

I found these questions and answers that just put the base64 data into the image, like you're doing, directly from the source.我发现这些问题和答案只是将 base64 数据放入图像中,就像您正在做的那样,直接从源中提取。 convert base64 to image in javascript/jquery How to show base64 image in react? 在 javascript/jquery 中将 base64 转换为图像如何在反应中显示 base64 图像?

Those answers may point you in the right direction.这些答案可能会为您指明正确的方向。 If not, there are several npm modules around displaying base64 images.如果没有,有几个 npm 模块可以显示 base64 图像。 They may be able to solve it for you.他们或许可以为您解决。 https://npms.io/search?q=convert-image-base64 https://npms.io/search?q=convert-image-base64

If the image is of type file you can maybe try to use URL.createObjectURL(yourData) It takes a type file or blob as input and sends a temporary URL.如果图像是文件类型,您可以尝试使用URL.createObjectURL(yourData)它以类型文件或 blob 作为输入并发送临时 URL。 So to convert a uint8 array into a blob you can try new Blob([bufferedImage])因此,要将 uint8 数组转换为 blob,您可以尝试new Blob([bufferedImage])

Code:代码:

const blob = new Blob([bufferImage]);
const tempUrl = URL.createObjectURL(blob);
setImage(tempUrl)
/// ...
<img src={tempUrl} />

Tell me if it works.告诉我它是否有效。

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

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