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

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

问题描述

下面是二进制的有效.JPEG图像。
http://pastebin.ca/raw/2314500

我曾尝试使用Python来拯救这个二进制数据转化为图像。

我如何可以将这些数据转换为可视.JPEG图像ExtJS的4?

我想这一点,但它不能正常工作。

 数据:为image / jpeg;的base64,+二进制数据
 

解决方案

需要将其转换为Base64。

JS有BTOA()函数吧。

例如:

  VAR IMG = document.createElement方法('IMG');
img.src ='数据:为image / jpeg; BASE64,+ BTOA('您的二进制数据);
document.body.appendChild(IMG);
 

但是,我认为在引擎收录你的二进制数据是无效的 - JPEG数据必须在ffd9'结尾

更新:

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

 函数hexToBase64(STR){
    返回BTOA(String.fromChar code.apply(空,str.replace(/ \ r |吗?\ n / g的,).replace(/([\ DA-FA-F] {2})/克,0X $ 1).replace(/ + $ /,).split()));
}
 

和使用它:

  img.src =数据:图像/ JPEG; BASE64,+ hexToBase64('您的二进制数据);
 

请参阅工作示例在 的jsfiddle

您的十六进制数据

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

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

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

解决方案

Need to convert it in base64.

JS have btoa() function for it.

For example:

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

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

Update:

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(" ")));
}

And use it:

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

See working example with your hex data on jsfiddle

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

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