How to send the Mat data via rosbridge? [closed]

asked 2017-01-14 11:14:01 -0600

sasadasd gravatar image

Hello, my purpose is to send the Mat data of the android's display image from Android to ROS PC and commucicate via rosbridge. Both Android and PC has installed Opencv. I would like to directly send Mat data that is not exchange for the other data. But , I can't send the Mat data correctly. The PC is not capable of getting the data (that is to be Error in the middle of sending) and the sending speed is very slow(I displayed the recieved data in PC by using TCP test tool) and sometime freezes showing the data.

Could you let me know the solution? and in case of that I want to send the image data via rosbridge how should I do?

My code(Android studio)

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 {
                             byte[] w ="0".getBytes("UTF8");
                            w = ("raw" + "\r" + "\n" + "\r" + "\n").getBytes("UTF8");
                           sendData(w);
                    } 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 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by sasadasd
close date 2017-02-25 07:22:18.550825