Camera on Android Example

前端 未结 3 914
时光取名叫无心
时光取名叫无心 2020-12-23 10:58

I want to write an activity that:

  1. Shows the camera preview (viewfinder), and has a \"capture\" button.
  2. When the \"capture\" button is pressed, takes a
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 11:24

    The example that CommonsWare gave works well. The example works when using it as-is, but here are the issues I ran into when modifying it for my use case:

    1. Never take a second picture before the first picture has completed, in other words PictureCallback.onPictureTaken() has been called. The CommonsWare example uses the inPreview flag for this purpose.
    2. Make sure that your SurfaceView is full-screen. If you want a smaller preview you might need to change the preview size selection logic, otherwise the preview might not fit into the SurfaceView on some devices. Some devices only support a full-screen preview size, so keeping it full-screen is the simplest solution.

    To add more components to the preview screen, FrameLayout works well in my experience. I started by using a LinearLayout to add text above the preview, but that broke rule #2. When using a FrameLayout to add components on top of the preview, you don't have any issues with the preview resolution.

    I also posted a minor issue relating to Camera.open() on GitHub.

提交回复
热议问题