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

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

问题描述

我正在使用预览来显示相机在屏幕上看到的内容.

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.

但在纵向模式下,它总是以错误的 90 度角显示图片.

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

如图:

我知道使用下面的代码会使图片变得清晰:

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

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

但是,我在一个包含其他元素的 Activity 中有预览,并且我的 Activity 以横向模式显示是没有意义的.(默认关闭)

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)

所以我想知道有没有办法改变预览的方向?并让我的 Activity 的其余部分以纵向模式正确显示?

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?

推荐答案

这个问题一开始似乎是某些硬件的错误 参见此处,但可以通过调用 API 8 中提供的 mCamera.setDisplayOrientation(degrees) 来克服.所以这就是我实现它的方式:

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);    
    }
}

这是在 HTC Desire 上,我最初必须在每个轮换检查中放入日志语句以说明轮换是什么,然后在设备上进行调试并在我旋转设备时观察 logCat 输出.对于 HTC Desire,0 是您所期望的手机(纵向),90 度是将手机逆时针旋转 90 度(我以为它会是顺时针方向).在代码中,您会看到当手机处于 90 度或 180 度时,我不需要进行任何显示旋转 - 设备似乎可以自行处理.只有一点不能正常工作: 270 度旋转是当您将设备顺时针旋转 90 度时,显示屏旋转计数器正常,但如果您将设备逆时针旋转 270 度,它似乎无法正确补偿.

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天全站免登陆