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

查看:192
本文介绍了如何显示二进制数据作为图像 - 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.

我如何将此数据转换为可视的.JPEG图像与extjs 4?

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

我尝试过,但是它不起作用。

I tried this, but it doesn't work.

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'.

更新:

需要将简单的hex写入base64转换器:

Need to write simple hex to base64 converter:

function hexToBase64(str) {
    return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/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天全站免登陆