Home >Web Front-end >JS Tutorial >How to Convert Base64 to Image in JavaScript/jQuery?

How to Convert Base64 to Image in JavaScript/jQuery?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-28 20:40:31693browse

How to Convert Base64 to Image in JavaScript/jQuery?

How to Convert Base64 to Image in JavaScript/jQuery

You have encountered a task where you need to convert captured base64 data from a camera feed into an image. Here's how you can accomplish this in JavaScript/jQuery:

To convert the base64 data into an image:

<code class="javascript">var image = new Image();
image.src = 'data:image/png;base64,' + item_image;</code>

The above code creates an Image object and sets its source (src) to the base64 data you have, including the 'data:image/png;base64,' part.

Once the image is loaded, you can append it to the DOM using:

<code class="javascript">document.body.appendChild(image);</code>

This will display the image in your HTML document.

It's important to note that some browsers might not support data URIs. You can check the [compatibility table](link to table) for more information.

The above is the detailed content of How to Convert Base64 to Image in JavaScript/jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn