1、效果图
2、主要代码
<html>
<body>
<div id="imageContainer"></div>
<script>
let hexString ="你图片的16进制字符串";
// 创建一个新的图片元素
const imgElement = document.createElement("img");
const imageUrl = imgRun(hexString);
// 设置图片元素的src属性以展示图片
imgElement.src = imageUrl;
// 将图片元素添加到HTML文档中
const imageContainer = document.getElementById("imageContainer");
imageContainer.appendChild(imgElement);
function imgRun(value) {
let pos = 0;
let len = value.length;
if (len % 2 != 0) {
return null;
}
len /= 2;
const hex = [];
for (let i = 0; i < len; i++) {
const s = value.substr(pos, 2);
const v = parseInt(s, 16);
hex.push(v);
pos += 2;
}
let binary = '';
const bytes = new Uint8Array(hex);
const len2 = bytes.byteLength;
for (let i = 0; i < len2; i++) {
binary += String.fromCharCode(bytes[i]);
}
return 'data:image/png;base64,' + window.btoa(binary);
}
</script>
</body>
</html>