Android Camera的編程參考

來源:互聯網
上載者:User

僅供參考

來源:轉載

This article is exerpted from chapter 10: Accessing Android Hardware of the Wrox book Professional Android Application Development by Reto Meier and is reused by permission of the publisher. This may not be reused without publisher permission

 

Using the Camera

The popularity of digital cameras (particularly 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 incorporate 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 includes 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.
  }
};

__________________
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.