gpt4 book ai didi

java - 使用套接字将 .jpg 从 Android 发送到 C++

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:43 27 4
gpt4 key购买 nike

我目前正在使用智能手机和 Raspberry Pi 开展一个项目。我有一个问题,就是我无法从手机正确发送 .jpg。我已经成功发送了照片的大小,这是应该在之后发送的字节数组的长度。谁能帮帮我?

部分客户端(Android Studio - Java)

Socket socket = new Socket("192.168.2.122",1234);
File file = new File("/storage/emulated/0/poza/cam_image.jpg");
FileInputStream fis = new FileInputStream(file);
InputStream is=fis;
byte[] message=IOUtils.toByteArray(is);
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
String zxc;
zxc = Integer.toString(message.length);
dOut.writeBytes(zxc);
dOut.write(message);

服务器的一部分(QT Creator - C++)

socket->waitForBytesWriten();
socket->waitForReadyRead(100);
char request[6];
socket->read(request,6);
request[6]=NULL;
int bs;
bs=atoi(request); //bs is the length which has the correct value

我也尝试过以 block 的形式发送字节数组,但我可能没有正确编写它,因为它不起作用。提前致谢。

编辑:我已成功发送,感谢您的帮助。

部分客户(良好)

Socket socket = new Socket("192.168.2.122",1234);
File file = new File("/storage/emulated/0/poza/cam_image.jpg");
FileInputStream fis = new FileInputStream(file);
InputStream is=fis;
byte[] message=IOUtils.toByteArray(is);
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
int leng = message.length;
byte [] length = ByteBuffer.allocate(4).putInt(message.length).array(); //int to bytes
System.out.println(message.length);
byte [] newLen = new byte[4]; //little endian, big endian stuff
for (int i=0; i<4; i++)
{
System.out.println(length[i]); //original bytes
newLen[3-i]=length[i]; //reversing order
}
dOut.write(newLen); //sending the size of image
dOut.flush();
dOut.write(message);//send image

部分服务器(良好)

QTcpSocket *socket = server->nextPendingConnection();
socket->waitForConnected();
qDebug()<<"connected";
char *sockData = new char[92160000]; //max bytes size for photo
int size = 0; //photo size
int bytes = 0; //bytes read at a time

qDebug()<<"waiting for bytes";
socket->waitForReadyRead();
socket->read((char*)&size,sizeof(int)); //reading the size of the photo
qDebug()<<"the size is just " <<size;
for(int i=0;i<size;i+=bytes){ //reading the rest of bytes
socket->waitForReadyRead();
bytes = socket->read(sockData+i,size-i);
if(bytes==-1){
printf("error");
break;
}
}
qDebug()<<"success in reading the image";
std::vector<char> data(sockData,sockData+size);
if(data.size()==0){
qDebug()<<"errorrrrr";
return;
}
Mat temporary = cv::imdecode(data,CV_LOAD_IMAGE_COLOR);
cv::imshow("sdfsd",temporary);
cv::waitKey(1000);
delete sockData;//memory deallocation

最佳答案

将图像数据转换为字节数组后,为什么要将数组的长度作为字符串发送? string 是可变长度的,但您并没有告诉接收者字符串的实际长度,无论是在发送字符串之前发送实际的字符串长度,还是在字符串之后放置一个唯一的终止符。因此,接收方无法可靠地知道长度字符串实际在哪里结束以及图像数据从哪里开始。您的接收器只是盲目地读取 6 个字节(为什么是 6 个?这只适用于大小在 100000-999999 字节之间的图像),但随后用空终止符替换了 7th 字节(因此丢弃内存,因为只分配了 6 个字节的空间),然后读取实际的图像字节。

您应该将图像字节数作为原始二进制数据发送,而不是作为可变长度的字符串。使用 dOut.writeInt(message.length) 将数组 length 作为 4 字节整数(按网络字节顺序)发送。

Socket socket = new Socket("192.168.2.122",1234);
File file = new File("/storage/emulated/0/poza/cam_image.jpg");
FileInputStream fis = new FileInputStream(file);
byte[] message = IOUtils.toByteArray(fis);
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeInt(message.length);
dOut.write(message);

然后接收器可以读取前 4 个字节(如果需要,将字节交换为主机顺序),然​​后将这些字节解释为一个整数,指定要读取多少图像字节:

socket->waitForBytesWriten();
socket->waitForReadyRead(100);
int32_t bs; // <-- be sure to use a 4-byte data type
socket->read((char*)&bs, 4);
bs = ntohl(bs);
//bs is the image length to read

关于java - 使用套接字将 .jpg 从 Android 发送到 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42931648/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com