Camera API
Camera API
Camera Usage:
<uses-permission android:name="android.permission.CAMERA" />
Camera Features:
<uses-feature android:name="android.hardware.camera" />
Camera Auto-Focus:
<uses-feature android:name="android.hardware.camera.autofocus" />
Package: android.hardware.Camera
Methods:
Camera.open() – Obtain an instance
startPreview() – Starts the Camera preview
takePicture() – Takes a picture
stopPreview() – Stops the Camera preview
release() – Releases the camera
getParameters() – Zoom, Image Quality, Location Information
Detect
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
Access
Use camera.open() to obtain a reference to the Camera. An exception implies
Camera is in use or does not exist.
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
SurfaceHolder.Callback (interface)
(android.view.SurfaceHolder.Callback)
Receives information about changes to the
surface (SurfaceView).
Methods:
surfaceCreated()
surfaceChanged()
surfaceDestroyed()
Preview Layout:
Relative Layout with a Frame Layout to display the Camera Preview, and a
Button which will trigger the capture.
Rotation
activity.getWindowManager().getDefaultDisplay().getRotation();
Returns: ROTATION_0, ROTATION_90, ROTATION_180, ROTATION_270
Orientation: Landscape
Rotation: ROTATION_270
Fix:
Set the Camera Preview Orientation
using
Camera.setDisplayOrientation()
If(Orientation=Landscape &
Rotation=ROTATION_270){
Camera.setDisplayOrientation(180).
}
Activity
new
CameraPreview(Camera) Device Camera
Button Event Handler
Camera Preview
Layout
surfaceCreated() FrameLayout and
surfaceChanged() Button
surfaceDestroyed()