Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of horizontal and vertical screen of Android OpenCV camera

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to solve the problem of horizontal and vertical screen of Android OpenCV camera? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

When we use OpenCV's own camera JavaCameraView, the vertical screen of the phone will rotate 90 degrees after it is turned on, which is very unfriendly. The following code can realize that we can use JavaCameraView to obtain the corresponding image according to the horizontal and vertical screen of the phone.

There are two main things we need to modify, one is the event onCameraFrame that gets the real-time frame, and the other is the deliverAndDrawFrame method in the CameraBridgeViewBase class in OpenCV.

OnCameraFrame

First copy the image from CameraBridgeViewBase.CvCameraViewFrame to the new Mat.

Then determine whether the current screen is horizontal or vertical.

Change the angle of image rotation by judging whether it is a front camera or a rear camera.

Zoom the rotated image to the size of the JavaCameraView component itself.

Output image

Public Mat onCameraFrame (CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

/ / get the display Mat assigned to frame

Mat frame = inputFrame.rgba ()

/ / judge that the horizontal and vertical screen is used to rotate the image

If (getResources (). GetConfiguration (). Orientation = = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {

/ / judge whether it is a front-facing camera or a rear-facing camera. The angle of flipping is different.

Switch (cameraindex) {

Case CameraBridgeViewBase.CAMERA_ID_FRONT:

Core.rotate (frame, frame, Core.ROTATE_90_COUNTERCLOCKWISE)

Break

Case CameraBridgeViewBase.CAMERA_ID_BACK:

Core.rotate (frame, frame, Core.ROTATE_90_CLOCKWISE)

Break

Default:

Core.rotate (frame, frame, Core.ROTATE_90_CLOCKWISE)

Break

}

/ / scale the rotated Mat image according to the size of the camera screen

Size size = new Size (cameraView.getWidth (), cameraView.getHeight ())

Imgproc.resize (frame, frame, size)

}

Return frame

}

CameraBridgeViewBase

Find the deliverAndDrawFrame method in the CameraBridgeViewBase class

Add after catch

/ / modify here. If not, release the picture and re-create it according to the size.

If (mCacheBitmap! = null) {

MCacheBitmap.recycle ()

MCacheBitmap = Bitmap.createBitmap (modified.width (), modified.height (), Bitmap.Config.ARGB_8888)

}

After adding the above two paragraphs, we turn on the camera again and the image displayed is the normal image.

The answer to the question about how to solve the problem of horizontal and vertical screen of Android OpenCV camera is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report