我的目的是将Android的显示图像的Mat数据从Android发送到PC。 Android和PC都安装了Opencv。我想直接发送不交换其他数据的Mat数据。但是,我无法正确发送Mat数据。 PC无法获取数据(即发送过程中的错误),发送速度非常慢(我使用TCP测试工具在PC上显示收到的数据),有时冻结显示数据。 / p>
你能让我知道解决方案吗?
我的代码
package opencv.ros.com.camera_opencv;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.WindowManager;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener {
private CameraBridgeViewBase mCameraView;
private Mat mOutputFrame;
private static final int PORT = 9250;
private static final String IP_ADDR = "111.111.111.11";
private volatile int Flag = 0;
private Socket socket;
private InputStream in;
private OutputStream out;
private StringBuffer Sendmessage;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
mCameraView.enableView();
try {
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
break;
default:
super.onManagerConnected(status);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
mCameraView = (CameraBridgeViewBase) findViewById(R.id.camera_view);
mCameraView.setCvCameraViewListener(this);
}
@Override
public void onPause() {
if (mCameraView != null) {
mCameraView.disableView();
}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_4, this, mLoaderCallback);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Flag = 1;
System.out.println(Flag);
}
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mCameraView != null) {
mCameraView.disableView();
}
}
@Override
public void onCameraViewStarted(int width, int height) {
// Mat(int rows, int cols, int type)
mOutputFrame = new Mat(width, height, CvType.CV_8UC3);
}
@Override
public void onCameraViewStopped() {
mOutputFrame.release();
}
@Override
public Mat onCameraFrame(Mat inputFrame) throws UnsupportedEncodingException {
Core.absdiff(inputFrame, new Scalar(0, 0, 0), mOutputFrame);
if (Flag == 1) {
Flag = 0;
String a = mOutputFrame.dump();
String b= a.replaceAll(";",",");
byte[] ds = a.getBytes("UTF8");
byte[] bytedata ="0".getBytes("UTF8");
Sendmessage=new StringBuffer();
Sendmessage.append("{"+"\""+"op"+"\""+":"+ "\""+"publish"+"\""+","+"\""+"msg"+"\""+":"+"{"+"\""+"header"+"\""+":"+"{"+"\""+"seq"+"\""+":"+0+","+"\""+"stamp"+"\""+":"+"{"+"\""+"secs"+"\""+":"+0+","+"\""+"nsecs"+"\""+":"+0+"}"+","+"\""+"frame_id"+"\""+":"+"\""+"ss"+"\""+"}"+","+"\""+"height"+"\""+":");
Sendmessage.append(mOutputFrame.height());
Sendmessage.append(","+"\""+"width"+"\""+":");
Sendmessage.append(mOutputFrame.width());
Sendmessage.append(","+"\""+"encoding"+"\""+":"+"\""+"bgr8"+"\""+","+"\""+"is_bigendian"+"\""+":"+0+","+"\""+"step"+"\""+":");
Sendmessage.append(mOutputFrame.width()*3);
Sendmessage.append(","+"\""+"data"+"\""+":");
Sendmessage.append(b);
Sendmessage.append("}"+","+"\""+"topic"+"\""+":"+"\""+"/camera/image"+"\""+"}");
String c1 = new String(Sendmessage);
System.out.println(c1);
try {
bytedata=c1.getBytes("UTF8");
sendData(bytedata);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return mOutputFrame;
}
private void sendData(final byte[] data) {
Thread thread = new Thread(new Runnable() {
public void run() {
Socket socket;
BufferedOutputStream out;
try {
socket = new Socket(IP_ADDR, PORT);
out = new BufferedOutputStream(socket.getOutputStream());
out.write(data);
if (out != null) out.close();
if (socket != null) socket.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
thread.start();
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
}