本文介绍了Android的 - 摄像头preVIEW横盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是preVIEW显示什么相机看到的在屏幕上。

我能得到的一切工作正常,创建面,面集并显示在表面。

然而,它始终显示的画面在纵向模式不正确的90度角。

如在图片:

据我所知,使用下面的code将直接设置图片:

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 

不过,我有一个在它的其他元素的活动中的preVIEW,它没有任何意义了我的活动显示在横向模式。 (它的禁用默认情况下)

所以我想知道有没有无论如何只是改变的preVIEW的方向?并留下我的活动的其余部分正确肖像模式显示?

或者反正旋转preVIEW,使其正确显示?

解决方案

这个问题似乎开始了与某些硬件的,但可以通过调用mCamera.setDisplayOrientation(度)的API 8.可这就是我怎么实现它需要克服:?

 公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,高度INT)
    {
        如果(是previewRunning)
        {
            mCamera.stop preVIEW();
        }

        参数参数= mCamera.getParameters();
        显示显示=((窗口管理器)getSystemService(WINDOW_SERVICE))getDefaultDisplay()。

        如果(display.getRotation()== Surface.ROTATION_0)
        {
            parameters.set previewSize(高度,宽度);
            mCamera.setDisplayOrientation(90);
        }

        如果(display.getRotation()== Surface.ROTATION_90)
        {
            parameters.set previewSize(宽,高);
        }

        如果(display.getRotation()== Surface.ROTATION_180)
        {
            parameters.set previewSize(高度,宽度);
        }

        如果(display.getRotation()== Surface.ROTATION_270)
        {
            parameters.set previewSize(宽,高);
            mCamera.setDisplayOrientation(180);
        }

        mCamera.setParameters(参数);
        previewCamera();
    }
 

而previewCamera方式:

 公共无效previewCamera()
{
    尝试
    {
        mCamera.set previewDisplay(mSurfaceHolder);
        mCamera.start preVIEW();
        为previewRunning = TRUE;
    }
    赶上(例外五)
    {
        Log.d(APP_CLASS,无法启动preVIEW,E);
    }
}
 

这是上的HTC Desire,我不得不开始把在每个旋转检查日志语句说什么旋转是再调试的设备上观看的logcat输出,同时我旋转设备。对于HTC Desire的,0是手机,你本来期望(纵向),90度被打开手机90度逆时针(我曾以为它会是顺时针方向)。在code,你会看到我并不需要做任何屏幕旋转功能,当手机在90度或180度 - 该设备似乎来处理这本身。只有一个点不能正常工作:当你打开设备顺时针旋转90度,并且确定,但如果你旋转设备270度逆时针,这似乎并没有适当地补偿它的屏幕旋转柜的270度旋转

P.S。注意的宽度和高度在相应的转速的swapover

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的 - 摄像头preVIEW横盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 14:41