如何将二进制数据显示为图像 - extjs 4 [英] How to display binary data as image - extjs 4

查看:29
本文介绍了如何将二进制数据显示为图像 - extjs 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有效 .JPEG 图像的二进制文件.
http://pastebin.ca/raw/2314500

Here is the binary for a valid .JPEG image.
http://pastebin.ca/raw/2314500

我尝试使用 Python 将这些二进制数据保存到图像中.

I have tried to use Python to save this binary data into an image.

如何使用 extjs 4 将此数据转换为可查看的 .JPEG 图像?

How can I convert this data to a viewable .JPEG image with extjs 4?

我试过了,但没有用.

data:image/jpeg;base64,+ binary data

推荐答案

需要转为 base64.

Need to convert it in base64.

JS 有 btoa() 函数.

JS have btoa() function for it.

例如:

var img = document.createElement('img');
img.src = 'data:image/jpeg;base64,' + btoa('your-binary-data');
document.body.appendChild(img);

但我认为你在 pastebin 中的二进制数据是无效的 - jpeg 数据必须以 'ffd9' 结束.

But i think what your binary data in pastebin is invalid - the jpeg data must be ended on 'ffd9'.

更新:

需要编写简单的十六进制转 base64 转换器:

Need to write simple hex to base64 converter:

function hexToBase64(str) {
    return btoa(String.fromCharCode.apply(null, str.replace(/
|
/g, "").replace(/([da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}

并使用它:

img.src = 'data:image/jpeg;base64,' + hexToBase64('your-binary-data');

jsfiddle

这篇关于如何将二进制数据显示为图像 - extjs 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆