Android中的相机预览

时间:2012-11-03 05:27:46

标签: android camera surfaceview

我开发了一个基于Camera的应用程序。它运行正常但相机和索尼Xperia Sola OS与Android 4.0存在一个问题。

问题在于:

当我从我的应用程序相机预览启动相机时,显示半屏与索尼Xperia Sola设备和任何其他设备HTC,三星它在全屏幕上工作得很好。

对于Camera Preview,我使用了SurfaceView类。在SurfaceChanged()方法中,我使用以下代码

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

    if(display.getRotation() == Surface.ROTATION_0)
    {

        System.out.println("Preview Height : " + h);
        System.out.println("Preview Width  : " + w);

        parameters.setPreviewSize(h, w);                           
        mCamera.setDisplayOrientation(90);
    }

请帮我找一个解决方案。

4 个答案:

答案 0 :(得分:0)

我很好奇运行应用时打印输出的高度和宽度。我实现了一个简单的SurfaceView类并使用了您的代码。通过logcat报告的宽度和高度是

  

I / System.out(4706):预览宽度:480

     

I / System.out(4706):预览高度:744

与SurfaceView跨越的区域大小非常一致。

我用于此的Panel代码:

public class Panel extends SurfaceView implements SurfaceHolder.Callback {
    private Camera  mCamera;
    private Context mContext;

    public Panel(Context context) {
        super(context);
        mContext = context;
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera = Camera.open();
            mCamera.setPreviewDisplay(getHolder());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)    {
        Parameters parameters = mCamera.getParameters();

        Display display = ((WindowManager) mContext.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();

        if (display.getRotation() == Surface.ROTATION_0) {

            System.out.println("Preview Width : " + width);
            System.out.println("Preview Height  : " + height);

            parameters.setPreviewSize(width, height);
            mCamera.setDisplayOrientation(90);
        }
    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

}

请提供更多详情,如有必要,我会尝试进一步调查。

我正在使用4.0.4的Xperia Sola。如果您能为我提供设备的内部版本号,这也很有帮助。

答案 1 :(得分:0)

我们在这里遇到同样的问题,我使用上面的代码来看看它是如何工作的,看起来旋转中心是移位的,所以当旋转90度到肖像时,它只显示了大约10-20%的视图。

好像是:

mCamera.setDisplayOrientation(90);

导致问题。

这可以重现:

  • Sony Xperia ST25i,Android版本4.0.4(版本:6.1.1.B.1.54)
  • Sony Xperia Solo MT27i,Android 4.0.4(版本:6.1.1.B.1.54)

如果相机参数包含任何信息,则以下是

01-22 16:04:01.015: D/CameraTest(1336): Haz camera: true
01-22 16:04:01.095: D/TextLayoutCache(1336): Using debug level: 0 - Debug Enabled: 0
01-22 16:04:01.425: D/libEGL(1336): loaded /system/lib/egl/libGLES_android.so
01-22 16:04:01.505: D/libEGL(1336): loaded /system/lib/egl/libEGL_mali.so
01-22 16:04:01.535: D/libEGL(1336): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-22 16:04:01.545: D/libEGL(1336): loaded /system/lib/egl/libGLESv2_mali.so
01-22 16:04:01.725: D/OpenGLRenderer(1336): Enabling debug mode 0
01-22 16:04:02.987: D/CameraTest(1336): preferred-preview-size-for-video=864x480
01-22 16:04:02.987: D/CameraTest(1336): zoom=0
01-22 16:04:02.987: D/CameraTest(1336): recording-hint=false
01-22 16:04:02.987: D/CameraTest(1336): max-num-detected-faces-hw=5
01-22 16:04:02.987: D/CameraTest(1336): zoom-supported=true
01-22 16:04:02.987: D/CameraTest(1336): whitebalance=auto
01-22 16:04:02.987: D/CameraTest(1336): scene-mode=auto
01-22 16:04:02.987: D/CameraTest(1336): jpeg-quality=85
01-22 16:04:02.987: D/CameraTest(1336): preview-format-values=yuv420sp,yuv420p
01-22 16:04:02.987: D/CameraTest(1336): jpeg-thumbnail-quality=100
01-22 16:04:02.987: D/CameraTest(1336): preview-format=yuv420sp
01-22 16:04:02.987: D/CameraTest(1336): video-size-values=1280x720,864x480,720x480,640x480,352x288,320x240,176x144
01-22 16:04:02.987: D/CameraTest(1336): preview-size=640x480
01-22 16:04:02.987: D/CameraTest(1336): focal-length=3.5
01-22 16:04:02.987: D/CameraTest(1336): flash-mode-values=off,auto,on,red-eye,torch
01-22 16:04:02.987: D/CameraTest(1336): preview-frame-rate-values=15,30
01-22 16:04:02.987: D/CameraTest(1336): max-num-metering-areas=0
01-22 16:04:02.987: D/CameraTest(1336): preview-frame-rate=30
01-22 16:04:02.987: D/CameraTest(1336): focus-mode-values=auto,infinity,macro,fixed,continuous-picture,continuous-video
01-22 16:04:02.987: D/CameraTest(1336): jpeg-thumbnail-width=160
01-22 16:04:02.987: D/CameraTest(1336): video-size=1280x720
01-22 16:04:02.987: D/CameraTest(1336): scene-mode-values=auto,portrait,landscape,night,night-portrait,beach,snow,sports,party,barcode
01-22 16:04:02.987: D/CameraTest(1336): preview-fps-range-values=(1000,15000),(1000,30000)
01-22 16:04:02.987: D/CameraTest(1336): jpeg-thumbnail-size-values=160x120,0x0
01-22 16:04:02.987: D/CameraTest(1336): zoom-ratios=100,107,115,123,132,141,152,162,174,187,200,214,230,246,264,283,303,325,348,373,400,429,459,492,528,566,606,650,696,746,800
01-22 16:04:02.987: D/CameraTest(1336): preview-size-values=864x480,720x480,640x480,480x320,352x288,320x240,176x144
01-22 16:04:02.987: D/CameraTest(1336): picture-size-values=2592x1944,2560x1440,1632x1224,1920x1080,640x480,480x320,320x240
01-22 16:04:02.987: D/CameraTest(1336): preview-fps-range=1000,30000
01-22 16:04:02.987: D/CameraTest(1336): auto-whitebalance-lock=false
01-22 16:04:02.987: D/CameraTest(1336): min-exposure-compensation=-6
01-22 16:04:02.987: D/CameraTest(1336): antibanding=auto
01-22 16:04:02.987: D/CameraTest(1336): max-num-focus-areas=1
01-22 16:04:02.987: D/CameraTest(1336): vertical-view-angle=360
01-22 16:04:02.987: D/CameraTest(1336): horizontal-view-angle=360
01-22 16:04:02.987: D/CameraTest(1336): video-stabilization-supported=false
01-22 16:04:02.987: D/CameraTest(1336): jpeg-thumbnail-height=120
01-22 16:04:02.987: D/CameraTest(1336): smooth-zoom-supported=true
01-22 16:04:02.987: D/CameraTest(1336): focus-mode=macro
01-22 16:04:02.987: D/CameraTest(1336): auto-whitebalance-lock-supported=true
01-22 16:04:02.987: D/CameraTest(1336): video-frame-format=yuv420mb
01-22 16:04:02.987: D/CameraTest(1336): max-num-detected-faces-sw=5
01-22 16:04:02.987: D/CameraTest(1336): picture-format-values=jpeg
01-22 16:04:02.987: D/CameraTest(1336): max-exposure-compensation=6
01-22 16:04:02.987: D/CameraTest(1336): focus-areas=(0,0,0,0,0)
01-22 16:04:02.987: D/CameraTest(1336): video-snapshot-supported=false
01-22 16:04:02.987: D/CameraTest(1336): exposure-compensation=0
01-22 16:04:02.987: D/CameraTest(1336): exposure-compensation-step=0.333333
01-22 16:04:02.987: D/CameraTest(1336): flash-mode=off
01-22 16:04:02.987: D/CameraTest(1336): auto-exposure-lock=false
01-22 16:04:02.987: D/CameraTest(1336): effect-values=none,mono,negative,solarize,sepia,posterize
01-22 16:04:02.987: D/CameraTest(1336): picture-size=2592x1944
01-22 16:04:02.987: D/CameraTest(1336): max-zoom=30
01-22 16:04:02.987: D/CameraTest(1336): effect=none
01-22 16:04:02.987: D/CameraTest(1336): whitebalance-values=auto,incandescent,fluorescent,daylight,cloudy-daylight
01-22 16:04:02.987: D/CameraTest(1336): picture-format=jpeg
01-22 16:04:02.987: D/CameraTest(1336): focus-distances=Infinity,Infinity,Infinity
01-22 16:04:02.987: D/CameraTest(1336): auto-exposure-lock-supported=true
01-22 16:04:02.987: D/CameraTest(1336): antibanding-values=auto,50hz,60hz,off
01-22 16:04:03.037: D/CameraTest(1336): Rotation : 0
01-22 16:04:03.037: D/CameraTest(1336): Preview Width : 480
01-22 16:04:03.037: D/CameraTest(1336): Preview Height  : 744
01-22 16:04:03.057: D/CameraTest(1336): Supported FPS  : [[1000, 15000], [1000, 30000]]

答案 2 :(得分:0)

我已经确认该问题与相机软件有关。

不幸的是,没有可用的解决方法。

唯一的解决方案是未来的固件更新,其中包括针对相机问题的修复程序。

答案 3 :(得分:0)

我遇到了同样的问题,在我使用Sony Experia LT22I的情况下,当我用

旋转屏幕时
  

mCamera.setDisplayOrientation(90);

只显示约10-20%的视图。最后我找到了这款手机的解决方案(补丁)。只有whit才能更改边缘Top(我不知道为什么)这个效果很好,如下所示:

String model = (Build.MANUFACTURER+Build.MODEL).toUpperCase(Locale.getDefault());
if (model.equals(Constants.MODEL_EXCEPTION)) {
    final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)       
    surfaceView.getLayoutParams();
    lpt.setMargins(0,-150,0,0);
    surfaceView.setLayoutParams(lpt);
}