0

I am trying to get the camera from DJI and use OpenCV with it, the problem relies on how to set OpenCv to get the video previewer that DJI is recording while the drone is active. The drone is actually working on streaming the video to my cellphone but when I try to use my OpenCV code to get the video preview id from the layout in the project I have in Android Studio, the app crashes every time I try to go to the camera view part of the app. Here is code that I use to initialize the OpenCv object to the video previewer captured by the DJI camera.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);

    openCvCameraView = (JavaCameraView)findViewById(R.id.video_previewer_surface);
    openCvCameraView.setVisibility(SurfaceView.VISIBLE);
    openCvCameraView.setCvCameraViewListener(this);

    initUI();

    // The callback for receiving the raw H264 video data for camera live view
    mReceivedVideoDataCallBack = new CameraReceivedVideoDataCallback() {

        @Override
        public void onResult(byte[] videoBuffer, int size) {
            if(mCodecManager != null){
                // Send the raw H264 video data to codec manager for decoding
                mCodecManager.sendDataToDecoder(videoBuffer, size);
            }else {
                Log.e(TAG, "mCodecManager is null");
            }
        }
    };

    DJICamera camera = FPVDemoApplication.getCameraInstance();

    if (camera != null) {

        camera.setDJICameraUpdatedSystemStateCallback(new DJICamera.CameraUpdatedSystemStateCallback() {
            @Override
            public void onResult(CameraSystemState cameraSystemState) {
                if (null != cameraSystemState) {

                    int recordTime = cameraSystemState.getCurrentVideoRecordingTimeInSeconds();
                    int minutes = (recordTime % 3600) / 60;
                    int seconds = recordTime % 60;

                    final String timeString = String.format("%02d:%02d", minutes, seconds);
                    final boolean isVideoRecording = cameraSystemState.isRecording();

                    MainActivity.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {

                            recordingTime.setText(timeString);

                            /*
                             * Update recordingTime TextView visibility and mRecordBtn's check state
                             */
                            if (isVideoRecording){
                                recordingTime.setVisibility(View.VISIBLE);
                            }else
                            {
                                recordingTime.setVisibility(View.INVISIBLE);
                            }
                        }
                    });
                }
            }
        });

    }

}
currarpickt
  • 2,290
  • 4
  • 24
  • 39

1 Answers1

0

It may be that you are using JavaCameraView which according to this post: What is the difference between `opencv.android.JavaCameraView` and `opencv.android.NativeCameraView`

The org.opencv.android.JavaCameraView class is implemented inside OpenCV library. It is inherited from CameraBridgeViewBase, that extends SurfaceView and uses standard Android camera API.

You are using the video feed from the DJI SDK and not the phone's hardware camera so that may explain the crash as when you invoke OpenCV it is in conflict with the incoming feed.

As I don't have a drone, my only suggestion is to look at the other DJI sample on Video Stream Decoding

https://github.com/DJI-Mobile-SDK-Tutorials/Android-VideoStreamDecodingSample

And instead of decoding the stream send the data to OpenCV perhaps in JNI (C/C++).

Community
  • 1
  • 1
Morrison Chang
  • 11,691
  • 3
  • 41
  • 77