字节[]到图像,奇怪的字节[] [英] Byte[] to an image, strange byte[]

查看:88
本文介绍了字节[]到图像,奇怪的字节[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过tcp发送给我的位图图像,然后将数据存储在一个名为jacketBytes的byte []数组中,但是当我尝试使用它制作图像时,该图像就会出现;

BufferedImage img = ImageIO.read(新ByteArrayInputStream(bytes));
我得到Nullpointerexception
因此我开始以各种方式写出字节数组来进行故障排除.

Hi, i am having a bitmap image sent to me over tcp, then stores the data in a byte[] array called jacketBytes, but thenen when i try to make an image out of it using;

BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
i get Nullpointerexception
so i started troubleshooting by writing out my byte array in different ways.

    FileOutputStream fos = new FileOutputStream(strFilePath);
System.out.println("total bytes: "+bytes.length);
for(int i =0; i < 56; i++){
System.out.print("0x"+bytes[i]+" ");
}
System.out.println();
for(int i =0; i < 1000; i++){
    System.out.print((char)bytes[i]);
    }
System.out.println();
bytes.toString();
System.out.println(bytes.toString());
fos.write(bytes);
fos.close();



我从中得到的输出是:



the output i get from that is :

total bytes: 34928
0x52 0x50 0x52 0x68 0x51 0x54 0x48 0x52 0x48 0x49 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48
424D3604010000000000



第一部分42 4D(0x52 0x50 0x52 0x68)是ASCII的BM,就像bmp标头应该开始的方式一样.
我无法弄清楚的是,如果有数据,剩下的就是什么!应该有4个字节,其中包含以字节为单位的图像数据.
至少根据维基百科维基百科

关于如何将数据发送给我的唯一文档是:
NET/USB夹克艺术品/专辑数据
-图像类型0:BMP,1:JPEG
-封包标记0:开始,1:下一个,2:结束
xxxxxxxxxxxxxx->夹克/唱片艺术数据(有效长度,最大1024个ASCII十六进制字母)

我设法将其存储在单字节数组中,但由于包含太多的空值,我不知道如何将其放入图像数据中!



the first part 42 4D(0x52 0x50 0x52 0x68) is BM in ASCII just the way a bmp header is supposed to start.
what i cant figure out is what the rest if the data is! there is supposed to be 4 bytes containing the image data in bytes.
at least according to wikipedia wikipedia

The only documentation on how the data is sent to me is this:
NET/USB Jacket Art/Album Art Data
t-> Image type 0:BMP,1:JPEG
p-> Packet flag 0:Start, 1:Next, 2:End
xxxxxxxxxxxxxx -> Jacket/Album Art Data (valiable length, 1024 ASCII HEX letters max)

i manage to store it in a single byte Array but i do not know how to make it in to image data since it contains so much nulls!
Help appreciated!

推荐答案

您应该将数据编码为base64.

.a框架中的 Base64? [ ^ ]

并具有用于此类传输的正确标题.
You should probably encode your data in base 64.

Base64 in .Net framework?[^]

and have the proper header for such transfer.


我自己解决了该问题,尽管感谢您的回答.当我解决它时,我感到非常沮丧.我得到的字符串是ASCII中的十六进制字符串,所以首先我必须成对地取成对的字符串,然后将成对的字符串变成一个字符.然后我可以使用新的字符串并将其编码回一个byteArray,然后它是一个工作映像.我认为是奇怪的协议.
I Solved it myself, thanks for the answer though. I got so frustrated when i solved it. the string i got was a hex string in ASCII so first i had to take the String pair by pair and then turn the pair i got into a Character. then i could use the new string and just encode it back to a byteArray and then it was a working image. Strange protocol i think.


这篇关于字节[]到图像,奇怪的字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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