Android camera programming reference

Source: Internet
Author: User

For reference only

Source: Reprinted

This article is exerpted fromChapter 10: Accessing Android hardwareOf the wrox bookProfessional Android Application Development by Reto MeierAnd is reused by permission of the publisher. This may not be reused without publisher permission

 

Using the camera

The popularity of digital cameras (particle ly within phone handsets) has caused their prices to drop just as their size has shrunk dramatically. it's now becoming difficult to even find a mobile phone without a camera, and Android devices are unlikely to be exceptions.
To access the camera hardware, you need to add the camera permission to your application manifest, as shown here:

Java5 code:
<uses-permission android:name=”android.permission.CAMERA”/>

This grants access to the camera service. The camera class lets you adjust camera settings, take pictures, and manipulate streaming camera previews.
To access the camera service, use the static open method on the camera class. when your application has finished with the camera, remember to relinquish your hold on the service by calling release following the simple use pattern shown in the code snippet below:

Java5 code:
Camera camera = Camera.open();
  [ … Do things with the camera … ]
camera.release();

Controlling camera settings
The current camera settings are available as a camera. Parameters object. Call the getparameters method on the camera to access the current parameters.
You can use the set * methods on the returned parameters to modify the settings. To apply changes, call setparameters, passing in the modified values as shown below:

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

The camera parameters can be used to specify the image and preview size, image format, and preview frame rate.
Using the camera Preview
Access to the camera's streaming video means that you can induplicate ate live video into your applications. Some of the most exciting early Android applications have used this functionality as the basis for augmenting reality.
The camera preview can be displayed in real time onto a surface, as shown in the code snippet below:

Java5 code:
camera.setPreviewDisplay(mySurface);
camera.startPreview();
[ … ]
camera.stopPreview();

You'll learn more about surfaces in the following chapter, although Android between des an excellent example of using a surfaceview to display the camera preview in real time. this example is available in the graphics/camerapreview project in the sdk api demos.
You can also assign a previewcallback to be fired for each preview frame, allowing you to manipulate or display each preview frame individually. call the setpreviewcallback method on the camera object, passing in a new previewcallback implementation overriding the onpreviewframe method as shown here:

Java5 code:
camera.setPreviewCallback(new PreviewCallback() {
 
  public void onPreviewFrame(byte[] _data, Camera _camera) {
   // TODO Do something with the preview image.
  }
});

Taking a picture
Take a picture by calling takepicture on a camera object, passing in a shuttercallback and picturecallback implementations for the raw and JPEG-encoded images. each picture callback will receive a byte array representing the image in the appropriate format, while the shutter callback is triggered immediately after the shutter is closed.

Java5 code:
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.
  }
};

__________________
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.