我正在开发一款Android应用程序,它可以抓取图像并将其发送到PC客户端进行显示,Android应用程序和PC应用程序都使用Opencv。我要发送的图像是彩色图像(以rbga格式抓取)。
首先我使用以下方法在java应用程序中抓取图像:
InputImage = inputFrame.rgba();
接下来我使用Mat图像变量并使用以下原生(使用JNI)函数将输入图像转换为字节数组:
JNIEXPORT jbyteArray JNICALL Java_com_example_communicationmoduleTCPIP_communicationmoduleTCPIP_ConvertImageToByteArray(
JNIEnv* Env, jobject,
jlong addrInputImage){
Mat& OutputImg = *(Mat*) addrInputImage;
jbyteArray Array;
//__android_log_write(ANDROID_LOG_ERROR, "Tag", "==== 1 ");
// Init java byte array
Array = Env->NewByteArray(1536000);
//__android_log_write(ANDROID_LOG_ERROR, "Tag", "==== 2 ");
// Set byte array region with the size of the SendData CommStruct.
// Now we can send the data back.
Env->SetByteArrayRegion(Array, 0, 1536000, (jbyte*)OutputImg.data);
//__android_log_write(ANDROID_LOG_ERROR, "Tag", "==== 3 ");
return Array;
}
接下来是通过TCP结束字节数组(包含图像数据),具有以下功能:
// Send buffer, the method can be used by both client and server objects.
public void SendByteBuffer(byte[] ByteBuffer){
try {
// Get socket output stream
OutputStream OutputBuffer = ClientSocket.getOutputStream();
//Write byte data to outputstream
OutputBuffer.write(ByteBuffer);
}
catch (IOException e) {
Log.e("TCPIPCommunicator: ", "Client: Failed to send", e);
e.printStackTrace();
}
}
在PC端(用c ++)我用boost lib接收函数接收缓冲区:
int CommunicationModuleTCPIPServer::RecieveBuffer(char Buffer[], int Size){
boost::asio::read(ServerSocket, boost::asio::buffer(TempBuffer, 1536000));
//boost::asio::read(ServerSocket, boost::asio::buffer((char*)InputImage, Size));
//cout <<"Temp buffer: " << TempBuffer << endl;
int ptr=0;
for (int i = 0; i < InputImage.rows; i++) {
for (int j = 0; j < InputImage.cols; j++) {
InputImage.at<cv::Vec4b>(i,j) = cv::Vec4b(TempBuffer[ptr+ 0],TempBuffer[ptr+1],TempBuffer[ptr+2],TempBuffer[ptr+3]);
ptr=ptr+3;
}
}
return COMMSUCCES;
}
然后我用opencv的imshow函数显示变量。问题是我没有在PC端的窗口中获得图像。我认为转换在某个地方出错但我不知道在哪里。有人有想法吗?欢迎提出所有建议和反馈!
我到目前为止的代码如下:
在PC服务器端,我运行一个小程序,它接收图像并尝试用imshow显示它,它调用RecieveImageBuffer函数。主要功能下方。
int ImgReciever(){
cout << "Setting up monitor server with ip: 192.168.2.11:5000" << endl;
CommunicationModuleTCPIPServer TICM("192.168.2.11", 5000);
//CommunicationModuleTCPIPServer TICM("192.168.1.103", 5000);
VisionModule VM;
TICM.RunServer();
char ACK[10];
ACK[0] = '@';
while(1){
cout << "Recieving data...." << endl;
TICM.RecieveImageBuffer(TICM.TempBuffer, 384000);
cout << "Data recieved, going to display image" << endl;
imshow("Testwindow", TICM.InputImage);
Sleep(1000);
TICM.SendBuffer(ACK, 1);
}
return 0;
}
RecieveImageBuffer函数(尝试切换行和列的fors,但程序崩溃)。读取函数al在一次读取中接收34800个字节。 Tempbuffer声明为:
uchar TempBuffer[384000]
int CommunicationModuleTCPIPServer::RecieveImageBuffer(uchar Buffer[], int Size){
Mat Temp;
int bytecount = 0;
int bytecountTotal = 0;
while(bytecountTotal < Size){
bytecount = boost::asio::read(ServerSocket, boost::asio::buffer(Buffer, Size));
cout << "Recieved chunck size: " << bytecount << endl;
bytecountTotal = bytecountTotal + bytecount;
bytecount = 0;
}
cout << "Recieved in total: " + bytecountTotal << endl;
int ptr=0;
for (int i = 0; i < InputImage.rows; i++) {
for (int j = 0; j < InputImage.cols; j++) {
//InputImage.at<cv::Vec4b>(i,j) = cv::Vec4b(TempBuffer[ptr+ 0],TempBuffer[ptr+1],TempBuffer[ptr+2],TempBuffer[ptr+3]);
//ptr=ptr+3;
ptr++;
InputImage.at<uchar>(i,j) = TempBuffer[ptr];
}
}
return COMMSUCCES;
}
在Android端我发送图像,第一次抓取并转换灰度图像,缓冲区大小与pc缓冲区大小匹配。
OutputImage = inputFrame.gray();
long Size = (OutputImage.total() * OutputImage.channels());
CMTCP.bufferByte = new byte[(int) Size];
CMTCP.bufferByte = ConvertMatToByteArray(OutputImage.getNativeObjAddr(), Size);
ConvertMatToByteArray函数如下所示:
JNIEXPORT jbyteArray JNICALL Java_com_example_opencv1_MainActivity_ConvertMatToByteArray(
JNIEnv* Env, jobject, jlong addrInputImage, jlong Size){
Mat& OutputImg = *(Mat*) addrInputImage;
jbyteArray Array;
// Init java byte array
Array = Env->NewByteArray(Size);
// Set byte array region with the size of the SendData CommStruct.
// Now we can send the data back.
Env->SetByteArrayRegion(Array, 0, Size, (jbyte*)OutputImg.data);
return Array;
}
TCP客户端运行一个线程,它发送一个图像缓冲区,然后收到一个新的发送确认。
线程函数如下所示:
while(true){
if(SendFrame){
//System.out.println("Converting image");
// Convert Buffer
//ImgDataSend = ConvertImageToByteArray(InputImage.getNativeObjAddr(), 10, 20, 30);
//System.out.println("Image converted");
// Send new image buffer
System.out.println("Sending frame data");
SendByteBuffer(bufferByte);
SendFrame = false;
System.out.println("Image data send");
// Wait for ACK
while(NoAck){
RecieveBuffer();
//System.out.println("Recieved: " +RecieveString);
if(RecieveString == "@"){
System.out.println("GOT ACK");
bufferByte = new byte[384000];
NoAck = false;
RecieveString = "#";
}
}
}
SendByteBuffer函数如下所示:
// Send buffer, the method can be used by both client and server objects.
public void SendByteBuffer(byte[] ByteBuffer){
/*
BufferedWriter out;
try {
out = new BufferedWriter(new OutputStreamWriter(ClientSocket.getOutputStream()));
out.write(bufferByte.toString());
out.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/
try {
// Get socket output stream
OutputStream OutputBuffer = ClientSocket.getOutputStream();
//Write byte data to outputstream
OutputBuffer.write(bufferByte);
//OutputBuffer.flush();
}
catch (IOException e) {
Log.e("TCPIPCommunicator: ", "Client: Failed to send", e);
e.printStackTrace();
}
}
我接收到发送和确认功能消息,但显示图像的PC端窗口保持灰色并且没有响应(在标题栏中说明)
答案 0 :(得分:1)
我认为您已经验证了通信交换正常,并且平台之间没有小/大端问题,即。发送一个小字节字符串,并在另一侧检查其保存完好。
另外,检查read是否返回所有数据 - 您可能需要调用它直到收到所有数据,特别是对于大字节缓冲区。
inputFrame.rgba()返回一个4通道图像 - 我认为你需要ptr = ptr + 4;