全屏幕相机预览,无失真/拉伸

时间:2012-03-28 21:10:39

标签: android android-camera

我有一个cameraPreview类(见下文),它正在启动全屏和风景......但是图像被拉伸/扭曲..有没有办法让这个预览保持全屏而不是扭曲?

camLayer:

public class CamLayer extends SurfaceView implements SurfaceHolder.Callback {
       Camera camera;
       SurfaceHolder previewHolder;
       String camID;
       private static final String TAG = "Cam Preview";


       public CamLayer(Context context, String facing)
       {
               super(context);
               camID = facing;
               previewHolder = this.getHolder();
               previewHolder.addCallback(this);
               previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


       }
       public void surfaceCreated(SurfaceHolder holder) {
           startCamera();
       }

       @Override
       public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
       {
               Parameters params = camera.getParameters();
                   //params.setPreviewSize(width, height);
                   //params.setPictureFormat(PixelFormat.JPEG);
                   camera.setParameters(params);
                   camera.startPreview();


       }

       public void surfaceDestroyed(SurfaceHolder arg0)
       {

            stopCamera();
       }



       public void onResume() {

           startCamera();

       }

       public void onPause() {

           stopCamera();
       }


       public void stopCamera(){
            System.out.println("stopCamera method");
            if (camera != null){
                camera.stopPreview();
                camera.setPreviewCallback(null);
                camera.release();
                camera = null;
                previewHolder.removeCallback(this);
                previewHolder = null;
            }
        }

       private void startCamera(){
           if(camID.equals("front")){
               camera=Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
           }else{
               camera=Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
           }
           try {
                   camera.setPreviewDisplay(previewHolder);

                }
                   catch (Throwable e){ Log.w("TAG,", "failed create surface !?!?"); }
        }

       public void draw(Canvas canvas) {
           super.draw(canvas);
           Paint p = new Paint(Color.RED);
           Log.d(TAG, "draw");
           canvas.drawText("PREVIEW", canvas.getWidth() / 2,
                canvas.getHeight() / 2, p);
        }
}

2 个答案:

答案 0 :(得分:1)

您需要运行params.getSupportedPreviewSizes();

从中找到最适合您视图的previewSize

看看这个项目:https://github.com/commonsguy/cw-advandroid/blob/master/Camera/Picture/src/com/commonsware/android/picture/PictureDemo.java

  private Camera.Size getBestPreviewSize(int width, int height,
                                         Camera.Parameters parameters) {
    Camera.Size result=null;

    for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
      if (size.width <= width && size.height <= height) {
        if (result == null) {
          result=size;
        }
        else {
          int resultArea=result.width * result.height;
          int newArea=size.width * size.height;

          if (newArea > resultArea) {
            result=size;
          }
        }
      }
    }

    return(result);
  }

答案 1 :(得分:0)

除了Lena Bru所说的关于找到一个好预览尺寸的内容之外,如果您的CamLayer尺寸与您选择的相机预览尺寸之间没有完全宽高比匹配,那么唯一的选择是避免失真是调整CamLayer的布局。

无论宽高比如何,相机预览都会被拉伸以覆盖整个视图,因此对于未失真的视图,您必须配置视图的布局以匹配所选的预览宽高比。查看Android开发者网站custom components documentation;特别是关于onMeasure的部分。

您需要报告基于预览宽高比的宽度和高度,并且在配置相机预览尺寸以更新视图大小调整时,可能需要触发UI的重新布局。