Can I use Android Camera in service without preview?

前端 未结 1 457
长发绾君心
长发绾君心 2020-12-30 15:47

I have create a application in Android that using a camera I can measure out the distance between user\'s face to the phone screen.

Problem description:

1条回答
  •  無奈伤痛
    2020-12-30 16:13

    You can refer this Question, which discuss how to do video record in the service. The steps to capture image is same as it.

    To achieve your requirement, you may need:

    1. Get camera instance in your service. Check this Official Guideline.
    2. Setup your camera parameters. Use the APIs like Camera.getParameters(), Camera.setParameters(), Camera.Parameters.setPictureSize(int with, int height) and Camera.Parameters.setPictureFormat(int format) to do so.
    3. Prepare a file used to store the image. You need to implement Camera.PictureCallback to do so.
    4. Call Camera.startPreview() before you takeing picutre. If you don't call this function before taking picture, you will get exception. (Note: You don't need to do Camera.setPreviewdisplay(SurfaceHolder display) first.)
    5. Call camera.takePicture() in your service. Then you can get the captured image stored on the file you specified.

    After it work, don forget to maintain the resource durning lifecycle to acquire/release camera correctly.

    Here is my sample code on Github, it is also mentioned in the comment.

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