Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 1 | page.title=Camera |
Joe Fernandez | 33baa5a | 2013-11-14 11:41:19 -0800 | [diff] [blame] | 2 | page.tags=photo,video,picture,mediarecorder |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 3 | @jd:body |
| 4 | |
| 5 | <div id="qv-wrapper"> |
| 6 | <div id="qv"> |
| 7 | <h2>In this document</h2> |
| 8 | <ol> |
| 9 | <li><a href="#considerations">Considerations</a></li> |
| 10 | <li><a href="#basics">The Basics</a> |
| 11 | <li><a href="#manifest">Manifest Declarations</a></li> |
| 12 | <li><a href="#intents">Using Existing Camera Apps</a> |
| 13 | <ol> |
| 14 | <li><a href="#intent-image">Image capture intent</a></li> |
| 15 | <li><a href="#intent-video">Video capture intent</a></li> |
| 16 | <li><a href="#intent-receive">Receiving camera intent result</a></li> |
| 17 | </ol> |
| 18 | <li><a href="#custom-camera">Building a Camera App</a> |
| 19 | <ol> |
| 20 | <li><a href="#detect-camera">Detecting camera hardware</a></li> |
| 21 | <li><a href="#access-camera">Accessing cameras</a></li> |
| 22 | <li><a href="#check-camera-features">Checking camera features</a></li> |
| 23 | <li><a href="#camera-preview">Creating a preview class</a></li> |
| 24 | <li><a href="#preview-layout">Placing preview in a layout</a></li> |
| 25 | <li><a href="#capture-picture">Capturing pictures</a></li> |
| 26 | <li><a href="#capture-video">Capturing videos</a></li> |
| 27 | <li><a href="#release-camera">Releasing the camera</a></li> |
| 28 | </ol> |
| 29 | </li> |
| 30 | <li><a href="#saving-media">Saving Media Files</a></li> |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 31 | <li><a href="#camera-features">Camera Features</a> |
| 32 | <ol> |
| 33 | <li><a href="#check-feature">Checking feature availability</a></li> |
| 34 | <li><a href="#using-features">Using camera features</a></li> |
| 35 | <li><a href="#metering-focus-areas">Metering and focus areas</a></li> |
| 36 | <li><a href="#face-detection">Face detection</a></li> |
| 37 | <li><a href="#time-lapse-video">Time lapse video</a></li> |
| 38 | </ol> |
| 39 | </li> |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 40 | </ol> |
| 41 | <h2>Key Classes</h2> |
| 42 | <ol> |
| 43 | <li>{@link android.hardware.Camera}</li> |
| 44 | <li>{@link android.view.SurfaceView}</li> |
| 45 | <li>{@link android.media.MediaRecorder}</li> |
| 46 | <li>{@link android.content.Intent}</li> |
| 47 | </ol> |
| 48 | <h2>See also</h2> |
| 49 | <ol> |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 50 | <li><a href="{@docRoot}guide/topics/media/mediaplayer.html">Media Playback</a></li> |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 51 | <li><a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a></li> |
| 52 | </ol> |
| 53 | </div> |
| 54 | </div> |
| 55 | |
| 56 | |
| 57 | <p>The Android framework includes support for various cameras and camera features available on |
| 58 | devices, allowing you to capture pictures and videos in your applications. This document discusses a |
| 59 | quick, simple approach to image and video capture and outlines an advanced approach for creating |
| 60 | custom camera experiences for your users.</p> |
| 61 | |
| 62 | <h2 id="considerations">Considerations</h2> |
| 63 | <p>Before enabling your application to use cameras on Android devices, you should consider a few |
| 64 | questions about how your app intends to use this hardware feature.</p> |
| 65 | |
| 66 | <ul> |
| 67 | <li><strong>Camera Requirement</strong> - Is the use of a camera so important to your |
| 68 | application that you do not want your application installed on a device that does not have a |
| 69 | camera? If so, you should declare the <a href="#manifest">camera requirement in your |
| 70 | manifest</a>.</li> |
| 71 | |
| 72 | <li><strong>Quick Picture or Customized Camera</strong> - How will your application use the |
| 73 | camera? Are you just interested in snapping a quick picture or video clip, or will your application |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 74 | provide a new way to use cameras? For a getting a quick snap or clip, consider |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 75 | <a href="#intents">Using Existing Camera Apps</a>. For developing a customized camera feature, check |
| 76 | out the <a href="#custom-camera">Building a Camera App</a> section.</li> |
| 77 | |
| 78 | <li><strong>Storage</strong> - Are the images or videos your application generates intended to be |
| 79 | only visible to your application or shared so that other applications such as Gallery or other |
| 80 | media and social apps can use them? Do you want the pictures and videos to be available even if your |
| 81 | application is uninstalled? Check out the <a href="#saving-media">Saving Media Files</a> section to |
| 82 | see how to implement these options.</li> |
| 83 | </ul> |
| 84 | |
| 85 | |
| 86 | |
| 87 | <h2 id="basics">The Basics</h2> |
| 88 | <p>The Android framework supports capturing images and video through the |
| 89 | {@link android.hardware.Camera} API or camera {@link android.content.Intent}. Here are the relevant |
| 90 | classes:</p> |
| 91 | |
| 92 | <dl> |
| 93 | <dt>{@link android.hardware.Camera}</dt> |
| 94 | <dd>This class is the primary API for controlling device cameras. This class is used to take |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 95 | pictures or videos when you are building a camera application.</dd> |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 96 | |
| 97 | <dt>{@link android.view.SurfaceView}</dt> |
| 98 | <dd>This class is used to present a live camera preview to the user.</dd> |
| 99 | |
| 100 | <dt>{@link android.media.MediaRecorder}</dt> |
| 101 | <dd>This class is used to record video from the camera.</dd> |
| 102 | |
| 103 | <dt>{@link android.content.Intent}</dt> |
| 104 | <dd>An intent action type of {@link android.provider.MediaStore#ACTION_IMAGE_CAPTURE |
| 105 | MediaStore.ACTION_IMAGE_CAPTURE} or {@link android.provider.MediaStore#ACTION_VIDEO_CAPTURE |
| 106 | MediaStore.ACTION_VIDEO_CAPTURE} can be used to capture images or videos without directly |
| 107 | using the {@link android.hardware.Camera} object.</dd> |
| 108 | </dl> |
| 109 | |
| 110 | |
| 111 | <h2 id="manifest">Manifest Declarations</h2> |
| 112 | <p>Before starting development on your application with the Camera API, you should make sure |
| 113 | your manifest has the appropriate declarations to allow use of camera hardware and other |
| 114 | related features.</p> |
| 115 | |
| 116 | <ul> |
| 117 | <li><strong>Camera Permission</strong> - Your application must request permission to use a device |
| 118 | camera. |
| 119 | <pre> |
| 120 | <uses-permission android:name="android.permission.CAMERA" /> |
| 121 | </pre> |
| 122 | <p class="note"><strong>Note:</strong> If you are using the camera <a href="#intents">via an |
| 123 | intent</a>, your application does not need to request this permission.</p> |
| 124 | </li> |
| 125 | <li><strong>Camera Features</strong> - Your application must also declare use of camera features, |
| 126 | for example: |
| 127 | <pre> |
| 128 | <uses-feature android:name="android.hardware.camera" /> |
| 129 | </pre> |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 130 | <p>For a list of camera features, see the manifest |
| 131 | <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html#hw-features">Features |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 132 | Reference</a>.</p> |
Dirk Dougherty | 4d7bc655 | 2012-01-27 17:56:49 -0800 | [diff] [blame] | 133 | <p>Adding camera features to your manifest causes Google Play to prevent your application from |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 134 | being installed to devices that do not include a camera or do not support the camera features you |
Dirk Dougherty | 4d7bc655 | 2012-01-27 17:56:49 -0800 | [diff] [blame] | 135 | specify. For more information about using feature-based filtering with Google Play, see <a |
| 136 | href="{@docRoot}guide/topics/manifest/uses-feature-element.html#market-feature-filtering">Google |
| 137 | Play and Feature-Based Filtering</a>.</p> |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 138 | <p>If your application <em>can use</em> a camera or camera feature for proper operation, but does |
| 139 | not <em>require</em> it, you should specify this in the manifest by including the {@code |
| 140 | android:required} attribute, and setting it to {@code false}:</p> |
| 141 | <pre> |
| 142 | <uses-feature android:name="android.hardware.camera" android:required="false" /> |
| 143 | </pre> |
| 144 | |
| 145 | </li> |
| 146 | <li><strong>Storage Permission</strong> - If your application saves images or videos to the |
| 147 | device's external storage (SD Card), you must also specify this in the manifest. |
| 148 | <pre> |
| 149 | <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
| 150 | </pre> |
| 151 | </li> |
| 152 | <li><strong>Audio Recording Permission</strong> - For recording audio with video capture, your |
| 153 | application must request the audio capture permission. |
| 154 | <pre> |
| 155 | <uses-permission android:name="android.permission.RECORD_AUDIO" /> |
| 156 | </pre> |
| 157 | </li> |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 158 | <li><strong>Location Permission</strong> - If your application tags images with GPS location |
| 159 | information, you must request location permission: |
| 160 | <pre> |
| 161 | <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> |
| 162 | </pre> |
| 163 | <p>For more information about getting user location, see |
Scott Main | 50e990c | 2012-06-21 17:14:39 -0700 | [diff] [blame] | 164 | <a href="{@docRoot}guide/topics/location/strategies.html">Location Strategies</a>.</p> |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 165 | </li> |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 166 | </ul> |
| 167 | |
| 168 | |
| 169 | <h2 id="intents">Using Existing Camera Apps</h2> |
| 170 | <p>A quick way to enable taking pictures or videos in your application without a lot of extra code |
| 171 | is to use an {@link android.content.Intent} to invoke an existing Android camera application. A |
| 172 | camera intent makes a request to capture a picture or video clip through an existing camera app and |
| 173 | then returns control back to your application. This section shows you how to capture an image or |
| 174 | video using this technique.</p> |
| 175 | |
| 176 | <p>The procedure for invoking a camera intent follows these general steps:</p> |
| 177 | |
| 178 | <ol> |
| 179 | <li><strong>Compose a Camera Intent</strong> - Create an {@link android.content.Intent} that |
| 180 | requests an image or video, using one of these intent types: |
| 181 | <ul> |
| 182 | <li>{@link android.provider.MediaStore#ACTION_IMAGE_CAPTURE MediaStore.ACTION_IMAGE_CAPTURE} - |
| 183 | Intent action type for requesting an image from an existing camera application.</li> |
| 184 | <li>{@link android.provider.MediaStore#ACTION_VIDEO_CAPTURE MediaStore.ACTION_VIDEO_CAPTURE} - |
| 185 | Intent action type for requesting a video from an existing camera application. </li> |
| 186 | </ul> |
| 187 | </li> |
| 188 | <li><strong>Start the Camera Intent</strong> - Use the {@link |
| 189 | android.app.Activity#startActivityForResult(android.content.Intent, int) startActivityForResult()} |
| 190 | method to execute the camera intent. After you start the intent, the Camera application user |
| 191 | interface appears on the device screen and the user can take a picture or video.</li> |
| 192 | <li><strong>Receive the Intent Result</strong> - Set up an {@link |
| 193 | android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} method |
| 194 | in your application to receive the callback and data from the camera intent. When the user |
| 195 | finishes taking a picture or video (or cancels the operation), the system calls this method.</li> |
| 196 | </ol> |
| 197 | |
| 198 | |
| 199 | <h3 id="intent-image">Image capture intent</h3> |
| 200 | <p>Capturing images using a camera intent is quick way to enable your application to take pictures |
| 201 | with minimal coding. An image capture intent can include the following extra information:</p> |
| 202 | |
| 203 | <ul> |
| 204 | <li>{@link android.provider.MediaStore#EXTRA_OUTPUT MediaStore.EXTRA_OUTPUT} - This setting |
| 205 | requires a {@link android.net.Uri} object specifying a path and file name where you'd like to |
| 206 | save the picture. This setting is optional but strongly recommended. If you do not specify this |
| 207 | value, the camera application saves the requested picture in the default location with a default |
| 208 | name, specified in the returned intent's {@link android.content.Intent#getData() Intent.getData()} |
| 209 | field.</li> |
| 210 | </ul> |
| 211 | |
| 212 | <p>The following example demonstrates how to construct a image capture intent and execute it. |
| 213 | The {@code getOutputMediaFileUri()} method in this example refers to the sample code shown in <a |
| 214 | href= "#saving-media">Saving Media Files</a>.</p> |
| 215 | |
| 216 | <pre> |
| 217 | private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; |
| 218 | private Uri fileUri; |
| 219 | |
| 220 | @Override |
| 221 | public void onCreate(Bundle savedInstanceState) { |
| 222 | super.onCreate(savedInstanceState); |
| 223 | setContentView(R.layout.main); |
| 224 | |
| 225 | // create Intent to take a picture and return control to the calling application |
| 226 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 227 | |
| 228 | fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image |
| 229 | intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name |
| 230 | |
| 231 | // start the image capture Intent |
| 232 | startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); |
| 233 | } |
| 234 | </pre> |
| 235 | |
| 236 | <p>When the {@link android.app.Activity#startActivityForResult(android.content.Intent, int) |
| 237 | startActivityForResult()} method is executed, users see a camera application interface. |
| 238 | After the user finishes taking a picture (or cancels the operation), the user interface returns to |
| 239 | your application, and you must intercept the {@link |
| 240 | android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} |
| 241 | method to receive the result of the intent and continue your application execution. For information |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 242 | on how to receive the completed intent, see <a href="#intent-receive">Receiving camera intent |
| 243 | result</a>.</p> |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 244 | |
| 245 | |
| 246 | <h3 id="intent-video">Video capture intent</h3> |
| 247 | <p>Capturing video using a camera intent is a quick way to enable your application to take videos |
| 248 | with minimal coding. A video capture intent can include the following extra information:</p> |
| 249 | |
| 250 | <ul> |
| 251 | <li>{@link android.provider.MediaStore#EXTRA_OUTPUT MediaStore.EXTRA_OUTPUT} - This setting |
| 252 | requires a {@link android.net.Uri} specifying a path and file name where you'd like to save the |
| 253 | video. This setting is optional but strongly recommended. If you do not specify this value, the |
| 254 | Camera application saves the requested video in the default location with a default name, specified |
| 255 | in the returned intent's {@link android.content.Intent#getData() Intent.getData()} field.</li> |
| 256 | <li>{@link android.provider.MediaStore#EXTRA_VIDEO_QUALITY MediaStore.EXTRA_VIDEO_QUALITY} - |
| 257 | This value can be 0 for lowest quality and smallest file size or 1 for highest quality and |
| 258 | larger file size.</li> |
| 259 | <li>{@link android.provider.MediaStore#EXTRA_DURATION_LIMIT MediaStore.EXTRA_DURATION_LIMIT} - |
| 260 | Set this value to limit the length, in seconds, of the video being captured.</li> |
| 261 | <li>{@link android.provider.MediaStore#EXTRA_SIZE_LIMIT MediaStore.EXTRA_SIZE_LIMIT} - |
| 262 | Set this value to limit the file size, in bytes, of the video being captured. |
| 263 | </li> |
| 264 | </ul> |
| 265 | |
| 266 | <p>The following example demonstrates how to construct a video capture intent and execute it. |
| 267 | The {@code getOutputMediaFileUri()} method in this example refers to the sample code shown in <a |
| 268 | href= "#saving-media">Saving Media Files</a>.</p> |
| 269 | |
| 270 | <pre> |
| 271 | private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200; |
| 272 | private Uri fileUri; |
| 273 | |
| 274 | @Override |
| 275 | public void onCreate(Bundle savedInstanceState) { |
| 276 | super.onCreate(savedInstanceState); |
| 277 | setContentView(R.layout.main); |
| 278 | |
| 279 | //create new Intent |
| 280 | Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); |
| 281 | |
| 282 | fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video |
| 283 | intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name |
| 284 | |
| 285 | intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high |
| 286 | |
| 287 | // start the Video Capture Intent |
| 288 | startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE); |
| 289 | } |
| 290 | </pre> |
| 291 | |
| 292 | <p>When the {@link |
| 293 | android.app.Activity#startActivityForResult(android.content.Intent, int) |
| 294 | startActivityForResult()} method is executed, users see a modified camera application interface. |
| 295 | After the user finishes taking a video (or cancels the operation), the user interface |
| 296 | returns to your application, and you must intercept the {@link |
| 297 | android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} |
| 298 | method to receive the result of the intent and continue your application execution. For information |
| 299 | on how to receive the completed intent, see the next section.</p> |
| 300 | |
| 301 | <h3 id="intent-receive">Receiving camera intent result</h3> |
| 302 | <p>Once you have constructed and executed an image or video camera intent, your application must be |
| 303 | configured to receive the result of the intent. This section shows you how to intercept the callback |
| 304 | from a camera intent so your application can do further processing of the captured image or |
| 305 | video.</p> |
| 306 | |
| 307 | <p>In order to receive the result of an intent, you must override the {@link |
| 308 | android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} in the |
| 309 | activity that started the intent. The following example demonstrates how to override {@link |
| 310 | android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} to |
| 311 | capture the result of the <a href="#intent-image">image camera intent</a> or <a |
| 312 | href="#intent-video">video camera intent</a> examples shown in the previous sections.</p> |
| 313 | |
| 314 | <pre> |
| 315 | private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; |
| 316 | private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200; |
| 317 | |
| 318 | @Override |
| 319 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 320 | if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { |
| 321 | if (resultCode == RESULT_OK) { |
| 322 | // Image captured and saved to fileUri specified in the Intent |
| 323 | Toast.makeText(this, "Image saved to:\n" + |
| 324 | data.getData(), Toast.LENGTH_LONG).show(); |
| 325 | } else if (resultCode == RESULT_CANCELED) { |
| 326 | // User cancelled the image capture |
| 327 | } else { |
| 328 | // Image capture failed, advise user |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) { |
| 333 | if (resultCode == RESULT_OK) { |
| 334 | // Video captured and saved to fileUri specified in the Intent |
| 335 | Toast.makeText(this, "Video saved to:\n" + |
| 336 | data.getData(), Toast.LENGTH_LONG).show(); |
| 337 | } else if (resultCode == RESULT_CANCELED) { |
| 338 | // User cancelled the video capture |
| 339 | } else { |
| 340 | // Video capture failed, advise user |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | </pre> |
| 345 | |
| 346 | <p>Once your activity receives a successful result, the captured image or video is available in the |
| 347 | specified location for your application to access.</p> |
| 348 | |
| 349 | |
| 350 | |
| 351 | <h2 id="custom-camera">Building a Camera App</h2> |
| 352 | <p>Some developers may require a camera user interface that is customized to the look of their |
| 353 | application or provides special features. Creating a customized camera activity requires more |
| 354 | code than <a href="#intents">using an intent</a>, but it can provide a more compelling experience |
| 355 | for your users.</p> |
| 356 | |
| 357 | <p>The general steps for creating a custom camera interface for your application are as follows:</p> |
| 358 | |
| 359 | <ul> |
| 360 | <li><strong>Detect and Access Camera</strong> - Create code to check for the existence of |
| 361 | cameras and request access.</li> |
| 362 | <li><strong>Create a Preview Class</strong> - Create a camera preview class that extends {@link |
| 363 | android.view.SurfaceView} and implements the {@link android.view.SurfaceHolder} interface. This |
| 364 | class previews the live images from the camera.</li> |
| 365 | <li><strong>Build a Preview Layout</strong> - Once you have the camera preview class, create a |
| 366 | view layout that incorporates the preview and the user interface controls you want.</li> |
| 367 | <li><strong>Setup Listeners for Capture</strong> - Connect listeners for your interface |
| 368 | controls to start image or video capture in response to user actions, such as pressing a |
| 369 | button.</li> |
| 370 | <li><strong>Capture and Save Files</strong> - Setup the code for capturing pictures or |
| 371 | videos and saving the output.</li> |
| 372 | <li><strong>Release the Camera</strong> - After using the camera, your application must |
| 373 | properly release it for use by other applications.</li> |
| 374 | </ul> |
| 375 | |
| 376 | <p>Camera hardware is a shared resource that must be carefully managed so your application does |
| 377 | not collide with other applications that may also want to use it. The following sections discusses |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 378 | how to detect camera hardware, how to request access to a camera, how to capture pictures or video |
| 379 | and how to release the camera when your application is done using it.</p> |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 380 | |
| 381 | <p class="caution"><strong>Caution:</strong> Remember to release the {@link android.hardware.Camera} |
| 382 | object by calling the {@link android.hardware.Camera#release() Camera.release()} when your |
| 383 | application is done using it! If your application does not properly release the camera, all |
| 384 | subsequent attempts to access the camera, including those by your own application, will fail and may |
| 385 | cause your or other applications to be shut down.</p> |
| 386 | |
| 387 | |
| 388 | <h3 id="detect-camera">Detecting camera hardware</h3> |
| 389 | <p>If your application does not specifically require a camera using a manifest declaration, you |
| 390 | should check to see if a camera is available at runtime. To perform this check, use the {@link |
| 391 | android.content.pm.PackageManager#hasSystemFeature(java.lang.String) |
| 392 | PackageManager.hasSystemFeature()} method, as shown in the example code below:</p> |
| 393 | |
| 394 | <pre> |
| 395 | /** Check if this device has a camera */ |
| 396 | private boolean checkCameraHardware(Context context) { |
| 397 | if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ |
| 398 | // this device has a camera |
| 399 | return true; |
| 400 | } else { |
| 401 | // no camera on this device |
| 402 | return false; |
| 403 | } |
| 404 | } |
| 405 | </pre> |
| 406 | |
| 407 | <p>Android devices can have multiple cameras, for example a back-facing camera for photography and a |
| 408 | front-facing camera for video calls. Android 2.3 (API Level 9) and later allows you to check the |
| 409 | number of cameras available on a device using the {@link |
| 410 | android.hardware.Camera#getNumberOfCameras() Camera.getNumberOfCameras()} method.</p> |
| 411 | |
| 412 | <h3 id="access-camera">Accessing cameras</h3> |
| 413 | <p>If you have determined that the device on which your application is running has a camera, you |
| 414 | must request to access it by getting an instance of {@link android.hardware.Camera} (unless you |
| 415 | are using an <a href="#intents">intent to access the camera</a>). </p> |
| 416 | |
| 417 | <p>To access the primary camera, use the {@link android.hardware.Camera#open() Camera.open()} method |
| 418 | and be sure to catch any exceptions, as shown in the code below:</p> |
| 419 | |
| 420 | <pre> |
| 421 | /** A safe way to get an instance of the Camera object. */ |
| 422 | public static Camera getCameraInstance(){ |
| 423 | Camera c = null; |
| 424 | try { |
| 425 | c = Camera.open(); // attempt to get a Camera instance |
| 426 | } |
| 427 | catch (Exception e){ |
| 428 | // Camera is not available (in use or does not exist) |
| 429 | } |
| 430 | return c; // returns null if camera is unavailable |
| 431 | } |
| 432 | </pre> |
| 433 | |
| 434 | <p class="caution"><strong>Caution:</strong> Always check for exceptions when using {@link |
| 435 | android.hardware.Camera#open() Camera.open()}. Failing to check for exceptions if the camera is in |
| 436 | use or does not exist will cause your application to be shut down by the system.</p> |
| 437 | |
| 438 | <p>On devices running Android 2.3 (API Level 9) or higher, you can access specific cameras using |
| 439 | {@link android.hardware.Camera#open(int) Camera.open(int)}. The example code above will access |
| 440 | the first, back-facing camera on a device with more than one camera.</p> |
| 441 | |
| 442 | <h3 id="check-camera-features">Checking camera features</h3> |
Pin Ting | 6f5b5ee | 2012-03-03 00:13:32 +0800 | [diff] [blame] | 443 | <p>Once you obtain access to a camera, you can get further information about its capabilities using |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 444 | the {@link android.hardware.Camera#getParameters() Camera.getParameters()} method and checking the |
| 445 | returned {@link android.hardware.Camera.Parameters} object for supported capabilities. When using |
| 446 | API Level 9 or higher, use the {@link android.hardware.Camera#getCameraInfo(int, |
| 447 | android.hardware.Camera.CameraInfo) Camera.getCameraInfo()} to determine if a camera is on the front |
| 448 | or back of the device, and the orientation of the image.</p> |
| 449 | |
| 450 | |
| 451 | |
| 452 | <h3 id="camera-preview">Creating a preview class</h3> |
| 453 | <p>For users to effectively take pictures or video, they must be able to see what the device camera |
| 454 | sees. A camera preview class is a {@link android.view.SurfaceView} that can display the live image |
| 455 | data coming from a camera, so users can frame and capture a picture or video.</p> |
| 456 | |
| 457 | <p>The following example code demonstrates how to create a basic camera preview class that can be |
| 458 | included in a {@link android.view.View} layout. This class implements {@link |
| 459 | android.view.SurfaceHolder.Callback SurfaceHolder.Callback} in order to capture the callback events |
| 460 | for creating and destroying the view, which are needed for assigning the camera preview input.</p> |
| 461 | |
| 462 | <pre> |
| 463 | /** A basic Camera preview class */ |
| 464 | public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { |
| 465 | private SurfaceHolder mHolder; |
| 466 | private Camera mCamera; |
| 467 | |
| 468 | public CameraPreview(Context context, Camera camera) { |
| 469 | super(context); |
| 470 | mCamera = camera; |
| 471 | |
| 472 | // Install a SurfaceHolder.Callback so we get notified when the |
| 473 | // underlying surface is created and destroyed. |
| 474 | mHolder = getHolder(); |
| 475 | mHolder.addCallback(this); |
| 476 | // deprecated setting, but required on Android versions prior to 3.0 |
| 477 | mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |
| 478 | } |
| 479 | |
| 480 | public void surfaceCreated(SurfaceHolder holder) { |
| 481 | // The Surface has been created, now tell the camera where to draw the preview. |
| 482 | try { |
| 483 | mCamera.setPreviewDisplay(holder); |
| 484 | mCamera.startPreview(); |
| 485 | } catch (IOException e) { |
| 486 | Log.d(TAG, "Error setting camera preview: " + e.getMessage()); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | public void surfaceDestroyed(SurfaceHolder holder) { |
| 491 | // empty. Take care of releasing the Camera preview in your activity. |
| 492 | } |
| 493 | |
| 494 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
| 495 | // If your preview can change or rotate, take care of those events here. |
| 496 | // Make sure to stop the preview before resizing or reformatting it. |
| 497 | |
| 498 | if (mHolder.getSurface() == null){ |
| 499 | // preview surface does not exist |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | // stop preview before making changes |
| 504 | try { |
| 505 | mCamera.stopPreview(); |
| 506 | } catch (Exception e){ |
| 507 | // ignore: tried to stop a non-existent preview |
| 508 | } |
| 509 | |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 510 | // set preview size and make any resize, rotate or |
| 511 | // reformatting changes here |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 512 | |
| 513 | // start preview with new settings |
| 514 | try { |
| 515 | mCamera.setPreviewDisplay(mHolder); |
| 516 | mCamera.startPreview(); |
| 517 | |
| 518 | } catch (Exception e){ |
| 519 | Log.d(TAG, "Error starting camera preview: " + e.getMessage()); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | </pre> |
| 524 | |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 525 | <p>If you want to set a specific size for your camera preview, set this in the {@code |
| 526 | surfaceChanged()} method as noted in the comments above. When setting preview size, you |
| 527 | <em>must use</em> values from {@link android.hardware.Camera.Parameters#getSupportedPreviewSizes}. |
| 528 | <em>Do not</em> set arbitrary values in the {@link |
| 529 | android.hardware.Camera.Parameters#setPreviewSize setPreviewSize()} method.</p> |
| 530 | |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 531 | |
| 532 | <h3 id="preview-layout">Placing preview in a layout</h3> |
| 533 | <p>A camera preview class, such as the example shown in the previous section, must be placed in the |
| 534 | layout of an activity along with other user interface controls for taking a picture or video. This |
| 535 | section shows you how to build a basic layout and activity for the preview.</p> |
| 536 | |
| 537 | <p>The following layout code provides a very basic view that can be used to display a camera |
| 538 | preview. In this example, the {@link android.widget.FrameLayout} element is meant to be the |
| 539 | container for the camera preview class. This layout type is used so that additional picture |
| 540 | information or controls can be overlayed on the live camera preview images.</p> |
| 541 | |
| 542 | <pre> |
| 543 | <?xml version="1.0" encoding="utf-8"?> |
| 544 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| 545 | android:orientation="horizontal" |
| 546 | android:layout_width="fill_parent" |
| 547 | android:layout_height="fill_parent" |
| 548 | > |
| 549 | <FrameLayout |
| 550 | android:id="@+id/camera_preview" |
| 551 | android:layout_width="fill_parent" |
| 552 | android:layout_height="fill_parent" |
| 553 | android:layout_weight="1" |
| 554 | /> |
| 555 | |
| 556 | <Button |
| 557 | android:id="@+id/button_capture" |
| 558 | android:text="Capture" |
| 559 | android:layout_width="wrap_content" |
| 560 | android:layout_height="wrap_content" |
| 561 | android:layout_gravity="center" |
| 562 | /> |
| 563 | </LinearLayout> |
| 564 | </pre> |
| 565 | |
| 566 | <p>On most devices, the default orientation of the camera preview is landscape. This example layout |
| 567 | specifies a horizontal (landscape) layout and the code below fixes the orientation of the |
| 568 | application to landscape. For simplicity in rendering a camera preview, you should change your |
| 569 | application's preview activity orientation to landscape by adding the following to your |
| 570 | manifest.</p> |
| 571 | |
| 572 | <pre> |
| 573 | <activity android:name=".CameraActivity" |
| 574 | android:label="@string/app_name" |
| 575 | |
| 576 | android:screenOrientation="landscape"> |
| 577 | <!-- configure this activity to use landscape orientation --> |
| 578 | |
| 579 | <intent-filter> |
| 580 | <action android:name="android.intent.action.MAIN" /> |
| 581 | <category android:name="android.intent.category.LAUNCHER" /> |
| 582 | </intent-filter> |
| 583 | </activity> |
| 584 | </pre> |
| 585 | |
| 586 | <p class="note"><strong>Note:</strong> A camera preview does not have to be in landscape mode. |
| 587 | Starting in Android 2.2 (API Level 8), you can use the {@link |
| 588 | android.hardware.Camera#setDisplayOrientation(int) setDisplayOrientation()} method to set the |
| 589 | rotation of the preview image. In order to change preview orientation as the user re-orients the |
| 590 | phone, within the {@link |
| 591 | android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int) |
| 592 | surfaceChanged()} method of your preview class, first stop the preview with {@link |
| 593 | android.hardware.Camera#stopPreview() Camera.stopPreview()} change the orientation and then |
| 594 | start the preview again with {@link android.hardware.Camera#startPreview() |
| 595 | Camera.startPreview()}.</p> |
| 596 | |
| 597 | <p>In the activity for your camera view, add your preview class to the {@link |
| 598 | android.widget.FrameLayout} element shown in the example above. Your camera activity must also |
| 599 | ensure that it releases the camera when it is paused or shut down. The following example shows how |
| 600 | to modify a camera activity to attach the preview class shown in <a href="#camera-preview">Creating |
| 601 | a preview class</a>.</p> |
| 602 | |
| 603 | <pre> |
| 604 | public class CameraActivity extends Activity { |
| 605 | |
| 606 | private Camera mCamera; |
| 607 | private CameraPreview mPreview; |
| 608 | |
| 609 | @Override |
| 610 | public void onCreate(Bundle savedInstanceState) { |
| 611 | super.onCreate(savedInstanceState); |
| 612 | setContentView(R.layout.main); |
| 613 | |
| 614 | // Create an instance of Camera |
| 615 | mCamera = getCameraInstance(); |
| 616 | |
| 617 | // Create our Preview view and set it as the content of our activity. |
| 618 | mPreview = new CameraPreview(this, mCamera); |
Scott Main | 183bf11 | 2012-08-13 19:12:13 -0700 | [diff] [blame] | 619 | FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 620 | preview.addView(mPreview); |
| 621 | } |
| 622 | } |
| 623 | </pre> |
| 624 | |
| 625 | <p class="note"><strong>Note:</strong> The {@code getCameraInstance()} method in the example above |
| 626 | refers to the example method shown in <a href="#access-camera">Accessing cameras</a>.</p> |
| 627 | |
| 628 | |
| 629 | <h3 id="capture-picture">Capturing pictures</h3> |
| 630 | <p>Once you have built a preview class and a view layout in which to display it, you are ready to |
| 631 | start capturing images with your application. In your application code, you must set up listeners |
| 632 | for your user interface controls to respond to a user action by taking a picture.</p> |
| 633 | |
| 634 | <p>In order to retrieve a picture, use the {@link |
| 635 | android.hardware.Camera#takePicture(android.hardware.Camera.ShutterCallback, |
| 636 | android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback) |
| 637 | Camera.takePicture()} method. This method takes three parameters which receive data from the camera. |
| 638 | In order to receive data in a JPEG format, you must implement an {@link |
| 639 | android.hardware.Camera.PictureCallback} interface to receive the image data and |
| 640 | write it to a file. The following code shows a basic implementation of the {@link |
| 641 | android.hardware.Camera.PictureCallback} interface to save an image received from the camera.</p> |
| 642 | |
| 643 | <pre> |
| 644 | private PictureCallback mPicture = new PictureCallback() { |
| 645 | |
| 646 | @Override |
| 647 | public void onPictureTaken(byte[] data, Camera camera) { |
| 648 | |
| 649 | File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); |
| 650 | if (pictureFile == null){ |
| 651 | Log.d(TAG, "Error creating media file, check storage permissions: " + |
| 652 | e.getMessage()); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | try { |
| 657 | FileOutputStream fos = new FileOutputStream(pictureFile); |
| 658 | fos.write(data); |
| 659 | fos.close(); |
| 660 | } catch (FileNotFoundException e) { |
| 661 | Log.d(TAG, "File not found: " + e.getMessage()); |
| 662 | } catch (IOException e) { |
| 663 | Log.d(TAG, "Error accessing file: " + e.getMessage()); |
| 664 | } |
| 665 | } |
| 666 | }; |
| 667 | </pre> |
| 668 | |
| 669 | <p>Trigger capturing an image by calling the {@link |
| 670 | android.hardware.Camera#takePicture(android.hardware.Camera.ShutterCallback, |
| 671 | android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback) |
| 672 | Camera.takePicture()} method. The following example code shows how to call this method from a |
| 673 | button {@link android.view.View.OnClickListener}.</p> |
| 674 | |
| 675 | <pre> |
| 676 | // Add a listener to the Capture button |
| 677 | Button captureButton = (Button) findViewById(id.button_capture); |
Pin Ting | 6f5b5ee | 2012-03-03 00:13:32 +0800 | [diff] [blame] | 678 | captureButton.setOnClickListener( |
| 679 | new View.OnClickListener() { |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 680 | @Override |
| 681 | public void onClick(View v) { |
| 682 | // get an image from the camera |
| 683 | mCamera.takePicture(null, null, mPicture); |
| 684 | } |
| 685 | } |
| 686 | ); |
| 687 | </pre> |
| 688 | |
| 689 | <p class="note"><strong>Note:</strong> The {@code mPicture} member in the following example refers |
| 690 | to the example code above.</p> |
| 691 | |
| 692 | <p class="caution"><strong>Caution:</strong> Remember to release the {@link android.hardware.Camera} |
| 693 | object by calling the {@link android.hardware.Camera#release() Camera.release()} when your |
| 694 | application is done using it! For information about how to release the camera, see <a |
| 695 | href="#release-camera">Releasing the camera</a>.</p> |
| 696 | |
| 697 | |
| 698 | <h3 id="capture-video">Capturing videos</h3> |
| 699 | |
| 700 | <p>Video capture using the Android framework requires careful management of the {@link |
| 701 | android.hardware.Camera} object and coordination with the {@link android.media.MediaRecorder} |
| 702 | class. When recording video with {@link android.hardware.Camera}, you must manage the {@link |
| 703 | android.hardware.Camera#lock() Camera.lock()} and {@link android.hardware.Camera#unlock() |
| 704 | Camera.unlock()} calls to allow {@link android.media.MediaRecorder} access to the camera hardware, |
| 705 | in addition to the {@link android.hardware.Camera#open() Camera.open()} and {@link |
| 706 | android.hardware.Camera#release() Camera.release()} calls.</p> |
| 707 | |
| 708 | <p class="note"><strong>Note:</strong> Starting with Android 4.0 (API level 14), the {@link |
| 709 | android.hardware.Camera#lock() Camera.lock()} and {@link android.hardware.Camera#unlock() |
| 710 | Camera.unlock()} calls are managed for you automatically.</p> |
| 711 | |
| 712 | <p>Unlike taking pictures with a device camera, capturing video requires a very particular call |
| 713 | order. You must follow a specific order of execution to successfully prepare for and capture video |
| 714 | with your application, as detailed below.</p> |
| 715 | |
| 716 | <ol> |
| 717 | <li><strong>Open Camera</strong> - Use the {@link android.hardware.Camera#open() Camera.open()} |
| 718 | to get an instance of the camera object.</li> |
| 719 | <li><strong>Connect Preview</strong> - Prepare a live camera image preview by connecting a {@link |
| 720 | android.view.SurfaceView} to the camera using {@link |
| 721 | android.hardware.Camera#setPreviewDisplay(android.view.SurfaceHolder) Camera.setPreviewDisplay()}. |
| 722 | </li> |
| 723 | <li><strong>Start Preview</strong> - Call {@link android.hardware.Camera#startPreview() |
| 724 | Camera.startPreview()} to begin displaying the live camera images.</li> |
| 725 | <li><strong>Start Recording Video</strong> - The following steps must be completed <em>in |
| 726 | order</em> to successfully record video: |
| 727 | <ol style="list-style-type: lower-alpha;"> |
| 728 | <li><strong>Unlock the Camera</strong> - Unlock the camera for use by {@link |
| 729 | android.media.MediaRecorder} by calling {@link android.hardware.Camera#unlock() |
| 730 | Camera.unlock()}.</li> |
| 731 | <li><strong>Configure MediaRecorder</strong> - Call in the following {@link |
| 732 | android.media.MediaRecorder} methods <em>in this order</em>. For more information, see the {@link |
| 733 | android.media.MediaRecorder} reference documentation. |
| 734 | <ol> |
| 735 | <li>{@link android.media.MediaRecorder#setCamera(android.hardware.Camera) |
| 736 | setCamera()} - Set the camera to be used for video capture, use your application's current instance |
| 737 | of {@link android.hardware.Camera}.</li> |
| 738 | <li>{@link android.media.MediaRecorder#setAudioSource(int) setAudioSource()} - Set the |
| 739 | audio source, use {@link android.media.MediaRecorder.AudioSource#CAMCORDER |
| 740 | MediaRecorder.AudioSource.CAMCORDER}. </li> |
| 741 | <li>{@link android.media.MediaRecorder#setVideoSource(int) setVideoSource()} - Set |
| 742 | the video source, use {@link android.media.MediaRecorder.VideoSource#CAMERA |
| 743 | MediaRecorder.VideoSource.CAMERA}.</li> |
| 744 | <li>Set the video output format and encoding. For Android 2.2 (API Level 8) and |
| 745 | higher, use the {@link android.media.MediaRecorder#setProfile(android.media.CamcorderProfile) |
| 746 | MediaRecorder.setProfile} method, and get a profile instance using {@link |
| 747 | android.media.CamcorderProfile#get(int) CamcorderProfile.get()}. For versions of Android prior to |
| 748 | 2.2, you must set the video output format and encoding parameters: |
| 749 | <ol style="list-style-type: lower-roman;"> |
| 750 | <li>{@link android.media.MediaRecorder#setOutputFormat(int) setOutputFormat()} - Set |
| 751 | the output format, specify the default setting or {@link |
| 752 | android.media.MediaRecorder.OutputFormat#MPEG_4 MediaRecorder.OutputFormat.MPEG_4}.</li> |
| 753 | <li>{@link android.media.MediaRecorder#setAudioEncoder(int) setAudioEncoder()} - Set |
| 754 | the sound encoding type, specify the default setting or {@link |
| 755 | android.media.MediaRecorder.AudioEncoder#AMR_NB MediaRecorder.AudioEncoder.AMR_NB}.</li> |
| 756 | <li>{@link android.media.MediaRecorder#setVideoEncoder(int) setVideoEncoder()} - Set |
| 757 | the video encoding type, specify the default setting or {@link |
| 758 | android.media.MediaRecorder.VideoEncoder#MPEG_4_SP MediaRecorder.VideoEncoder.MPEG_4_SP}.</li> |
| 759 | </ol> |
| 760 | </li> |
| 761 | <li>{@link android.media.MediaRecorder#setOutputFile(java.lang.String) setOutputFile()} - |
| 762 | Set the output file, use {@code getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()} from the example |
| 763 | method in the <a href="#saving-media">Saving Media Files</a> section.</li> |
| 764 | <li>{@link android.media.MediaRecorder#setPreviewDisplay(android.view.Surface) |
| 765 | setPreviewDisplay()} - Specify the {@link android.view.SurfaceView} preview layout element for |
| 766 | your application. Use the same object you specified for <strong>Connect Preview</strong>.</li> |
| 767 | </ol> |
| 768 | <p class="caution"><strong>Caution:</strong> You must call these {@link |
| 769 | android.media.MediaRecorder} configuration methods <em>in this order</em>, otherwise your |
| 770 | application will encounter errors and the recording will fail.</p> |
| 771 | </li> |
| 772 | <li><strong>Prepare MediaRecorder</strong> - Prepare the {@link android.media.MediaRecorder} |
| 773 | with provided configuration settings by calling {@link android.media.MediaRecorder#prepare() |
| 774 | MediaRecorder.prepare()}.</li> |
| 775 | <li><strong>Start MediaRecorder</strong> - Start recording video by calling {@link |
| 776 | android.media.MediaRecorder#start() MediaRecorder.start()}.</li> |
| 777 | </ol> |
| 778 | </li> |
| 779 | <li><strong>Stop Recording Video</strong> - Call the following methods <em>in order</em>, to |
| 780 | successfully complete a video recording: |
| 781 | <ol style="list-style-type: lower-alpha;"> |
| 782 | <li><strong>Stop MediaRecorder</strong> - Stop recording video by calling {@link |
| 783 | android.media.MediaRecorder#stop() MediaRecorder.stop()}.</li> |
| 784 | <li><strong>Reset MediaRecorder</strong> - Optionally, remove the configuration settings from |
| 785 | the recorder by calling {@link android.media.MediaRecorder#reset() MediaRecorder.reset()}.</li> |
| 786 | <li><strong>Release MediaRecorder</strong> - Release the {@link android.media.MediaRecorder} |
| 787 | by calling {@link android.media.MediaRecorder#release() MediaRecorder.release()}.</li> |
| 788 | <li><strong>Lock the Camera</strong> - Lock the camera so that future {@link |
| 789 | android.media.MediaRecorder} sessions can use it by calling {@link android.hardware.Camera#lock() |
| 790 | Camera.lock()}. Starting with Android 4.0 (API level 14), this call is not required unless the |
| 791 | {@link android.media.MediaRecorder#prepare() MediaRecorder.prepare()} call fails.</li> |
| 792 | </ol> |
| 793 | </li> |
| 794 | <li><strong>Stop the Preview</strong> - When your activity has finished using the camera, stop the |
| 795 | preview using {@link android.hardware.Camera#stopPreview() Camera.stopPreview()}.</li> |
| 796 | <li><strong>Release Camera</strong> - Release the camera so that other applications can use |
| 797 | it by calling {@link android.hardware.Camera#release() Camera.release()}.</li> |
| 798 | </ol> |
| 799 | |
| 800 | <p class="note"><strong>Note:</strong> It is possible to use {@link android.media.MediaRecorder} |
| 801 | without creating a camera preview first and skip the first few steps of this process. However, |
| 802 | since users typically prefer to see a preview before starting a recording, that process is not |
| 803 | discussed here.</p> |
| 804 | |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 805 | <p class="note"><strong>Tip:</strong> If your application is typically used for recording video, set |
| 806 | {@link android.hardware.Camera.Parameters#setRecordingHint} to {@code true} prior to starting your |
| 807 | preview. This setting can help reduce the time it takes to start recording.</p> |
| 808 | |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 809 | <h4 id="configuring-mediarecorder">Configuring MediaRecorder</h4> |
| 810 | <p>When using the {@link android.media.MediaRecorder} class to record video, you must perform |
| 811 | configuration steps in a <em>specific order</em> and then call the {@link |
| 812 | android.media.MediaRecorder#prepare() MediaRecorder.prepare()} method to check and implement the |
| 813 | configuration. The following example code demonstrates how to properly configure and prepare the |
| 814 | {@link android.media.MediaRecorder} class for video recording.</p> |
| 815 | |
| 816 | <pre> |
| 817 | private boolean prepareVideoRecorder(){ |
| 818 | |
| 819 | mCamera = getCameraInstance(); |
| 820 | mMediaRecorder = new MediaRecorder(); |
| 821 | |
| 822 | // Step 1: Unlock and set camera to MediaRecorder |
| 823 | mCamera.unlock(); |
| 824 | mMediaRecorder.setCamera(mCamera); |
| 825 | |
| 826 | // Step 2: Set sources |
| 827 | mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); |
| 828 | mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); |
| 829 | |
| 830 | // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) |
| 831 | mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); |
| 832 | |
| 833 | // Step 4: Set output file |
| 834 | mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); |
| 835 | |
| 836 | // Step 5: Set the preview output |
| 837 | mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); |
| 838 | |
| 839 | // Step 6: Prepare configured MediaRecorder |
| 840 | try { |
| 841 | mMediaRecorder.prepare(); |
| 842 | } catch (IllegalStateException e) { |
| 843 | Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage()); |
| 844 | releaseMediaRecorder(); |
| 845 | return false; |
| 846 | } catch (IOException e) { |
| 847 | Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage()); |
| 848 | releaseMediaRecorder(); |
| 849 | return false; |
| 850 | } |
| 851 | return true; |
| 852 | } |
| 853 | </pre> |
| 854 | |
| 855 | <p>Prior to Android 2.2 (API Level 8), you must set the output format and encoding formats |
| 856 | parameters directly, instead of using {@link android.media.CamcorderProfile}. This approach is |
| 857 | demonstrated in the following code:</p> |
| 858 | |
| 859 | <pre> |
| 860 | // Step 3: Set output format and encoding (for versions prior to API Level 8) |
| 861 | mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); |
| 862 | mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); |
| 863 | mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); |
| 864 | </pre> |
| 865 | |
| 866 | <p>The following video recording parameters for {@link android.media.MediaRecorder} are given |
| 867 | default settings, however, you may want to adjust these settings for your application:</p> |
| 868 | |
| 869 | <ul> |
| 870 | <li>{@link android.media.MediaRecorder#setVideoEncodingBitRate(int) |
| 871 | setVideoEncodingBitRate()}</li> |
| 872 | <li>{@link android.media.MediaRecorder#setVideoSize(int, int) setVideoSize()}</li> |
| 873 | <li>{@link android.media.MediaRecorder#setVideoFrameRate(int) setVideoFrameRate()}</li> |
| 874 | <li>{@link android.media.MediaRecorder#setAudioEncodingBitRate(int) |
| 875 | setAudioEncodingBitRate()}</li> <li>{@link android.media.MediaRecorder#setAudioChannels(int) |
| 876 | setAudioChannels()}</li> |
| 877 | <li>{@link android.media.MediaRecorder#setAudioSamplingRate(int) setAudioSamplingRate()}</li> |
| 878 | </ul> |
| 879 | |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 880 | <h4 id="start-stop-mediarecorder">Starting and stopping MediaRecorder</h4> |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 881 | <p>When starting and stopping video recording using the {@link android.media.MediaRecorder} class, |
| 882 | you must follow a specific order, as listed below.</p> |
| 883 | |
| 884 | <ol> |
| 885 | <li>Unlock the camera with {@link android.hardware.Camera#unlock() Camera.unlock()}</li> |
| 886 | <li>Configure {@link android.media.MediaRecorder} as shown in the code example above</li> |
| 887 | <li>Start recording using {@link android.media.MediaRecorder#start() |
| 888 | MediaRecorder.start()}</li> |
| 889 | <li>Record the video</li> |
| 890 | <li>Stop recording using {@link |
| 891 | android.media.MediaRecorder#stop() MediaRecorder.stop()}</li> |
| 892 | <li>Release the media recorder with {@link android.media.MediaRecorder#release() |
| 893 | MediaRecorder.release()}</li> |
| 894 | <li>Lock the camera using {@link android.hardware.Camera#lock() Camera.lock()}</li> |
| 895 | </ol> |
| 896 | |
| 897 | <p>The following example code demonstrates how to wire up a button to properly start and stop |
| 898 | video recording using the camera and the {@link android.media.MediaRecorder} class.</p> |
| 899 | |
| 900 | <p class="note"><strong>Note:</strong> When completing a video recording, do not release the camera |
| 901 | or else your preview will be stopped.</p> |
| 902 | |
| 903 | <pre> |
| 904 | private boolean isRecording = false; |
| 905 | |
| 906 | // Add a listener to the Capture button |
| 907 | Button captureButton = (Button) findViewById(id.button_capture); |
| 908 | captureButton.setOnClickListener( |
| 909 | new View.OnClickListener() { |
| 910 | @Override |
| 911 | public void onClick(View v) { |
| 912 | if (isRecording) { |
| 913 | // stop recording and release camera |
| 914 | mMediaRecorder.stop(); // stop the recording |
| 915 | releaseMediaRecorder(); // release the MediaRecorder object |
| 916 | mCamera.lock(); // take camera access back from MediaRecorder |
| 917 | |
| 918 | // inform the user that recording has stopped |
| 919 | setCaptureButtonText("Capture"); |
| 920 | isRecording = false; |
| 921 | } else { |
| 922 | // initialize video camera |
| 923 | if (prepareVideoRecorder()) { |
| 924 | // Camera is available and unlocked, MediaRecorder is prepared, |
| 925 | // now you can start recording |
| 926 | mMediaRecorder.start(); |
| 927 | |
| 928 | // inform the user that recording has started |
| 929 | setCaptureButtonText("Stop"); |
| 930 | isRecording = true; |
| 931 | } else { |
| 932 | // prepare didn't work, release the camera |
| 933 | releaseMediaRecorder(); |
| 934 | // inform user |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | ); |
| 940 | </pre> |
| 941 | |
| 942 | <p class="note"><strong>Note:</strong> In the above example, the {@code prepareVideoRecorder()} |
| 943 | method refers to the example code shown in <a |
| 944 | href="#configuring-mediarecorder">Configuring MediaRecorder</a>. This method takes care of locking |
| 945 | the camera, configuring and preparing the {@link android.media.MediaRecorder} instance.</p> |
| 946 | |
| 947 | |
| 948 | <h3 id="release-camera">Releasing the camera</h3> |
| 949 | <p>Cameras are a resource that is shared by applications on a device. Your application can make |
| 950 | use of the camera after getting an instance of {@link android.hardware.Camera}, and you must be |
| 951 | particularly careful to release the camera object when your application stops using it, and as |
| 952 | soon as your application is paused ({@link android.app.Activity#onPause() Activity.onPause()}). If |
| 953 | your application does not properly release the camera, all subsequent attempts to access the camera, |
| 954 | including those by your own application, will fail and may cause your or other applications to be |
| 955 | shut down.</p> |
| 956 | |
| 957 | <p>To release an instance of the {@link android.hardware.Camera} object, use the {@link |
| 958 | android.hardware.Camera#release() Camera.release()} method, as shown in the example code below.</p> |
| 959 | |
| 960 | <pre> |
| 961 | public class CameraActivity extends Activity { |
| 962 | private Camera mCamera; |
| 963 | private SurfaceView mPreview; |
| 964 | private MediaRecorder mMediaRecorder; |
| 965 | |
| 966 | ... |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 967 | |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 968 | @Override |
| 969 | protected void onPause() { |
| 970 | super.onPause(); |
| 971 | releaseMediaRecorder(); // if you are using MediaRecorder, release it first |
| 972 | releaseCamera(); // release the camera immediately on pause event |
| 973 | } |
| 974 | |
| 975 | private void releaseMediaRecorder(){ |
| 976 | if (mMediaRecorder != null) { |
| 977 | mMediaRecorder.reset(); // clear recorder configuration |
| 978 | mMediaRecorder.release(); // release the recorder object |
| 979 | mMediaRecorder = null; |
| 980 | mCamera.lock(); // lock camera for later use |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | private void releaseCamera(){ |
| 985 | if (mCamera != null){ |
| 986 | mCamera.release(); // release the camera for other applications |
| 987 | mCamera = null; |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | </pre> |
| 992 | |
| 993 | <p class="caution"><strong>Caution:</strong> If your application does not properly release the |
| 994 | camera, all subsequent attempts to access the camera, including those by your own application, will |
| 995 | fail and may cause your or other applications to be shut down.</p> |
| 996 | |
| 997 | |
| 998 | <h2 id="saving-media">Saving Media Files</h2> |
| 999 | <p>Media files created by users such as pictures and videos should be saved to a device's external |
| 1000 | storage directory (SD Card) to conserve system space and to allow users to access these files |
| 1001 | without their device. There are many possible directory locations to save media files on a device, |
| 1002 | however there are only two standard locations you should consider as a developer:</p> |
| 1003 | |
| 1004 | <ul> |
| 1005 | <li><strong>{@link android.os.Environment#getExternalStoragePublicDirectory(java.lang.String) |
| 1006 | Environment.getExternalStoragePublicDirectory}({@link android.os.Environment#DIRECTORY_PICTURES |
| 1007 | Environment.DIRECTORY_PICTURES})</strong> - This method returns the standard, shared and recommended |
| 1008 | location for saving pictures and videos. This directory is shared (public), so other applications |
| 1009 | can easily discover, read, change and delete files saved in this location. If your application is |
| 1010 | uninstalled by the user, media files saved to this location will not be removed. To avoid |
| 1011 | interfering with users existing pictures and videos, you should create a sub-directory for your |
| 1012 | application's media files within this directory, as shown in the code sample below. This method is |
| 1013 | available in Android 2.2 (API Level 8), for equivalent calls in earlier API versions, see <a |
| 1014 | href="{@docRoot}guide/topics/data/data-storage.html#SavingSharedFiles">Saving Shared Files</a>.</li> |
| 1015 | <li><strong>{@link android.content.Context#getExternalFilesDir(java.lang.String) |
| 1016 | Context.getExternalFilesDir}({@link android.os.Environment#DIRECTORY_PICTURES |
| 1017 | Environment.DIRECTORY_PICTURES})</strong> - This method returns a standard location for saving |
| 1018 | pictures and videos which are associated with your application. If your application is uninstalled, |
| 1019 | any files saved in this location are removed. Security is not enforced for files in this |
| 1020 | location and other applications may read, change and delete them.</li> |
| 1021 | </ul> |
| 1022 | |
| 1023 | <p>The following example code demonstrates how to create a {@link java.io.File} or {@link |
| 1024 | android.net.Uri} location for a media file that can be used when invoking a device's camera with |
| 1025 | an {@link android.content.Intent} or as part of a <a href="#custom-camera">Building a Camera |
| 1026 | App</a>.</p> |
| 1027 | |
| 1028 | <pre> |
| 1029 | public static final int MEDIA_TYPE_IMAGE = 1; |
| 1030 | public static final int MEDIA_TYPE_VIDEO = 2; |
| 1031 | |
| 1032 | /** Create a file Uri for saving an image or video */ |
| 1033 | private static Uri getOutputMediaFileUri(int type){ |
| 1034 | return Uri.fromFile(getOutputMediaFile(type)); |
| 1035 | } |
| 1036 | |
| 1037 | /** Create a File for saving an image or video */ |
Bill Gruber | 1d56f41 | 2011-12-19 17:32:47 -0800 | [diff] [blame] | 1038 | private static File getOutputMediaFile(int type){ |
Joe Fernandez | 99b70f3 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 1039 | // To be safe, you should check that the SDCard is mounted |
| 1040 | // using Environment.getExternalStorageState() before doing this. |
| 1041 | |
| 1042 | File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( |
| 1043 | Environment.DIRECTORY_PICTURES), "MyCameraApp"); |
| 1044 | // This location works best if you want the created images to be shared |
| 1045 | // between applications and persist after your app has been uninstalled. |
| 1046 | |
| 1047 | // Create the storage directory if it does not exist |
| 1048 | if (! mediaStorageDir.exists()){ |
| 1049 | if (! mediaStorageDir.mkdirs()){ |
| 1050 | Log.d("MyCameraApp", "failed to create directory"); |
| 1051 | return null; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | // Create a media file name |
| 1056 | String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); |
| 1057 | File mediaFile; |
| 1058 | if (type == MEDIA_TYPE_IMAGE){ |
| 1059 | mediaFile = new File(mediaStorageDir.getPath() + File.separator + |
| 1060 | "IMG_"+ timeStamp + ".jpg"); |
| 1061 | } else if(type == MEDIA_TYPE_VIDEO) { |
| 1062 | mediaFile = new File(mediaStorageDir.getPath() + File.separator + |
| 1063 | "VID_"+ timeStamp + ".mp4"); |
| 1064 | } else { |
| 1065 | return null; |
| 1066 | } |
| 1067 | |
| 1068 | return mediaFile; |
| 1069 | } |
| 1070 | </pre> |
| 1071 | |
| 1072 | <p class="note"><strong>Note:</strong> {@link |
| 1073 | android.os.Environment#getExternalStoragePublicDirectory(java.lang.String) |
| 1074 | Environment.getExternalStoragePublicDirectory()} is available in Android 2.2 (API Level 8) or |
| 1075 | higher. If you are targeting devices with earlier versions of Android, use {@link |
| 1076 | android.os.Environment#getExternalStorageDirectory() Environment.getExternalStorageDirectory()} |
| 1077 | instead. For more information, see <a |
| 1078 | href="{@docRoot}guide/topics/data/data-storage.html#SavingSharedFiles">Saving Shared Files</a>.</p> |
| 1079 | |
| 1080 | <p>For more information about saving files on an Android device, see <a |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 1081 | href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a>.</p> |
| 1082 | |
| 1083 | |
| 1084 | <h2 id="camera-features">Camera Features</h2> |
| 1085 | <p>Android supports a wide array of camera features you can control with your camera application, |
| 1086 | such as picture format, flash mode, focus settings, and many more. This section lists the common |
| 1087 | camera features, and briefly discusses how to use them. Most camera features can be accessed and set |
| 1088 | using the through {@link android.hardware.Camera.Parameters} object. However, there are several |
| 1089 | important features that require more than simple settings in {@link |
| 1090 | android.hardware.Camera.Parameters}. These features are covered in the following sections:<p> |
| 1091 | |
| 1092 | <ul> |
| 1093 | <li><a href="#metering-focus-areas">Metering and focus areas</a></li> |
| 1094 | <li><a href="#face-detection">Face detection</a></li> |
| 1095 | <li><a href="#time-lapse-video">Time lapse video</a></li> |
| 1096 | </ul> |
| 1097 | |
| 1098 | <p>For general information about how to use features that are controlled through {@link |
| 1099 | android.hardware.Camera.Parameters}, review the <a href="#using-features">Using camera |
| 1100 | features</a> section. For more detailed information about how to use features controlled through the |
| 1101 | camera parameters object, follow the links in the feature list below to the API reference |
| 1102 | documentation.</p> |
| 1103 | |
| 1104 | <p class="table-caption" id="table1"> |
| 1105 | <strong>Table 1.</strong> Common camera features sorted by the Android API Level in which they |
| 1106 | were introduced.</p> |
| 1107 | <table> |
| 1108 | <tr> |
| 1109 | <th>Feature</th> <th>API Level</th> <th>Description</th> |
| 1110 | </tr> |
| 1111 | <tr> |
| 1112 | <td><a href="#face-detection">Face Detection</a></td> |
| 1113 | <td>14</td> |
| 1114 | <td>Identify human faces within a picture and use them for focus, metering and white |
| 1115 | balance</td> |
| 1116 | </tr> |
| 1117 | <tr> |
| 1118 | <td><a href="#metering-focus-areas">Metering Areas</a></td> |
| 1119 | <td>14</td> |
| 1120 | <td>Specify one or more areas within an image for calculating white balance</td> |
| 1121 | </tr> |
| 1122 | <tr> |
| 1123 | <td><a href="#metering-focus-areas">Focus Areas</a></td> |
| 1124 | <td>14</td> |
| 1125 | <td>Set one or more areas within an image to use for focus</td> |
| 1126 | </tr> |
| 1127 | <tr> |
| 1128 | <td>{@link android.hardware.Camera.Parameters#setAutoWhiteBalanceLock White Balance Lock}</td> |
| 1129 | <td>14</td> |
| 1130 | <td>Stop or start automatic white balance adjustments</td> |
| 1131 | </tr> |
| 1132 | <tr> |
| 1133 | <td>{@link android.hardware.Camera.Parameters#setAutoExposureLock Exposure Lock}</td> |
| 1134 | <td>14</td> |
| 1135 | <td>Stop or start automatic exposure adjustments</td> |
| 1136 | </tr> |
| 1137 | <tr> |
| 1138 | <td>{@link android.hardware.Camera#takePicture Video Snapshot}</td> |
| 1139 | <td>14</td> |
| 1140 | <td>Take a picture while shooting video (frame grab)</td> |
| 1141 | </tr> |
| 1142 | <tr> |
| 1143 | <td><a href="#time-lapse-video">Time Lapse Video</a></td> |
| 1144 | <td>11</td> |
| 1145 | <td>Record frames with set delays to record a time lapse video</td> |
| 1146 | </tr> |
| 1147 | <tr> |
| 1148 | <td>{@link android.hardware.Camera#open(int) Multiple Cameras}</td> |
| 1149 | <td>9</td> |
| 1150 | <td>Support for more than one camera on a device, including front-facing and back-facing |
| 1151 | cameras</td> |
| 1152 | </tr> |
| 1153 | <tr> |
| 1154 | <td>{@link android.hardware.Camera.Parameters#getFocusDistances Focus Distance}</td> |
| 1155 | <td>9</td> |
| 1156 | <td>Reports distances between the camera and objects that appear to be in focus</td> |
| 1157 | </tr> |
| 1158 | <tr> |
| 1159 | <td>{@link android.hardware.Camera.Parameters#setZoom Zoom}</td> |
| 1160 | <td>8</td> |
| 1161 | <td>Set image magnification</td> |
| 1162 | </tr> |
| 1163 | <tr> |
| 1164 | <td>{@link android.hardware.Camera.Parameters#setExposureCompensation Exposure |
| 1165 | Compensation}</td> |
| 1166 | <td>8</td> |
| 1167 | <td>Increase or decrease the light exposure level</td> |
| 1168 | </tr> |
| 1169 | <tr> |
| 1170 | <td>{@link android.hardware.Camera.Parameters#setGpsLatitude GPS Data}</td> |
| 1171 | <td>5</td> |
| 1172 | <td>Include or omit geographic location data with the image</td> |
| 1173 | </tr> |
| 1174 | <tr> |
| 1175 | <td>{@link android.hardware.Camera.Parameters#setWhiteBalance White Balance}</td> |
| 1176 | <td>5</td> |
| 1177 | <td>Set the white balance mode, which affects color values in the captured image</td> |
| 1178 | </tr> |
| 1179 | <tr> |
| 1180 | <td>{@link android.hardware.Camera.Parameters#setFocusMode Focus Mode}</td> |
| 1181 | <td>5</td> |
| 1182 | <td>Set how the camera focuses on a subject such as automatic, fixed, macro or infinity</td> |
| 1183 | </tr> |
| 1184 | <tr> |
| 1185 | <td>{@link android.hardware.Camera.Parameters#setSceneMode Scene Mode}</td> |
| 1186 | <td>5</td> |
| 1187 | <td>Apply a preset mode for specific types of photography situations such as night, beach, snow |
| 1188 | or candlelight scenes</td> |
| 1189 | </tr> |
| 1190 | <tr> |
| 1191 | <td>{@link android.hardware.Camera.Parameters#setJpegQuality JPEG Quality}</td> |
| 1192 | <td>5</td> |
| 1193 | <td>Set the compression level for a JPEG image, which increases or decreases image output file |
| 1194 | quality and size</td> |
| 1195 | </tr> |
| 1196 | <tr> |
| 1197 | <td>{@link android.hardware.Camera.Parameters#setFlashMode Flash Mode}</td> |
| 1198 | <td>5</td> |
| 1199 | <td>Turn flash on, off, or use automatic setting</td> |
| 1200 | </tr> |
| 1201 | <tr> |
| 1202 | <td>{@link android.hardware.Camera.Parameters#setColorEffect Color Effects}</td> |
| 1203 | <td>5</td> |
| 1204 | <td>Apply a color effect to the captured image such as black and white, sepia tone or negative. |
| 1205 | </td> |
| 1206 | </tr> |
| 1207 | <tr> |
| 1208 | <td>{@link android.hardware.Camera.Parameters#setAntibanding Anti-Banding}</td> |
| 1209 | <td>5</td> |
| 1210 | <td>Reduces the effect of banding in color gradients due to JPEG compression</td> |
| 1211 | </tr> |
| 1212 | <tr> |
| 1213 | <td>{@link android.hardware.Camera.Parameters#setPictureFormat Picture Format}</td> |
| 1214 | <td>1</td> |
| 1215 | <td>Specify the file format for the picture</td> |
| 1216 | </tr> |
| 1217 | <tr> |
| 1218 | <td>{@link android.hardware.Camera.Parameters#setPictureSize Picture Size}</td> |
| 1219 | <td>1</td> |
| 1220 | <td>Specify the pixel dimensions of the saved picture</td> |
| 1221 | </tr> |
| 1222 | </table> |
| 1223 | |
| 1224 | <p class="note"><strong>Note:</strong> These features are not supported on all devices due to |
| 1225 | hardware differences and software implementation. For information on checking the availability |
| 1226 | of features on the device where your application is running, see <a href="#check-feature">Checking |
| 1227 | feature availability</a>.</p> |
| 1228 | |
| 1229 | |
| 1230 | <h3 id="check-feature">Checking feature availability</h3> |
| 1231 | <p>The first thing to understand when setting out to use camera features on Android devices is that |
| 1232 | not all camera features are supported on all devices. In addition, devices that support a particular |
| 1233 | feature may support them to different levels or with different options. Therefore, part of your |
| 1234 | decision process as you develop a camera application is to decide what camera features you want to |
| 1235 | support and to what level. After making that decision, you should plan on including code in your |
| 1236 | camera application that checks to see if device hardware supports those features and fails |
| 1237 | gracefully if a feature is not available.</p> |
| 1238 | |
| 1239 | <p>You can check the availabilty of camera features by getting an instance of a camera’s parameters |
| 1240 | object, and checking the relevant methods. The following code sample shows you how to obtain a |
| 1241 | {@link android.hardware.Camera.Parameters} object and check if the camera supports the autofocus |
| 1242 | feature:</p> |
| 1243 | |
| 1244 | <pre> |
| 1245 | // get Camera parameters |
| 1246 | Camera.Parameters params = mCamera.getParameters(); |
| 1247 | |
| 1248 | List<String> focusModes = params.getSupportedFocusModes(); |
| 1249 | if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { |
| 1250 | // Autofocus mode is supported |
| 1251 | } |
| 1252 | </pre> |
| 1253 | |
| 1254 | <p>You can use the technique shown above for most camera features. The |
| 1255 | {@link android.hardware.Camera.Parameters} object provides a {@code getSupported...()}, {@code |
| 1256 | is...Supported()} or {@code getMax...()} method to determine if (and to what extent) a feature is |
| 1257 | supported.</p> |
| 1258 | |
| 1259 | <p>If your application requires certain camera features in order to function properly, you can |
| 1260 | require them through additions to your application manifest. When you declare the use of specific |
Dirk Dougherty | 4d7bc655 | 2012-01-27 17:56:49 -0800 | [diff] [blame] | 1261 | camera features, such as flash and auto-focus, Google Play restricts your application from |
Joe Fernandez | 452e41f | 2011-10-25 11:52:10 -0700 | [diff] [blame] | 1262 | being installed on devices which do not support these features. For a list of camera features that |
| 1263 | can be declared in your app manifest, see the manifest |
| 1264 | <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html#hw-features"> Features |
| 1265 | Reference</a>.</p> |
| 1266 | |
| 1267 | <h3 id="using-features">Using camera features</h3> |
| 1268 | <p>Most camera features are activated and controlled using a {@link |
| 1269 | android.hardware.Camera.Parameters} object. You obtain this object by first getting an instance of |
| 1270 | the {@link android.hardware.Camera} object, calling the {@link |
| 1271 | android.hardware.Camera#getParameters getParameters()} method, changing the returned parameter |
| 1272 | object and then setting it back into the camera object, as demonstrated in the following example |
| 1273 | code:</p> |
| 1274 | |
| 1275 | <pre> |
| 1276 | // get Camera parameters |
| 1277 | Camera.Parameters params = mCamera.getParameters(); |
| 1278 | // set the focus mode |
| 1279 | params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); |
| 1280 | // set Camera parameters |
| 1281 | mCamera.setParameters(params); |
| 1282 | </pre> |
| 1283 | |
| 1284 | <p>This technique works for nearly all camera features, and most parameters can be changed at any |
| 1285 | time after you have obtained an instance of the {@link android.hardware.Camera} object. Changes to |
| 1286 | parameters are typically visible to the user immediately in the application’s camera preview. |
| 1287 | On the software side, parameter changes may take several frames to actually take effect as the |
| 1288 | camera hardware processes the new instructions and then sends updated image data.</p> |
| 1289 | |
| 1290 | <p class="caution"><strong>Important:</strong> Some camera features cannot be changed at will. In |
| 1291 | particular, changing the size or orientation of the camera preview requires that you first stop the |
| 1292 | preview, change the preview size, and then restart the preview. Starting with Android 4.0 (API |
| 1293 | Level 14) preview orientation can be changed without restarting the preview.</p> |
| 1294 | |
| 1295 | <p>Other camera features require more code in order to implement, including:</p> |
| 1296 | <ul> |
| 1297 | <li>Metering and focus areas</li> |
| 1298 | <li>Face detection</li> |
| 1299 | <li>Time lapse video</li> |
| 1300 | </ul> |
| 1301 | <p>A quick outline of how to implement these features is provided in the following sections.</p> |
| 1302 | |
| 1303 | |
| 1304 | <h3 id="metering-focus-areas">Metering and focus areas</h3> |
| 1305 | <p>In some photographic scenarios, automatic focusing and light metering may not produce the |
| 1306 | desired results. Starting with Android 4.0 (API Level 14), your camera application can provide |
| 1307 | additional controls to allow your app or users to specify areas in an image to use for determining |
| 1308 | focus or light level settings and pass these values to the camera hardware for use in capturing |
| 1309 | images or video.</p> |
| 1310 | |
| 1311 | <p>Areas for metering and focus work very similarly to other camera features, in that you control |
| 1312 | them through methods in the {@link android.hardware.Camera.Parameters} object. The following code |
| 1313 | demonstrates setting two light metering areas for an instance of |
| 1314 | {@link android.hardware.Camera}:</p> |
| 1315 | |
| 1316 | <pre> |
| 1317 | // Create an instance of Camera |
| 1318 | mCamera = getCameraInstance(); |
| 1319 | |
| 1320 | // set Camera parameters |
| 1321 | Camera.Parameters params = mCamera.getParameters(); |
| 1322 | |
| 1323 | if (params.getMaxNumMeteringAreas() > 0){ // check that metering areas are supported |
| 1324 | List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>(); |
| 1325 | |
| 1326 | Rect areaRect1 = new Rect(-100, -100, 100, 100); // specify an area in center of image |
| 1327 | meteringAreas.add(new Camera.Area(areaRect1, 600)); // set weight to 60% |
| 1328 | Rect areaRect2 = new Rect(800, -1000, 1000, -800); // specify an area in upper right of image |
| 1329 | meteringAreas.add(new Camera.Area(areaRect2, 400)); // set weight to 40% |
| 1330 | params.setMeteringAreas(meteringAreas); |
| 1331 | } |
| 1332 | |
| 1333 | mCamera.setParameters(params); |
| 1334 | </pre> |
| 1335 | |
| 1336 | <p>The {@link android.hardware.Camera.Area} object contains two data parameters: A {@link |
| 1337 | android.graphics.Rect} object for specifying an area within the camera’s field of view and a weight |
| 1338 | value, which tells the camera what level of importance this area should be given in light metering |
| 1339 | or focus calculations.</p> |
| 1340 | |
| 1341 | <p>The {@link android.graphics.Rect} field in a {@link android.hardware.Camera.Area} object |
| 1342 | describes a rectangular shape mapped on a 2000 x 2000 unit grid. The coordinates -1000, -1000 |
| 1343 | represent the top, left corner of the camera image, and coordinates 1000, 1000 represent the |
| 1344 | bottom, right corner of the camera image, as shown in the illustration below.</p> |
| 1345 | |
| 1346 | <img src='images/camera-area-coordinates.png' /> |
| 1347 | <p class="img-caption"> |
| 1348 | <strong>Figure 1.</strong> The red lines illustrate the coordinate system for specifying a |
| 1349 | {@link android.hardware.Camera.Area} within a camera preview. The blue box shows the location and |
| 1350 | shape of an camera area with the {@link android.graphics.Rect} values 333,333,667,667. |
| 1351 | </p> |
| 1352 | |
| 1353 | <p>The bounds of this coordinate system always correspond to the outer edge of the image visible in |
| 1354 | the camera preview and do not shrink or expand with the zoom level. Similarly, rotation of the image |
| 1355 | preview using {@link android.hardware.Camera#setDisplayOrientation Camera.setDisplayOrientation()} |
| 1356 | does not remap the coordinate system.</p> |
| 1357 | |
| 1358 | |
| 1359 | <h3 id="face-detection">Face detection</h3> |
| 1360 | <p>For pictures that include people, faces are usually the most important part of the picture, and |
| 1361 | should be used for determining both focus and white balance when capturing an image. The Android 4.0 |
| 1362 | (API Level 14) framework provides APIs for identifying faces and calculating picture settings using |
| 1363 | face recognition technology.</p> |
| 1364 | |
| 1365 | <p class="note"><strong>Note:</strong> While the face detection feature is running, |
| 1366 | {@link android.hardware.Camera.Parameters#setWhiteBalance}, |
| 1367 | {@link android.hardware.Camera.Parameters#setFocusAreas} and |
| 1368 | {@link android.hardware.Camera.Parameters#setMeteringAreas} have no effect.</p> |
| 1369 | |
| 1370 | <p>Using the face detection feature in your camera application requires a few general steps:</p> |
| 1371 | <ul> |
| 1372 | <li>Check that face detection is supported on the device</li> |
| 1373 | <li>Create a face detection listener</li> |
| 1374 | <li>Add the face detection listener to your camera object</li> |
| 1375 | <li>Start face detection after preview (and after <em>every</em> preview restart)</li> |
| 1376 | </ul> |
| 1377 | |
| 1378 | <p>The face detection feature is not supported on all devices. You can check that this feature is |
| 1379 | supported by calling {@link android.hardware.Camera.Parameters#getMaxNumDetectedFaces}. An |
| 1380 | example of this check is shown in the {@code startFaceDetection()} sample method below.</p> |
| 1381 | |
| 1382 | <p>In order to be notified and respond to the detection of a face, your camera application must set |
| 1383 | a listener for face detection events. In order to do this, you must create a listener class that |
| 1384 | implements the {@link android.hardware.Camera.FaceDetectionListener} interface as shown in the |
| 1385 | example code below.</p> |
| 1386 | |
| 1387 | <pre> |
| 1388 | class MyFaceDetectionListener implements Camera.FaceDetectionListener { |
| 1389 | |
| 1390 | @Override |
| 1391 | public void onFaceDetection(Face[] faces, Camera camera) { |
| 1392 | if (faces.length > 0){ |
| 1393 | Log.d("FaceDetection", "face detected: "+ faces.length + |
| 1394 | " Face 1 Location X: " + faces[0].rect.centerX() + |
| 1395 | "Y: " + faces[0].rect.centerY() ); |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | </pre> |
| 1400 | |
| 1401 | <p>After creating this class, you then set it into your application’s |
| 1402 | {@link android.hardware.Camera} object, as shown in the example code below:</p> |
| 1403 | |
| 1404 | <pre> |
| 1405 | mCamera.setFaceDetectionListener(new MyFaceDetectionListener()); |
| 1406 | </pre> |
| 1407 | |
| 1408 | <p>Your application must start the face detection function each time you start (or restart) the |
| 1409 | camera preview. Create a method for starting face detection so you can call it as needed, as shown |
| 1410 | in the example code below.</p> |
| 1411 | |
| 1412 | <pre> |
| 1413 | public void startFaceDetection(){ |
| 1414 | // Try starting Face Detection |
| 1415 | Camera.Parameters params = mCamera.getParameters(); |
| 1416 | |
| 1417 | // start face detection only *after* preview has started |
| 1418 | if (params.getMaxNumDetectedFaces() > 0){ |
| 1419 | // camera supports face detection, so can start it: |
| 1420 | mCamera.startFaceDetection(); |
| 1421 | } |
| 1422 | } |
| 1423 | </pre> |
| 1424 | |
| 1425 | <p>You must start face detection <em>each time</em> you start (or restart) the camera preview. If |
| 1426 | you use the preview class shown in <a href="#camera-preview">Creating a preview class</a>, add your |
| 1427 | {@link android.hardware.Camera#startFaceDetection startFaceDetection()} method to both the |
| 1428 | {@link android.view.SurfaceHolder.Callback#surfaceCreated surfaceCreated()} and {@link |
| 1429 | android.view.SurfaceHolder.Callback#surfaceChanged surfaceChanged()} methods in your preview class, |
| 1430 | as shown in the sample code below.</p> |
| 1431 | |
| 1432 | <pre> |
| 1433 | public void surfaceCreated(SurfaceHolder holder) { |
| 1434 | try { |
| 1435 | mCamera.setPreviewDisplay(holder); |
| 1436 | mCamera.startPreview(); |
| 1437 | |
| 1438 | startFaceDetection(); // start face detection feature |
| 1439 | |
| 1440 | } catch (IOException e) { |
| 1441 | Log.d(TAG, "Error setting camera preview: " + e.getMessage()); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
| 1446 | |
| 1447 | if (mHolder.getSurface() == null){ |
| 1448 | // preview surface does not exist |
| 1449 | Log.d(TAG, "mHolder.getSurface() == null"); |
| 1450 | return; |
| 1451 | } |
| 1452 | |
| 1453 | try { |
| 1454 | mCamera.stopPreview(); |
| 1455 | |
| 1456 | } catch (Exception e){ |
| 1457 | // ignore: tried to stop a non-existent preview |
| 1458 | Log.d(TAG, "Error stopping camera preview: " + e.getMessage()); |
| 1459 | } |
| 1460 | |
| 1461 | try { |
| 1462 | mCamera.setPreviewDisplay(mHolder); |
| 1463 | mCamera.startPreview(); |
| 1464 | |
| 1465 | startFaceDetection(); // re-start face detection feature |
| 1466 | |
| 1467 | } catch (Exception e){ |
| 1468 | // ignore: tried to stop a non-existent preview |
| 1469 | Log.d(TAG, "Error starting camera preview: " + e.getMessage()); |
| 1470 | } |
| 1471 | } |
| 1472 | </pre> |
| 1473 | |
| 1474 | <p class="note"><strong>Note:</strong> Remember to call this method <em>after</em> calling |
| 1475 | {@link android.hardware.Camera#startPreview startPreview()}. Do not attempt to start face detection |
| 1476 | in the {@link android.app.Activity#onCreate onCreate()} method of your camera app’s main activity, |
| 1477 | as the preview is not available by this point in your application's the execution.</p> |
| 1478 | |
| 1479 | |
| 1480 | <h3 id="time-lapse-video">Time lapse video</h3> |
| 1481 | <p>Time lapse video allows users to create video clips that combine pictures taken a few seconds or |
| 1482 | minutes apart. This feature uses {@link android.media.MediaRecorder} to record the images for a time |
| 1483 | lapse sequence. </p> |
| 1484 | |
| 1485 | <p>To record a time lapse video with {@link android.media.MediaRecorder}, you must configure the |
| 1486 | recorder object as if you are recording a normal video, setting the captured frames per second to a |
| 1487 | low number and using one of the time lapse quality settings, as shown in the code example below.</p> |
| 1488 | |
| 1489 | <pre> |
| 1490 | // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) |
| 1491 | mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_HIGH)); |
| 1492 | ... |
| 1493 | // Step 5.5: Set the video capture rate to a low number |
| 1494 | mMediaRecorder.setCaptureRate(0.1); // capture a frame every 10 seconds |
| 1495 | </pre> |
| 1496 | |
| 1497 | <p>These settings must be done as part of a larger configuration procedure for {@link |
| 1498 | android.media.MediaRecorder}. For a full configuration code example, see <a |
| 1499 | href="#configuring-mediarecorder">Configuring MediaRecorder</a>. Once the configuration is complete, |
| 1500 | you start the video recording as if you were recording a normal video clip. For more information |
| 1501 | about configuring and running {@link android.media.MediaRecorder}, see <a |
| 1502 | href="#capture-video">Capturing videos</a>.</p> |