2014-10-01 81 views
0

现在,我正在做一个示例应用程序,我必须在surfaceview中使用相机。我已经成功地在Surfaceview中设置了相机,但是我无法在其中获得正常的相机视图,宽度和高度也会发生变化。以下是我的代码,我很高兴得到任何人的任何想法。如何在Surfaceview android中获得正常的摄像头视图?

Camera类:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera); 
      cameraObject = isCameraAvailiable(); 
    showCamera = new ShowCamera(this, cameraObject); 
    preview.addView(showCamera); 
      takePhotoButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     cameraObject.takePicture(null, null, capturedIt); 
     cameraObject.stopPreview(); 
     preview.removeView(showCamera); 
     cameraObject.release(); 
     cameraObject = Camera.open(); 
     showCamera = new ShowCamera(Photo.this, cameraObject); 

     preview.addView(showCamera); 
     } 
    }); 
} 

SurfaceHolder类:

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback { 

private SurfaceHolder holdMe; 
private Camera theCamera; 

public ShowCamera(Context context, Camera camera) { 
    super(context); 
    theCamera = camera; 
    holdMe = getHolder(); 
    holdMe.addCallback(this); 
} 

public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    theCamera.stopPreview(); 
    theCamera.setDisplayOrientation(90); 
    camWidth = width; 
    camHeight = height; 
    theCamera.startPreview(); 
    initPreview(width, height); 
} 

private void initPreview(int width, int height) { 
    // Log.i(TAG, "initPreview()starts"); 

    if (theCamera != null) { 
     try { 
      Camera.Parameters param; 
      param = camera.getParameters(); 
      param.setPreviewSize(176, 144); 
      camera.setParameters(param); 
      theCamera.setPreviewDisplay(holdMe); 
     } catch (Throwable t) { 
      Log.e("PreviewDemo-surfaceCallback", 
        "Exception in setPreviewDisplay()", t); 
     } 
    } 

    // Log.i(TAG, "initPreview() ends"); 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    try { 
     theCamera.setDisplayOrientation(90); 
     theCamera.setPreviewDisplay(holdme); 
     theCamera.startPreview(); 
    } catch (IOException e) { 
    } 
} 

public void surfaceDestroyed(SurfaceHolder arg0) { 
    this.getHolder().removeCallback(this); 
    theCamera.stopPreview(); 
    theCamera.release(); 
} 

}

Framelayout.xml

<FrameLayout 
    android:id="@+id/preview1" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:layout_alignParentLeft="true"> 

</FrameLayout> 

enter image description here

+0

的布局参数在你** **相机类,有会员**预览**。它是如何定义和初始化的? – 2014-10-01 08:55:58

+0

上面的代码是一个,你可以看到它。 – Vicky 2014-10-01 09:30:24

+0

不,我只看到像'preview.addView()'或'preview.removeView()'这样的语句,但没有一个像'preview - new Preview(width,height);'。 – 2014-10-01 13:06:49

回答

0

如果您希望显示相机而不失真,则应该覆盖layout_height预览帧。如果您知道该相机支持176×144预览大小,这是你真的想用什么,然后简单地设置预览

showCamera = new ShowCamera(this, cameraObject); 

ViewGroup.LayoutParams lp = preview.getLayoutParams(); 
lp.width = getWindowManager().getDefaultDisplay().getWidth(); 
//*** no, this is wrong!! *** lp.height = lp.width*144/176; 
lp.height = lp.width*176/144; // that's correct for Camera.setDisplayOrientation(90) !! 
preview.setLayoutParams(lp); 

preview.addView(showCamera); 
+0

我已经用上面的解决方案设置宽度和高度,但它仍然是一样的。 – Vicky 2014-10-06 11:51:45

+0

如果我设置cameraview的高度为200dp它显示图像在strech我怎么能得到正常图像 – Vicky 2014-10-06 11:54:49

+0

你应该改变预览的布局,然后再调用'addView(showCamera)'。但是,如果您不关心不同的屏幕尺寸,只需计算屏幕宽度* 144/176并将其设置为您的XML中的layout_height。你应该使用** px **而不是** dp **,或者使用相关的缩放因子。如果你的屏幕截图是1:1,并且屏幕宽度真的是** 200px **,那么高度应该设置为** 163px ** – 2014-10-06 12:02:04

相关问题