Make a SurfaceView larger than the screen (Fitting a camera preview to a SurfaceView larger than the display)

前端 未结 1 1170
半阙折子戏
半阙折子戏 2020-12-02 11:40

I have a custom camera application and I want for any preview sizes to display it in full screen mode without stretching the camera preview image. For this, I need to make t

相关标签:
1条回答
  • 2020-12-02 12:18

    First, remove source of crashes: startPreviewCamera called in onResume. Camera preview shall be started in SurfaceHolder.Callback methods.

    Then you should know that you can set preview size only to sizes reported by Camera.Parameters.getSupportedPreviewSizes. And these sizes will most likely be smaller or equal to device's screen size.

    Then you simply call

    Camera.Parameters p = camera.getParameters();
    p.setPreviewSize(w, h); // one of supported sizes
    camera.setParameters(p);
    

    Then Surface of preview will have that size (possibly rotated and w/h swapped). And this surface will be rescaled by Android to size of your CameraPreview view when being drawn, so it's also important how you set size of your CameraPreview.

    You can set fixed size of your CameraPreview simply by calling

    previewCamera.setLayoutParams(new FrameLayout.LayoutParams(w, h));
    

    So in short, you set requested preview size in Camera.setParameters, and you size your preview view as desired, possibly to same size as preview, as is your requirement. Your preview view then may be equal to screen size or be smaller (assuming camera doesn't provide preview bigger than screen). If camera provides bigger preview than screen, you can still call preview.setX, preview.setY to move it around.

    0 讨论(0)
提交回复
热议问题