Inconsapevole
Inconsapevole

Reputation: 23

OpenCV Android - Elaborate Camera in Portrait Mode

I have just start with OpenCv and I use the sdk for android without c++ header right now.

I have imported the example found into sdk folder to my android studio, I have follow some tips found online to make it work. Right now they work and I have tried it, but all that example have an issue. They show the camera preview, or better the frame elaborate from camera then show over display, flipped 90 degree to the right and they are mirrored too.

This is strange for me and I can't to make it work correctly, have read a lot of tips online, it's a very common issue.

I have tried to setDisplayOrientation(90) into JavaCameraView class, but it's work only to show the camera preview, if i try to write on canvas surface it's locked and make error so I can't elaborate the frame anymore.

I have tried to modify the onCameraFrame into my main class but if I try to rotate with core.flip the camera frame by frame I'm going into a stretched image or in a black screen. Finally this method make the response of camera very slow because the elaboration frame by frame.

Anyone have this issue? How could solve it? Is there a method in OpenCv to have the right orientation of camera?

Thanks

Upvotes: 2

Views: 1499

Answers (1)

ImLearning
ImLearning

Reputation: 357

I know it's to late to answer this question .But i finally found the solution which will not make camera frame slow.
You have to make changes in default opencv class . Follow this steps: 1) In CameraBridgeViewBase class add following code

Matrix matrix = new Matrix();
matrix.setRotate(90f);
Bitmap bitmap = Bitmap.createBitmap(mCacheBitmap, 0, 0, mCacheBitmap.getWidth(), mCacheBitmap.getHeight(), matrix, true);

2) now in drawbitmap method replace above bitmap with mCacheBitmap , as like below

if (mScale != 0) {
            canvas.drawBitmap(bitmap, new Rect(0,0,bitmap.getWidth(), bitmap.getHeight()),
                    new Rect((int)((canvas.getWidth() - mScale*bitmap.getWidth()) / 2),
                            (int)((canvas.getHeight() - mScale*bitmap.getHeight()) / 2),
                            (int)((canvas.getWidth() - mScale*bitmap.getWidth()) / 2 + mScale*bitmap.getWidth()),
                            (int)((canvas.getHeight() - mScale*bitmap.getHeight()) / 2 + mScale*bitmap.getHeight())), null);
        } else {
 canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
                    new Rect((canvas.getWidth() - bitmap.getWidth()) / 2,
                            (canvas.getHeight() - bitmap.getHeight()) / 2,
                            (canvas.getWidth() - bitmap.getWidth()) / 2 + bitmap.getWidth(),
                            (canvas.getHeight() - bitmap.getHeight()) / 2 + bitmap.getHeight()), null);

}

3) now , In your JavaCameraView class replace following code in initializeCamera method (changing height ,width for Portrait mode)

if ((getLayoutParams().width == ActionBar.LayoutParams.MATCH_PARENT) && (getLayoutParams().height == ActionBar.LayoutParams.MATCH_PARENT))
                mScale = Math.min(((float)height)/mFrameWidth, ((float)width)/mFrameHeight); 
            else
                mScale = 0;

and you are done !! Hope it helped..

Upvotes: 3

Related Questions