OpenCV4Android - Displaying Preview in Portrait Mode

Normally OpenCV for Android displays the preview in landscape mode. For STEMbot1 and STEMbot2 I needed to display the preview in a portrait mode.

Here is the code I came up with.

In your manifest file change the orientation from landscape to portrait.


In the onCameraViewStarted() method add:

    public void onCameraViewStarted(int width, int height)


        mRgba = new Mat(height, width, CvType.CV_8UC4);

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

        mRgbaT = new Mat(width, width, CvType.CV_8UC4);  // NOTE width,width is NOT a typo


In the OpenCV preview callback add the following code:

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) 


          mRgba = inputFrame.rgba();

          // Rotate mRgba 90 degrees

          Core.transpose(mRgba, mRgbaT);

          Imgproc.resize(mRgbaT, mRgbaF, mRgbaF.size(), 0,0, 0);

          Core.flip(mRgbaF, mRgba, 1 );

          return mRgba;


Views: 2372

Comment by eric gregori on July 27, 2013 at 3:59pm


You need to be a member of buildsmartrobots to add comments!


© 2015   Created by eric gregori.

Badges  |  Report an Issue  |  Terms of Service