Android - 相机预览是横向 [英] Android - Camera preview is sideways

查看:323
本文介绍了Android - 相机预览是横向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用预览显示相机在屏幕上显示的内容。



我可以得到一切正常,表面创建,表面设置和表面显示。



但是,它总是以肖像模式显示不正确的90度角度的照片。



图片:





我知道使用下面的代码可以直接设置图片:

  setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

但是我在一个活动中有一个包含其他元素的预览,我的活动以横向模式显示。 (默认为禁用)



所以我想知道是否还有只是改变预览的方向?



还是无法旋转预览,以便能正确显示?

解决方案

此问题最初出现在某些硬件上的错误参见这里,但可以通过使用调用mCamera.setDisplayOrientation(degrees)在API 8中可以克服。所以这是我如何实现它:

  public void surfaceChanged(SurfaceHolder holder,int format,int width,int height)
{
if(isPreviewRunning)
{
mCamera.stopPreview();
}

参数parameters = mCamera.getParameters();
显示display =((WindowManager)getSystemService(WINDOW_SERVICE))。getDefaultDisplay();

if(display.getRotation()== Surface.ROTATION_0)
{
parameter.setPreviewSize(height,width);
mCamera.setDisplayOrientation(90);
}

if(display.getRotation()== Surface.ROTATION_90)
{
parameters.setPreviewSize(width,height);
}

if(display.getRotation()== Surface.ROTATION_180)
{
parameter.setPreviewSize(height,width);
}

if(display.getRotation()== Surface.ROTATION_270)
{
parameter.setPreviewSize(width,height);
mCamera.setDisplayOrientation(180);
}

mCamera.setParameters(parameters);
previewCamera();
}

并且previewCamera方法:

  public void previewCamera()
{
try
{
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
isPreviewRunning = true;
}
catch(Exception e)
{
Log.d(APP_CLASS,无法启动预览,e);
}
}

这是在HTC Desire上,在每个旋转检查中记录日志语句以说明旋转是什么,然后在设备上调试,并在旋转设备时观察logCat输出。对于HTC Desire,0是手机,你会想到的(肖像),90度转动手机90度反时钟(我认为它是顺时针)。在代码中,你会看到,我不需要做任何显示旋转,当手机在90或180度 - 设备似乎处理这本身。只有一个点不能正常工作:270度旋转是当您将设备顺时针旋转90度,显示旋转计数器确定,但如果逆时针旋转设备270度,它似乎没有正确补偿。 / p>

PS请注意适当旋转中的宽度和高度的交换。


I am using a Preview to display what the camera see's on the screen.

I can get everything working fine, surface created, surface set and the surface is displayed.

However it always displays the picture at an incorrect 90 degree angle in portrait mode.

Such as in the picture:

I am aware that using the following code will set the picture straight:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

However I have the Preview within an Activity that has other elements in it and it does not make sense for my Activity to be displayed in landscape mode. (Its disabled by default)

So I was wondering is there anyway to just change the orientation of the Preview? And leave the rest of my Activity correctly displayed in Portrait mode?

Or anyway to rotate the preview so that it is displayed correctly?

解决方案

This issue appeared to start out as a bug with certain hardware see here but can be overcome by using the call to mCamera.setDisplayOrientation(degrees) available in API 8. So this is how I implement it:

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {            
        if (isPreviewRunning)
        {
            mCamera.stopPreview();
        }

        Parameters parameters = mCamera.getParameters();
        Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

        if(display.getRotation() == Surface.ROTATION_0)
        {
            parameters.setPreviewSize(height, width);                           
            mCamera.setDisplayOrientation(90);
        }

        if(display.getRotation() == Surface.ROTATION_90)
        {
            parameters.setPreviewSize(width, height);                           
        }

        if(display.getRotation() == Surface.ROTATION_180)
        {
            parameters.setPreviewSize(height, width);               
        }

        if(display.getRotation() == Surface.ROTATION_270)
        {
            parameters.setPreviewSize(width, height);
            mCamera.setDisplayOrientation(180);
        }

        mCamera.setParameters(parameters);
        previewCamera();                      
    }

And the previewCamera method :

public void previewCamera()
{        
    try 
    {           
        mCamera.setPreviewDisplay(mSurfaceHolder);          
        mCamera.startPreview();
        isPreviewRunning = true;
    }
    catch(Exception e)
    {
        Log.d(APP_CLASS, "Cannot start preview", e);    
    }
}

This was on an HTC Desire and I had to initially put in logging statements in each of the rotation checks to say what the rotation was and then debugged on the device and watched the logCat output while I rotated the device. For the HTC Desire, 0 was the phone as you would have expected (portrait), 90 degrees was turning the phone 90 degrees COUNTER-CLOCKWISE (I had assumed it would have been clockwise). In the code you'll see I didn't need to do any display rotation when the phone was at 90 or 180 degrees - the device seemed to handle this itself. Only one point not working properly: The 270 degree rotation is when you turn the device 90 degrees clockwise and the display rotation counters that ok but if you rotate the device 270 degrees counter-clockwise, it doesn't appear to compensate it properly.

P.S. Note the swapover of width and height in the appropriate rotations.

这篇关于Android - 相机预览是横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆