OpenCV - 将数据传输到PC

时间:2018-06-18 17:36:54

标签: android opencv

请问有人解释这段代码中的问题。

我想在每次从相机获取新帧时调用MessageSender类。但它不起作用。我尝试从onCameraFrame()方法调用MessageSender。

这是MainActivity.java:

package com.example.hp.opencvtutorial;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.SurfaceView;
    import android.view.View;
    import android.widget.Toast;

    import org.opencv.android.BaseLoaderCallback;
    import org.opencv.android.CameraBridgeViewBase;
    import org.opencv.android.JavaCameraView;
    import org.opencv.android.OpenCVLoader;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;

    public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{

        CameraBridgeViewBase cameraBridgeViewBase;
        double[] a;

        Mat mat1,mat2,mat3;
        BaseLoaderCallback baseLoaderCallback;
        MessageSender messageSender;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            cameraBridgeViewBase=(JavaCameraView)findViewById(R.id.myCameraView);
            cameraBridgeViewBase.setVisibility(SurfaceView.VISIBLE);
            cameraBridgeViewBase.setCvCameraViewListener(this);

            baseLoaderCallback = new BaseLoaderCallback(this) {


                @Override
                public void onManagerConnected(int status) {

                    switch(status){


                        case BaseLoaderCallback.SUCCESS:

                            cameraBridgeViewBase.enableView();
                            break;

                        default:

                            super.onManagerConnected(status);
                            break;

                    }


                }


            };





        }

        @Override
        public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
            mat1= inputFrame.rgba();
            a = mat1.get(100,100);

            messageSender.execute(Double.toString(a[0]));

            return mat1;
        }



        @Override
        public void onCameraViewStopped() {
            mat1.release();
            mat2.release();
            mat3.release();
        }

        @Override
        public void onCameraViewStarted(int width, int height) {


            mat1 = new Mat(width,height, CvType.CV_8UC4);
            mat2 = new Mat(width,height, CvType.CV_8UC4);
            mat3 = new Mat(width,height, CvType.CV_8UC4);
        }

        @Override
        protected void onPause() {
            super.onPause();

            if(cameraBridgeViewBase!=null){
                cameraBridgeViewBase.disableView();

            }
        }

        @Override
        protected void onResume() {
            super.onResume();
            if(!OpenCVLoader.initDebug()){

                Toast.makeText(getApplicationContext(),"There is a problem in OpenCV.",Toast.LENGTH_SHORT).show();

            }
            else{

                baseLoaderCallback.onManagerConnected(BaseLoaderCallback.SUCCESS);
            }

        }

        @Override
        public void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            if(cameraBridgeViewBase!=null){
                cameraBridgeViewBase.disableView();

            }
        }



    }

这是MessageSender.java类:

package com.example.hp.opencvtutorial;

import android.os.AsyncTask;


import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class MessageSender extends AsyncTask<String,Void,Void>
{

    Socket s;
    PrintWriter pw;


    @Override
    protected Void doInBackground(String... voids) {



        try{
            String message = voids[0];
            s = new Socket("192.168.0.100",5000);
            pw = new PrintWriter(s.getOutputStream());
            pw.write(message);
            pw.flush();
            pw.close();
            s.close();

        }catch(IOException e){e.printStackTrace();}

        return null;
    }
}

非常感谢提前

0 个答案:

没有答案