"I want to run the sample program ""face-detection"" in-camera + vertical orientation on Android + OpenCV."

Asked 2 years ago, Updated 2 years ago, 81 views

I looked for the OpenCV reference and source code, but I want to run it in camera + vertical direction, but I can't do it as the camera preview was intended.
If your smartphone is sideways, the preview is normal.Vertical orientation causes the preview to be upside down.
I would like to use setDisplayOrientation and setRotation upside down, but my program did not work as intended.
Please let me know if you know.

  • Eclipse 4.4+ADT
  • API19
  • Nexus5

What I've tried.
Verified that the CameraBridgeViewBase type of the member variable mOpenCvCameraView works with the JavaCameraView type. ->Verified that it works as intended.

JavaCameraViewCreate a JavaCameraView2 class that inherited the JavaCameraView and add a method to change the orientation

FdActivityIn the class onCreate method, call the created method
->Here we are.

public class JavaCameraView2 extensions JavaCameraView{
    // snip..
    public void setCameraRotation(intangle){
        Log.i(TAG, "called setCameraRotation");

        try{
            This.mCamera.setDisplayOrientation(angle);

        } catch(Exceptione){
            Log.e(TAG, "failed camera setting");
            e.printStackTrace();
        }
    }
}


public class FdActivity extensions Activity implements CvCameraViewListener2 {
    private JavaCameraView 2mOpenCvCameraView;
// snip..
    /** Called when the activity is first created.*/
    @ Override
    public void onCreate (Bundle savedInstanceState) {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.face_detect_surface_view);

        mOpenCvCameraView=(JavaCameraView2) findViewById(R.id.fd_activity_surface_view);
        mOpenCvCameraView.setCvCameraViewListener(this);
        mOpenCvCameraView.setCameraRotation(90);
    }
// snip..
}

mOpenCvCameraView.setCameraRotation(90); generates Java.lang.NullPointerException.

Thank you for your cooperation.

Additional
Based on your advice, I added code to the "onResume" method.

public void onResume(){
  super.onResume();

  OpenCVLoader.initAsync (OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
  // Additional hereinafter
  mOpenCvCameraView.setVisibility (SurfaceView.VISIBLE);
  mOpenCvCameraView.setCameraRotation(90);
  mOpenCvCameraView.enableView();
}

The result was a NullPointerException and did not work as intended.
Please let me know if you have any information to help me.

1/27 Additional
I was able to set the preview in the intended direction.Please refer to the following site.
OpenCV4 Android-Displaying Preview in Portrait Mode

Immediately after opening the camera, the setDisplayOrientation method does not change its orientation. I think OpenCV has been coded so that I can't change the orientation, so I adjusted the orientation on the reference site
I did.
However, if you keep doing this, the ratio will not be correct, so it will be previewed in a crushed state.
I am not sure if this method is correct.

Now, I'm adjusting the ratio so that it doesn't collapse.

public class FdActivity extensions Activity implements CvCameraViewListener2{
    private MatmRgbaF;
    private MatmRgbaT;

    public void onCameraViewStarted(int width, int height) {
        mGray = new Mat();
        mRgba = new Mat();

        mRgbaF = new Mat(height, width, CvType.CV_8UC4);
        mRgbaT = new Mat (width, width, CvType.CV_8UC4);
    }

    public MatonCameraFrame (CvCameraViewFrame inputFrame) {
        // omission

        // Rotate mRgba90 degrees
        Core.transpose (mRgba, mRgbaT);
        Imgproc.resize(mRgbaT, mRgbaF, mRgbaF.size(), 0, 0);
        Core.flip(mRgbaF,mRgba,0);

        return mRgba;
    }

}

If you have any information other than this method, please let me know.
Thank you for your cooperation.

Note 2/9
With the above method, the preview screen was crushed (the ratio of vertical to horizontal is strange), and it didn't work out as intended, so I was looking for a website that I could refer to.
I checked the following site and confirmed that the back camera + portrait will work as intended, but the preview screen will be upside down with the in-camera + portrait.

Reference Site:
http://answers.opencv.org/question/7313/rotating-android-camera-to-portrait/
http://littlecheesecake.me/blog/13804736/opencv-android-orientation

In conclusion, OpenCV cannot operate as intended under "in-camera + portrait" conditions.I have determined that
Thank you.

android opencv

2022-09-30 19:21

1 Answers

in mOpenCvCameraView.setVisibility (SurfaceView.VISIBLE); The reason for the drop is that mCamera is null because it is not set to VISIBLE and the status is not indicated by enableView.

If you follow the opencv sample source code, call enableView in onResune and then do the above.
Affected by: https://github.com/Itseez/opencv/blob/79f593a42923a411fb0c421cd3b766acee004387/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java#L100

The following is the basis and the foot of the snake.
initializeCameraconnectCamera and
for the instance to enter mCamera This connectCamera must be called, unless explicitly called
On line 355 of CameraBridgeViewBase,
Yes, to get there, checkCurrentState() in the same class must be called processEnterState(), but this conditional expression was as follows:
mEnabled & mSurfaceExist & getVisibility()==VISIBLE
Therefore, set the situation to apply the above conditional expression and
processEnterState() (actually surfaceChanged()) is tapped to notify you of a state change.
I have to.


2022-09-30 19:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.