How do I access the camera on Android phones?

前端 未结 4 1705
庸人自扰
庸人自扰 2021-02-13 17:08

I\'ve written a program in Java that takes in an image file and manipulates the image. Now I\'m trying to access the camera so that I can take the photo and give it to the image

4条回答
  •  囚心锁ツ
    2021-02-13 17:52

    Google is your best friend, here are some tutorials:

    Using the camera

    How-To Program The Google Android Camera To Take Pictures

    Take Picture from Camera Emulator

    camera

    First edit your AndroidManifest.xml, add the camera permission:

    
    

    Camera service has to be opened and closed:

    Camera camera = Camera.open();
     //Do things with the camera
    camera.release();
    

    You can set camera settings, e.g.:

    Camera.Parameters parameters = camera.getParameters();
    parameters.setPictureFormat(PixelFormat.JPEG); 
    camera.setParameters(parameters);
    

    To take a picture:

    private void takePicture() {
      camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
    }
    
    ShutterCallback shutterCallback = new ShutterCallback() {
      public void onShutter() {
        // TODO Do something when the shutter closes.
      }
    };
    
    PictureCallback rawCallback = new PictureCallback() {
      public void onPictureTaken(byte[] _data, Camera _camera) {
        // TODO Do something with the image RAW data.
      }
    };
    
    PictureCallback jpegCallback = new PictureCallback() {
      public void onPictureTaken(byte[] _data, Camera _camera) {
        // TODO Do something with the image JPEG data.
      }
    };
    

    Do not forget to add the camera layout to your main layout xml.

提交回复
热议问题