从 c++ 传输 jpeg到安卓(java)
我正在努力将 ID3v2 标签内的简单 jpeg 文件从 c++ 通过 TCP 套接字传输到 java (Android)。库“taglib”提供了提取此文件的功能,我可以将 jpeg 保存为新文件。
send 函数如下所示
char *parameter_full = new char[f3->picture().size()+2];
sprintf(parameter_full,"%s\n\0",f3->picture().data());
// send
result = send(c,parameter_full,strlen(parameter_full),0);
delete[] parameter_full;
,其中
f3->picture().data() 返回指向内部数据结构的指针(它返回 char*)并且 f3->picture().size() 返回数组的大小。
然后 Android 接收它,
String imageString = inFromServer.readLine();
byte[] imageBytes = imageString.getBytes();
Bitmap cover = BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
但不知何故,decodeByteArray 总是返回 null。我的想法是,Java 无法正确接收图像,因为 imageString 仅包含 4 个字符......而提取的 jpeg 文件大小为 12.7 KB。 但出了什么问题呢?
马丁
I am struggling with the transfer of a simple jpeg file inside an ID3v2 tag from c++ over a TCP socket to java (Android). The library "taglib" offers to extract this file and I am able to save the jpeg as a new file.
The send function looks like this
char *parameter_full = new char[f3->picture().size()+2];
sprintf(parameter_full,"%s\n\0",f3->picture().data());
// send
result = send(c,parameter_full,strlen(parameter_full),0);
delete[] parameter_full;
where
f3->picture().data() returns a pointer to the internal data structure (it returns char*) and
f3->picture().size() returns the size of the array.
Then Android receives it with
String imageString = inFromServer.readLine();
byte[] imageBytes = imageString.getBytes();
Bitmap cover = BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
But somehow decodeByteArray always returns null. My idea is that Java doesn't receive the image correctly because imageString only consists of 4 characters...while the extracted jpeg file has a size of 12.7 KB.
But what has gone wrong?
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该对字节数据使用字符串函数,因为 0 值被视为字符串终止符。如果您需要复制
char*
以及 Java 上InputStream
的byte[]
读取函数,请尝试查看 C++ 端的 memcpy边。You shouldn't use string functions on byte data because 0 values are taken as string terminators. Try looking into memcpy on the C++ side if you need to copy the
char*
and also thebyte[]
read functions forInputStream
on the Java side.