本文介绍了如何强制Android相机以纵向模式显示,或者让我进行旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的发布,所以请好好!我已经看过几乎所有其他与此相关的问题,并尝试最多,但完全沮丧。我只想让我的Android手机以纵向模式显示相机,而不是横向。我只是修改随opencv提供的ImageManipulations示例。

I'm new to posting so please be nice! I have looked at practically all the other questions related to this and tried most, but am completely frustrated. I simply want to have my Android phone display the camera in portrait mode, not landscape. I am simply modifying the ImageManipulations sample that is provided with opencv.

我使用opencv 3.01,Android sdk版本23,Android Studio 2.0和Nexus 5手机Android版本6.0.1。这是4/28/2016,这是一切的最近的稳定版本。

I'm using opencv 3.01, Android sdk version 23, Android Studio 2.0, and a Nexus 5 phone with Android version 6.0.1. This is 4/28/2016 and this is pretty much the most current stable version of everything.

我已经强制该应用程序进入肖像模式在AndroidManifest.xml文件与:

I have already forced the app into portrait mode in the AndroidManifest.xml file with:

              android:screenOrientation="portrait"

,并且整个应用程序在纵向模式下正确显示,但显示的摄像机图像旋转90度。换句话说,如果你将头部向左倾斜90度,你会看到合适的图像。

and the entire app is correctly displayed in portrait mode, but the camera image that is displayed is rotated 90 degrees. In other words, if you tilt your head to the left 90 degrees you will see the appropriate image. So I need to rotate the image to the right.

我尝试使用opencv Tutorial3代码和setDisplayOrientation(90):

I tried using the opencv Tutorial3 code and setDisplayOrientation(90):

public void setResolution(Size resolution) {
    disconnectCamera();
    mMaxHeight = resolution.height;
    mMaxWidth = resolution.width;
    // mCamera.setDisplayOrientation(90);  //* Crashes if placed here
    connectCamera(getWidth(), getHeight());
    mCamera.setDisplayOrientation(90);  //* Doesn't crash if here, but doesn't work either
}

并没有工作。在任何情况下,本教程使用相机类,已在Android版本21中弃用,并被替换为camera2类。我没有去更远的地方,因为我想使用最新的API,但也许在camera2的东西可以工作吗?

and that didn't work. And in any case this tutorial uses the "camera" class that was deprecated in Android version 21, and was replaced with the "camera2" class. I didn't go any farther on this because I wanted to use the most current APIs, but maybe something in camera2 could work?

有些人发布了关于使用转置和flip函数来旋转图像,我试着在onCameraFrame方法中这么做:

Some people posted about using "transpose" and "flip" functions to rotate the image and I tried doing this many ways in the onCameraFrame method:

原始的,旋转的:

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    Mat rgba = inputFrame.rgba();
    ... do stuff to the image ...
    return rgba;

图像显示变暗,我的FPS计算变为零(奇怪)::

Image display goes dark, my FPS calculation goes to zero (weird)::

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    Mat rgba = inputFrame.rgba();

    // Rotate clockwise 90 degrees
    Core.transpose(rgba, rgba);
    Core.flip(rgba, rgba, 1);
    ... do stuff to the image ...
    return rgba;

旋转180度并再次显示视频,现在翻转,但仍处于横向模式:

Rotated 180 degrees and video displays again, now flipped over, but still landscape mode:

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    Mat rgba = inputFrame.rgba();

    // Rotate clockwise 90 degrees, then 90 degrees again
    Core.transpose(rgba, rgba);
    Core.flip(rgba, rgba, 1);
    Core.transpose(rgba, rgba);
    Core.flip(rgba, rgba, 1);
    ... do stuff to the image ...
    return rgba;

因此,看起来像onCameraFrame返回一个与原始图像具有不同分辨率的Mat, 。 onCameraFrame的文档是粗略的 - 所以,这是真的吗?

So it looks like if onCameraFrame returns a Mat with a different resolution from the original, the display goes blank. The documentation on onCameraFrame is sketchy - so, is this true?

有没有另一个地方,我应该在调用onCameraFrame之前做这个图像旋转?有没有另一种方法,我可以简单地强制相机以纵向模式提供框架?

Is there another place where I should be trying to do this image rotation before onCameraFrame is called? Is there another way that I can simply force the camera to provide the frame in portrait mode instead?

推荐答案

setDisplayOrientation适用于相机的标准预览输出(一个SurfaceView或TextureView直接连接到窗口),但OpenCV在这里,它接收数据的方式不受setDisplayOrientation的影响。

setDisplayOrientation works for the camera's standard preview outputs (A SurfaceView or TextureView directly connected to the camear), but OpenCV is in the middle here, and the way it receives its data is unaffected by setDisplayOrientation.

转置将改变图像数组的尺寸,除非样本的其余部分知道如何处理这个,你会有问题。你必须跟踪代码,看看哪里有关于缓冲区大小(输出缓冲区绘制到UI,例如)的假设,并相应地更改它们是转置大小超过插入转置的点。

The transpose will change the dimensions of the image array, and unless the rest of the sample knows how to handle this, you'll have problems. You'll have to trace through the code to see where there are assumptions about buffer sizes (the output buffer drawn to the UI, for example) and update them accordingly to be the transposed size past the point where you're inserting the transpose.

理想情况下,有一些方法可以在OpenCV正在绘制的输出View上设置方向,但我对OpenCV的样本不够了解,这样的控制存在。

Ideally, there'd be some way to set the orientation on the output View that OpenCV is drawing to, but I don't know enough about OpenCV's samples to say if such a control exists.

这篇关于如何强制Android相机以纵向模式显示,或者让我进行旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:28
查看更多