199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpage.title=Camera
233baa5ad7d8cdcc89ce4fbc3bc8cd537d5f5d639Joe Fernandezpage.tags=photo,video,picture,mediarecorder
399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez@jd:body
499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<div id="qv-wrapper">
699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <div id="qv">
799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <h2>In this document</h2>
899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <ol>
999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li><a href="#considerations">Considerations</a></li>
1099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li><a href="#basics">The Basics</a>
1199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li><a href="#manifest">Manifest Declarations</a></li>
1299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li><a href="#intents">Using Existing Camera Apps</a>
1399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <ol>
1499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#intent-image">Image capture intent</a></li>
1599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#intent-video">Video capture intent</a></li>
1699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#intent-receive">Receiving camera intent result</a></li>
1799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      </ol>
1899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li><a href="#custom-camera">Building a Camera App</a>
1999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <ol>
2099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#detect-camera">Detecting camera hardware</a></li>
2199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#access-camera">Accessing cameras</a></li>
2299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#check-camera-features">Checking camera features</a></li>
2399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#camera-preview">Creating a preview class</a></li>
2499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#preview-layout">Placing preview in a layout</a></li>
2599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#capture-picture">Capturing pictures</a></li>
2699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#capture-video">Capturing videos</a></li>
2799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <li><a href="#release-camera">Releasing the camera</a></li>
2899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      </ol>
2999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    </li>
3099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li><a href="#saving-media">Saving Media Files</a></li>
31452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <li><a href="#camera-features">Camera Features</a>
32452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez      <ol>
33452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        <li><a href="#check-feature">Checking feature availability</a></li>
34452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        <li><a href="#using-features">Using camera features</a></li>
35452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        <li><a href="#metering-focus-areas">Metering and focus areas</a></li>
36452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        <li><a href="#face-detection">Face detection</a></li>
37452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        <li><a href="#time-lapse-video">Time lapse video</a></li>
38452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez      </ol>
39452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    </li>
4099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </ol>
4199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <h2>Key Classes</h2>
4299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <ol>
4399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li>{@link android.hardware.Camera}</li>
4499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li>{@link android.view.SurfaceView}</li>
4599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li>{@link android.media.MediaRecorder}</li>
4699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li>{@link android.content.Intent}</li>
4799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </ol>
4899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <h2>See also</h2>
4999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <ol>
50452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <li><a href="{@docRoot}guide/topics/media/mediaplayer.html">Media Playback</a></li>
5199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <li><a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a></li>
5299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </ol>
5399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </div>
5499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</div>
5599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
5699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
5799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The Android framework includes support for various cameras and camera features available on
5899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezdevices, allowing you to capture pictures and videos in your applications. This document discusses a
5999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezquick, simple approach to image and video capture and outlines an advanced approach for creating
6099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcustom camera experiences for your users.</p>
6199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
6299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h2 id="considerations">Considerations</h2>
6399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Before enabling your application to use cameras on Android devices, you should consider a few
6499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezquestions about how your app intends to use this hardware feature.</p>
6599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
6699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ul>
6799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Camera Requirement</strong> - Is the use of a camera so important to your
6899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication that you do not want your application installed on a device that does not have a
6999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcamera? If so, you should declare the <a href="#manifest">camera requirement in your
7099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezmanifest</a>.</li>
7199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
7299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Quick Picture or Customized Camera</strong> - How will your application use the
7399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcamera? Are you just interested in snapping a quick picture or video clip, or will your application
74452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezprovide a new way to use cameras? For a getting a quick snap or clip, consider
7599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<a href="#intents">Using Existing Camera Apps</a>. For developing a customized camera feature, check
7699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezout the <a href="#custom-camera">Building a Camera App</a> section.</li>
7799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
7899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Storage</strong> - Are the images or videos your application generates intended to be
7999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezonly visible to your application or shared so that other applications such as Gallery or other
8099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezmedia and social apps can use them? Do you want the pictures and videos to be available even if your
8199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication is uninstalled? Check out the <a href="#saving-media">Saving Media Files</a> section to
8299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezsee how to implement these options.</li>
8399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ul>
8499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
8599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
8699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
8799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h2 id="basics">The Basics</h2>
8899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The Android framework supports capturing images and video through the
89b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville Talvala{@link android.hardware.camera2} API or camera {@link android.content.Intent}. Here are the relevant
9099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezclasses:</p>
9199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
9299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<dl>
93b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville Talvala  <dt>{@link android.hardware.camera2}</dt>
94b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville Talvala  <dd>This package is the primary API for controlling device cameras. It can be used to take
95452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezpictures or videos when you are building a camera application.</dd>
9699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
97b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville Talvala  <dt>{@link android.hardware.Camera}</dt>
98b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville Talvala  <dd>This class is the older deprecated API for controlling device cameras.</dd>
99b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville Talvala
10099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <dt>{@link android.view.SurfaceView}</dt>
10199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <dd>This class is used to present a live camera preview to the user.</dd>
10299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
10399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <dt>{@link android.media.MediaRecorder}</dt>
10499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <dd>This class is used to record video from the camera.</dd>
10599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
10699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <dt>{@link android.content.Intent}</dt>
10799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <dd>An intent action type of {@link android.provider.MediaStore#ACTION_IMAGE_CAPTURE
10899b70f3f5d051261229d1792c169a374fc23326bJoe FernandezMediaStore.ACTION_IMAGE_CAPTURE} or {@link android.provider.MediaStore#ACTION_VIDEO_CAPTURE
10999b70f3f5d051261229d1792c169a374fc23326bJoe FernandezMediaStore.ACTION_VIDEO_CAPTURE} can be used to capture images or videos without directly
11099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezusing the {@link android.hardware.Camera} object.</dd>
11199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</dl>
11299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
11399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
11499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h2 id="manifest">Manifest Declarations</h2>
11599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Before starting development on your application with the Camera API, you should make sure
11699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezyour manifest has the appropriate declarations to allow use of camera hardware and other
11799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezrelated features.</p>
11899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
11999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ul>
12099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Camera Permission</strong> - Your application must request permission to use a device
12199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcamera.
12299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
12399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
12499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
12599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <p class="note"><strong>Note:</strong> If you are using the camera <a href="#intents">via an
12699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezintent</a>, your application does not need to request this permission.</p>
12799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </li>
12899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Camera Features</strong> - Your application must also declare use of camera features,
12999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezfor example:
13099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
13199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;uses-feature android:name=&quot;android.hardware.camera&quot; /&gt;
13299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
133452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <p>For a list of camera features, see the manifest
134452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<a href="{@docRoot}guide/topics/manifest/uses-feature-element.html#hw-features">Features
13599b70f3f5d051261229d1792c169a374fc23326bJoe FernandezReference</a>.</p>
1364d7bc65538c7cd9fbb1fbbcf22d1da47fcee1219Dirk Dougherty  <p>Adding camera features to your manifest causes Google Play to prevent your application from
13799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezbeing installed to devices that do not include a camera or do not support the camera features you
1384d7bc65538c7cd9fbb1fbbcf22d1da47fcee1219Dirk Doughertyspecify. For more information about using feature-based filtering with Google Play, see <a
1394d7bc65538c7cd9fbb1fbbcf22d1da47fcee1219Dirk Doughertyhref="{@docRoot}guide/topics/manifest/uses-feature-element.html#market-feature-filtering">Google
1404d7bc65538c7cd9fbb1fbbcf22d1da47fcee1219Dirk DoughertyPlay and Feature-Based Filtering</a>.</p>
14199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <p>If your application <em>can use</em> a camera or camera feature for proper operation, but does
14299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandeznot <em>require</em> it, you should specify this in the manifest by including the {@code
14399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid:required} attribute, and setting it to {@code false}:</p>
14499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
14599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;uses-feature android:name="android.hardware.camera" android:required="false" /&gt;
14699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
14799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
14899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </li>
14999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Storage Permission</strong> - If your application saves images or videos to the
15099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezdevice's external storage (SD Card), you must also specify this in the manifest.
15199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
15299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
15399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
15499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </li>
15599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Audio Recording Permission</strong> - For recording audio with video capture, your
15699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication must request the audio capture permission.
15799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
15899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
15999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
16099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </li>
161452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li><strong>Location Permission</strong> - If your application tags images with GPS location
162452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezinformation, you must request location permission:
163452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<pre>
164452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
165452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</pre>
166452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>For more information about getting user location, see
16750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<a href="{@docRoot}guide/topics/location/strategies.html">Location Strategies</a>.</p>
168452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </li>
16999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ul>
17099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
17199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
17299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h2 id="intents">Using Existing Camera Apps</h2>
17399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>A quick way to enable taking pictures or videos in your application without a lot of extra code
17499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezis to use an {@link android.content.Intent} to invoke an existing Android camera application. A
17599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcamera intent makes a request to capture a picture or video clip through an existing camera app and
17699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezthen returns control back to your application. This section shows you how to capture an image or
17799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezvideo using this technique.</p>
17899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
17999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The procedure for invoking a camera intent follows these general steps:</p>
18099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
18199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ol>
18299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Compose a Camera Intent</strong> - Create an {@link android.content.Intent} that
18399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezrequests an image or video, using one of these intent types:
18499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <ul>
18599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li>{@link android.provider.MediaStore#ACTION_IMAGE_CAPTURE MediaStore.ACTION_IMAGE_CAPTURE} -
18699b70f3f5d051261229d1792c169a374fc23326bJoe FernandezIntent action type for requesting an image from an existing camera application.</li>
18799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li>{@link android.provider.MediaStore#ACTION_VIDEO_CAPTURE MediaStore.ACTION_VIDEO_CAPTURE} -
18899b70f3f5d051261229d1792c169a374fc23326bJoe FernandezIntent action type for requesting a video from an existing camera application. </li>
18999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    </ul>
19099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </li>
19199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Start the Camera Intent</strong> - Use the {@link
19299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.app.Activity#startActivityForResult(android.content.Intent, int) startActivityForResult()}
19399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezmethod to execute the camera intent. After you start the intent, the Camera application user
19499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezinterface appears on the device screen and the user can take a picture or video.</li>
19599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Receive the Intent Result</strong> - Set up an {@link
19699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} method
19799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezin your application to receive the callback and data from the camera intent. When the user
19899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezfinishes taking a picture or video (or cancels the operation), the system calls this method.</li>
19999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ol>
20099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
20199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
20299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="intent-image">Image capture intent</h3>
20399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Capturing images using a camera intent is quick way to enable your application to take pictures
20499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezwith minimal coding. An image capture intent can include the following extra information:</p>
20599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
20699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ul>
20799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.provider.MediaStore#EXTRA_OUTPUT MediaStore.EXTRA_OUTPUT} - This setting
20899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezrequires a {@link android.net.Uri} object specifying a path and file name where you'd like to
20999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezsave the picture. This setting is optional but strongly recommended. If you do not specify this
21099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezvalue, the camera application saves the requested picture in the default location with a default
21199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezname, specified in the returned intent's {@link android.content.Intent#getData() Intent.getData()}
21299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezfield.</li>
21399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ul>
21499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
21599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The following example demonstrates how to construct a image capture intent and execute it.
21699b70f3f5d051261229d1792c169a374fc23326bJoe FernandezThe {@code getOutputMediaFileUri()} method in this example refers to the sample code shown in <a
21799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhref= "#saving-media">Saving Media Files</a>.</p>
21899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
21999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
22099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
22199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate Uri fileUri;
22299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
22399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&#64;Override
22499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpublic void onCreate(Bundle savedInstanceState) {
22599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    super.onCreate(savedInstanceState);
22699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    setContentView(R.layout.main);
22799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
22899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // create Intent to take a picture and return control to the calling application
22999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
23099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
23199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
23299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
23399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
23499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // start the image capture Intent
23599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
23699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
23799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
23899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
23999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>When the {@link android.app.Activity#startActivityForResult(android.content.Intent, int)
24099b70f3f5d051261229d1792c169a374fc23326bJoe FernandezstartActivityForResult()} method is executed, users see a camera application interface.
24199b70f3f5d051261229d1792c169a374fc23326bJoe FernandezAfter the user finishes taking a picture (or cancels the operation), the user interface returns to
24299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezyour application, and you must intercept the {@link
24399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()}
24499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezmethod to receive the result of the intent and continue your application execution. For information
245452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezon how to receive the completed intent, see <a href="#intent-receive">Receiving camera intent
246452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezresult</a>.</p>
24799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
24899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
24999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="intent-video">Video capture intent</h3>
25099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Capturing video using a camera intent is a quick way to enable your application to take videos
25199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezwith minimal coding. A video capture intent can include the following extra information:</p>
25299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
25399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ul>
25499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.provider.MediaStore#EXTRA_OUTPUT MediaStore.EXTRA_OUTPUT} - This setting
25599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezrequires a {@link android.net.Uri} specifying a path and file name where you'd like to save the
25699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezvideo. This setting is optional but strongly recommended. If you do not specify this value, the
25799b70f3f5d051261229d1792c169a374fc23326bJoe FernandezCamera application saves the requested video in the default location with a default name, specified
25899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezin the returned intent's {@link android.content.Intent#getData() Intent.getData()} field.</li>
25999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.provider.MediaStore#EXTRA_VIDEO_QUALITY MediaStore.EXTRA_VIDEO_QUALITY} -
26099b70f3f5d051261229d1792c169a374fc23326bJoe FernandezThis value can be 0 for lowest quality and smallest file size or 1 for highest quality and
26199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezlarger file size.</li>
26299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.provider.MediaStore#EXTRA_DURATION_LIMIT MediaStore.EXTRA_DURATION_LIMIT} -
26399b70f3f5d051261229d1792c169a374fc23326bJoe FernandezSet this value to limit the length, in seconds, of the video being captured.</li>
26499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.provider.MediaStore#EXTRA_SIZE_LIMIT MediaStore.EXTRA_SIZE_LIMIT} -
26599b70f3f5d051261229d1792c169a374fc23326bJoe FernandezSet this value to limit the file size, in bytes, of the video being captured.
26699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</li>
26799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ul>
26899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
26999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The following example demonstrates how to construct a video capture intent and execute it.
27099b70f3f5d051261229d1792c169a374fc23326bJoe FernandezThe {@code getOutputMediaFileUri()} method in this example refers to the sample code shown in <a
27199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhref= "#saving-media">Saving Media Files</a>.</p>
27299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
27399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
27499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
27599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate Uri fileUri;
27699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
27799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&#64;Override
27899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpublic void onCreate(Bundle savedInstanceState) {
27999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    super.onCreate(savedInstanceState);
28099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    setContentView(R.layout.main);
28199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
28299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    //create new Intent
28399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
28499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
28599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video
28699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name
28799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
28899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
28999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
29099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // start the Video Capture Intent
29199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
29299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
29399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
29499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
29599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>When the {@link
29699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.app.Activity#startActivityForResult(android.content.Intent, int)
29799b70f3f5d051261229d1792c169a374fc23326bJoe FernandezstartActivityForResult()} method is executed, users see a modified camera application interface.
29899b70f3f5d051261229d1792c169a374fc23326bJoe FernandezAfter the user finishes taking a video (or cancels the operation), the user interface
29999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezreturns to your application, and you must intercept the {@link
30099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()}
30199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezmethod to receive the result of the intent and continue your application execution. For information
30299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezon how to receive the completed intent, see the next section.</p>
30399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
30499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="intent-receive">Receiving camera intent result</h3>
30599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Once you have constructed and executed an image or video camera intent, your application must be
30699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezconfigured to receive the result of the intent. This section shows you how to intercept the callback
30799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezfrom a camera intent so your application can do further processing of the captured image or
30899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezvideo.</p>
30999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
31099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>In order to receive the result of an intent, you must override the {@link
31199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} in the
31299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezactivity that started the intent. The following example demonstrates how to override {@link
31399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} to
31499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcapture the result of the <a href="#intent-image">image camera intent</a> or <a
31599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhref="#intent-video">video camera intent</a> examples shown in the previous sections.</p>
31699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
31799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
31899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
31999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
32099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
32199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&#64;Override
32299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
32399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
32499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        if (resultCode == RESULT_OK) {
32599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            // Image captured and saved to fileUri specified in the Intent
32699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            Toast.makeText(this, "Image saved to:\n" +
32799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                     data.getData(), Toast.LENGTH_LONG).show();
32899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        } else if (resultCode == RESULT_CANCELED) {
32999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            // User cancelled the image capture
33099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        } else {
33199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            // Image capture failed, advise user
33299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
33399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
33499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
33599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
33699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        if (resultCode == RESULT_OK) {
33799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            // Video captured and saved to fileUri specified in the Intent
33899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            Toast.makeText(this, "Video saved to:\n" +
33999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                     data.getData(), Toast.LENGTH_LONG).show();
34099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        } else if (resultCode == RESULT_CANCELED) {
34199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            // User cancelled the video capture
34299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        } else {
34399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            // Video capture failed, advise user
34499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
34599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
34699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
34799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
34899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
34999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Once your activity receives a successful result, the captured image or video is available in the
35099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezspecified location for your application to access.</p>
35199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
35299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
35399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
35499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h2 id="custom-camera">Building a Camera App</h2>
35599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Some developers may require a camera user interface that is customized to the look of their
35699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication or provides special features. Creating a customized camera activity requires more
35799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcode than <a href="#intents">using an intent</a>, but it can provide a more compelling experience
35899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezfor your users.</p>
35999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
360b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville Talvala<p><strong> Note: The following guide is for the older, deprecated {@link android.hardware.Camera}
361b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville TalvalaAPI. For new or advanced camera applications, the newer {@link android.hardware.camera2} API is
362b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville Talvalarecommended.</strong></p>
363b942b05093d2b1cee59ac73196a4b99962f10addEino-Ville Talvala
36499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The general steps for creating a custom camera interface for your application are as follows:</p>
36599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
36699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ul>
36799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez   <li><strong>Detect and Access Camera</strong> - Create code to check for the existence of
36899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcameras and request access.</li>
36999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez   <li><strong>Create a Preview Class</strong> - Create a camera preview class that extends {@link
37099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.view.SurfaceView} and implements the {@link android.view.SurfaceHolder} interface. This
37199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezclass previews the live images from the camera.</li>
37299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez   <li><strong>Build a Preview Layout</strong> - Once you have the camera preview class, create a
37399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezview layout that incorporates the preview and the user interface controls you want.</li>
37499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez   <li><strong>Setup Listeners for Capture</strong> - Connect listeners for your interface
37599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcontrols to start image or video capture in response to user actions, such as pressing a
37699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezbutton.</li>
37799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez   <li><strong>Capture and Save Files</strong> - Setup the code for capturing pictures or
37899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezvideos and saving the output.</li>
37999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez   <li><strong>Release the Camera</strong> - After using the camera, your application must
38099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezproperly release it for use by other applications.</li>
38199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ul>
38299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
38399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Camera hardware is a shared resource that must be carefully managed so your application does
38499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandeznot collide with other applications that may also want to use it. The following sections discusses
385452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezhow to detect camera hardware, how to request access to a camera, how to capture pictures or video
386452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezand how to release the camera when your application is done using it.</p>
38799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
38899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="caution"><strong>Caution:</strong> Remember to release the {@link android.hardware.Camera}
38999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezobject by calling the {@link android.hardware.Camera#release() Camera.release()} when your
39099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication is done using it! If your application does not properly release the camera, all
39199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezsubsequent attempts to access the camera, including those by your own application, will fail and may
39299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcause your or other applications to be shut down.</p>
39399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
39499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
39599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="detect-camera">Detecting camera hardware</h3>
39699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>If your application does not specifically require a camera using a manifest declaration, you
39799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezshould check to see if a camera is available at runtime. To perform this check, use the {@link
39899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.content.pm.PackageManager#hasSystemFeature(java.lang.String)
39999b70f3f5d051261229d1792c169a374fc23326bJoe FernandezPackageManager.hasSystemFeature()} method, as shown in the example code below:</p>
40099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
40199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
40299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez/** Check if this device has a camera */
40399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate boolean checkCameraHardware(Context context) {
40499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
40599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // this device has a camera
40699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        return true;
40799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    } else {
40899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // no camera on this device
40999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        return false;
41099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
41199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
41299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
41399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
41499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Android devices can have multiple cameras, for example a back-facing camera for photography and a
41599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezfront-facing camera for video calls. Android 2.3 (API Level 9) and later allows you to check the
41699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandeznumber of cameras available on a device using the {@link
41799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#getNumberOfCameras() Camera.getNumberOfCameras()} method.</p>
41899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
41999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="access-camera">Accessing cameras</h3>
42099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>If you have determined that the device on which your application is running has a camera, you
42199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezmust request to access it by getting an instance of {@link android.hardware.Camera} (unless you
42299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezare using an <a href="#intents">intent to access the camera</a>). </p>
42399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
42499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>To access the primary camera, use the {@link android.hardware.Camera#open() Camera.open()} method
42599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezand be sure to catch any exceptions, as shown in the code below:</p>
42699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
42799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
42899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez/** A safe way to get an instance of the Camera object. */
42999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpublic static Camera getCameraInstance(){
43099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    Camera c = null;
43199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    try {
43299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        c = Camera.open(); // attempt to get a Camera instance
43399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
43499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    catch (Exception e){
43599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // Camera is not available (in use or does not exist)
43699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
43799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    return c; // returns null if camera is unavailable
43899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
43999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
44099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
44199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="caution"><strong>Caution:</strong> Always check for exceptions when using {@link
44299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#open() Camera.open()}. Failing to check for exceptions if the camera is in
44399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezuse or does not exist will cause your application to be shut down by the system.</p>
44499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
44599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>On devices running Android 2.3 (API Level 9) or higher, you can access specific cameras using
44699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez{@link android.hardware.Camera#open(int) Camera.open(int)}. The example code above will access
44799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezthe first, back-facing camera on a device with more than one camera.</p>
44899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
44999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="check-camera-features">Checking camera features</h3>
4506f5b5ee3212c194cd20750af7adb90f938bc8fcdPin Ting<p>Once you obtain access to a camera, you can get further information about its capabilities using
45199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezthe {@link android.hardware.Camera#getParameters() Camera.getParameters()} method and checking the
45299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezreturned {@link android.hardware.Camera.Parameters} object for supported capabilities. When using
45399b70f3f5d051261229d1792c169a374fc23326bJoe FernandezAPI Level 9 or higher, use the {@link android.hardware.Camera#getCameraInfo(int,
45499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera.CameraInfo) Camera.getCameraInfo()} to determine if a camera is on the front
45599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezor back of the device, and the orientation of the image.</p>
45699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
45799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
45899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
45999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="camera-preview">Creating a preview class</h3>
46099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>For users to effectively take pictures or video, they must be able to see what the device camera
46199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezsees. A camera preview class is a {@link android.view.SurfaceView} that can display the live image
46299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezdata coming from a camera, so users can frame and capture a picture or video.</p>
46399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
46499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The following example code demonstrates how to create a basic camera preview class that can be
46599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezincluded in a {@link android.view.View} layout. This class implements {@link
46699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.view.SurfaceHolder.Callback SurfaceHolder.Callback} in order to capture the callback events
46799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezfor creating and destroying the view, which are needed for assigning the camera preview input.</p>
46899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
46999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
47099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez/** A basic Camera preview class */
47199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpublic class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
47299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    private SurfaceHolder mHolder;
47399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    private Camera mCamera;
47499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
47599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    public CameraPreview(Context context, Camera camera) {
47699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        super(context);
47799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        mCamera = camera;
47899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
47999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // Install a SurfaceHolder.Callback so we get notified when the
48099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // underlying surface is created and destroyed.
48199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        mHolder = getHolder();
48299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        mHolder.addCallback(this);
48399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // deprecated setting, but required on Android versions prior to 3.0
48499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
48599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
48699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
48799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    public void surfaceCreated(SurfaceHolder holder) {
48899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // The Surface has been created, now tell the camera where to draw the preview.
48999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        try {
49099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mCamera.setPreviewDisplay(holder);
49199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mCamera.startPreview();
49299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        } catch (IOException e) {
49399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
49499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
49599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
49699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
49799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    public void surfaceDestroyed(SurfaceHolder holder) {
49899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // empty. Take care of releasing the Camera preview in your activity.
49999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
50099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
50199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
50299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // If your preview can change or rotate, take care of those events here.
50399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // Make sure to stop the preview before resizing or reformatting it.
50499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
50599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        if (mHolder.getSurface() == null){
50699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          // preview surface does not exist
50799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          return;
50899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
50999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
51099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // stop preview before making changes
51199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        try {
51299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mCamera.stopPreview();
51399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        } catch (Exception e){
51499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          // ignore: tried to stop a non-existent preview
51599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
51699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
517452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        // set preview size and make any resize, rotate or
518452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        // reformatting changes here
51999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
52099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // start preview with new settings
52199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        try {
52299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mCamera.setPreviewDisplay(mHolder);
52399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mCamera.startPreview();
52499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
52599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        } catch (Exception e){
52699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
52799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
52899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
52999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
53099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
53199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
532452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>If you want to set a specific size for your camera preview, set this in the {@code
533452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezsurfaceChanged()} method as noted in the comments above. When setting preview size, you
534452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<em>must use</em> values from {@link android.hardware.Camera.Parameters#getSupportedPreviewSizes}.
535452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<em>Do not</em> set arbitrary values in the {@link
536452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezandroid.hardware.Camera.Parameters#setPreviewSize setPreviewSize()} method.</p>
537452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
53899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
53999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="preview-layout">Placing preview in a layout</h3>
54099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>A camera preview class, such as the example shown in the previous section, must be placed in the
54199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezlayout of an activity along with other user interface controls for taking a picture or video. This
54299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezsection shows you how to build a basic layout and activity for the preview.</p>
54399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
54499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The following layout code provides a very basic view that can be used to display a camera
54599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpreview. In this example, the {@link android.widget.FrameLayout} element is meant to be the
54699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcontainer for the camera preview class. This layout type is used so that additional picture
54799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezinformation or controls can be overlayed on the live camera preview images.</p>
54899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
54999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
55099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
55199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android";
55299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:orientation=&quot;horizontal&quot;
55399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:layout_width=&quot;fill_parent&quot;
55499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:layout_height=&quot;fill_parent&quot;
55599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    &gt;
55699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  &lt;FrameLayout
55799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:id=&quot;@+id/camera_preview&quot;
55899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:layout_width=&quot;fill_parent&quot;
55999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:layout_height=&quot;fill_parent&quot;
56099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:layout_weight=&quot;1&quot;
56199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    /&gt;
56299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
56399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  &lt;Button
56499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:id=&quot;@+id/button_capture&quot;
56599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:text=&quot;Capture&quot;
56699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:layout_width=&quot;wrap_content&quot;
56799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:layout_height=&quot;wrap_content&quot;
56899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    android:layout_gravity=&quot;center&quot;
56999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    /&gt;
57099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;/LinearLayout&gt;
57199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
57299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
57399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>On most devices, the default orientation of the camera preview is landscape. This example layout
57499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezspecifies a horizontal (landscape) layout and the code below fixes the orientation of the
57599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication to landscape. For simplicity in rendering a camera preview, you should change your
57699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication's preview activity orientation to landscape by adding the following to your
57799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezmanifest.</p>
57899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
57999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
58099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;activity android:name=&quot;.CameraActivity&quot;
58199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          android:label=&quot;@string/app_name&quot;
58299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
58399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          android:screenOrientation=&quot;landscape&quot;&gt;
58499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          &lt;!-- configure this activity to use landscape orientation --&gt;
58599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
58699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          &lt;intent-filter&gt;
58799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
58899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
58999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    &lt;/intent-filter&gt;
59099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez&lt;/activity&gt;
59199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
59299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
59399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="note"><strong>Note:</strong> A camera preview does not have to be in landscape mode.
59499b70f3f5d051261229d1792c169a374fc23326bJoe FernandezStarting in Android 2.2 (API Level 8), you can use the {@link
59599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#setDisplayOrientation(int) setDisplayOrientation()} method to set the
59699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezrotation of the preview image. In order to change preview orientation as the user re-orients the
59799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezphone, within the {@link
59899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int)
59999b70f3f5d051261229d1792c169a374fc23326bJoe FernandezsurfaceChanged()} method of your preview class, first stop the preview with {@link
60099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#stopPreview() Camera.stopPreview()} change the orientation and then
60199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezstart the preview again with {@link android.hardware.Camera#startPreview()
60299b70f3f5d051261229d1792c169a374fc23326bJoe FernandezCamera.startPreview()}.</p>
60399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
60499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>In the activity for your camera view, add your preview class to the {@link
60599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.widget.FrameLayout} element shown in the example above. Your camera activity must also
60699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezensure that it releases the camera when it is paused or shut down. The following example shows how
60799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezto modify a camera activity to attach the preview class shown in <a href="#camera-preview">Creating
60899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandeza preview class</a>.</p>
60999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
61099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
61199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpublic class CameraActivity extends Activity {
61299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
61399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    private Camera mCamera;
61499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    private CameraPreview mPreview;
61599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
61699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    &#64;Override
61799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    public void onCreate(Bundle savedInstanceState) {
61899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        super.onCreate(savedInstanceState);
61999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        setContentView(R.layout.main);
62099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
62199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // Create an instance of Camera
62299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        mCamera = getCameraInstance();
62399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
62499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        // Create our Preview view and set it as the content of our activity.
62599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        mPreview = new CameraPreview(this, mCamera);
626183bf116978e3c44292c9ead2bceb47e972624a1Scott Main        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
62799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        preview.addView(mPreview);
62899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
62999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
63099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
63199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
63299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="note"><strong>Note:</strong> The {@code getCameraInstance()} method in the example above
63399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezrefers to the example method shown in <a href="#access-camera">Accessing cameras</a>.</p>
63499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
63599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
63699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="capture-picture">Capturing pictures</h3>
63799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Once you have built a preview class and a view layout in which to display it, you are ready to
63899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezstart capturing images with your application. In your application code, you must set up listeners
63999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezfor your user interface controls to respond to a user action by taking a picture.</p>
64099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
64199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>In order to retrieve a picture, use the {@link
64299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#takePicture(android.hardware.Camera.ShutterCallback,
64399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback)
64499b70f3f5d051261229d1792c169a374fc23326bJoe FernandezCamera.takePicture()} method. This method takes three parameters which receive data from the camera.
64599b70f3f5d051261229d1792c169a374fc23326bJoe FernandezIn order to receive data in a JPEG format, you must implement an {@link
64699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera.PictureCallback} interface to receive the image data and
64799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezwrite it to a file. The following code shows a basic implementation of the {@link
64899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera.PictureCallback} interface to save an image received from the camera.</p>
64999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
65099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
65199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate PictureCallback mPicture = new PictureCallback() {
65299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
65399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    &#64;Override
65499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    public void onPictureTaken(byte[] data, Camera camera) {
65599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
65699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
65799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        if (pictureFile == null){
65899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            Log.d(TAG, "Error creating media file, check storage permissions: " +
65999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                e.getMessage());
66099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            return;
66199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
66299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
66399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        try {
66499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            FileOutputStream fos = new FileOutputStream(pictureFile);
66599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            fos.write(data);
66699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            fos.close();
66799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        } catch (FileNotFoundException e) {
66899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            Log.d(TAG, "File not found: " + e.getMessage());
66999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        } catch (IOException e) {
67099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            Log.d(TAG, "Error accessing file: " + e.getMessage());
67199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
67299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
67399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez};
67499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
67599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
67699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Trigger capturing an image by calling the {@link
67799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#takePicture(android.hardware.Camera.ShutterCallback,
67899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback)
67999b70f3f5d051261229d1792c169a374fc23326bJoe FernandezCamera.takePicture()} method. The following example code shows how to call this method from a
68099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezbutton {@link android.view.View.OnClickListener}.</p>
68199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
68299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
68399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez// Add a listener to the Capture button
68499b70f3f5d051261229d1792c169a374fc23326bJoe FernandezButton captureButton = (Button) findViewById(id.button_capture);
6856f5b5ee3212c194cd20750af7adb90f938bc8fcdPin TingcaptureButton.setOnClickListener(
6866f5b5ee3212c194cd20750af7adb90f938bc8fcdPin Ting    new View.OnClickListener() {
68799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        &#64;Override
68899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        public void onClick(View v) {
68999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            // get an image from the camera
69099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mCamera.takePicture(null, null, mPicture);
69199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
69299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
69399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez);
69499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
69599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
69699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="note"><strong>Note:</strong> The {@code mPicture} member in the following example refers
69799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezto the example code above.</p>
69899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
69999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="caution"><strong>Caution:</strong> Remember to release the {@link android.hardware.Camera}
70099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezobject by calling the {@link android.hardware.Camera#release() Camera.release()} when your
70199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication is done using it! For information about how to release the camera, see <a
70299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhref="#release-camera">Releasing the camera</a>.</p>
70399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
70499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
70599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="capture-video">Capturing videos</h3>
70699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
70799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Video capture using the Android framework requires careful management of the {@link
70899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera} object and coordination with the {@link android.media.MediaRecorder}
70999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezclass. When recording video with {@link android.hardware.Camera}, you must manage the {@link
71099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#lock() Camera.lock()} and {@link android.hardware.Camera#unlock()
71199b70f3f5d051261229d1792c169a374fc23326bJoe FernandezCamera.unlock()} calls to allow {@link android.media.MediaRecorder} access to the camera hardware,
71299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezin addition to the {@link android.hardware.Camera#open() Camera.open()} and {@link
71399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#release() Camera.release()} calls.</p>
71499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
71599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="note"><strong>Note:</strong> Starting with Android 4.0 (API level 14), the {@link
71699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#lock() Camera.lock()} and {@link android.hardware.Camera#unlock()
71799b70f3f5d051261229d1792c169a374fc23326bJoe FernandezCamera.unlock()} calls are managed for you automatically.</p>
71899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
71999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Unlike taking pictures with a device camera, capturing video requires a very particular call
72099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezorder. You must follow a specific order of execution to successfully prepare for and capture video
72199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezwith your application, as detailed below.</p>
72299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
72399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ol>
72499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Open Camera</strong> - Use the {@link android.hardware.Camera#open() Camera.open()}
72599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezto get an instance of the camera object.</li>
72699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Connect Preview</strong> - Prepare a live camera image preview by connecting a {@link
72799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.view.SurfaceView} to the camera using {@link
72899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#setPreviewDisplay(android.view.SurfaceHolder) Camera.setPreviewDisplay()}.
72999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </li>
73099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Start Preview</strong> - Call {@link android.hardware.Camera#startPreview()
73199b70f3f5d051261229d1792c169a374fc23326bJoe FernandezCamera.startPreview()} to begin displaying the live camera images.</li>
73299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Start Recording Video</strong> - The following steps must be completed <em>in
73399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezorder</em> to successfully record video:
73499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <ol style="list-style-type: lower-alpha;">
73599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li><strong>Unlock the Camera</strong> - Unlock the camera for use by {@link
73699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder} by calling {@link android.hardware.Camera#unlock()
73799b70f3f5d051261229d1792c169a374fc23326bJoe FernandezCamera.unlock()}.</li>
73899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li><strong>Configure MediaRecorder</strong> - Call in the following {@link
73999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder} methods <em>in this order</em>. For more information, see the {@link
74099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder} reference documentation.
74199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <ol>
74299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          <li>{@link android.media.MediaRecorder#setCamera(android.hardware.Camera)
74399b70f3f5d051261229d1792c169a374fc23326bJoe FernandezsetCamera()} - Set the camera to be used for video capture, use your application's current instance
74499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezof {@link android.hardware.Camera}.</li>
74599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          <li>{@link android.media.MediaRecorder#setAudioSource(int) setAudioSource()} - Set the
74699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezaudio source, use {@link android.media.MediaRecorder.AudioSource#CAMCORDER
74799b70f3f5d051261229d1792c169a374fc23326bJoe FernandezMediaRecorder.AudioSource.CAMCORDER}. </li>
74899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          <li>{@link android.media.MediaRecorder#setVideoSource(int) setVideoSource()} - Set
74999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezthe video source, use {@link android.media.MediaRecorder.VideoSource#CAMERA
75099b70f3f5d051261229d1792c169a374fc23326bJoe FernandezMediaRecorder.VideoSource.CAMERA}.</li>
75199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          <li>Set the video output format and encoding. For Android 2.2 (API Level 8) and
75299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhigher, use the {@link android.media.MediaRecorder#setProfile(android.media.CamcorderProfile)
75399b70f3f5d051261229d1792c169a374fc23326bJoe FernandezMediaRecorder.setProfile} method, and get a profile instance using {@link
75499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.CamcorderProfile#get(int) CamcorderProfile.get()}. For versions of Android prior to
75599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez2.2, you must set the video output format and encoding parameters:
75699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          <ol style="list-style-type: lower-roman;">
75799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            <li>{@link android.media.MediaRecorder#setOutputFormat(int) setOutputFormat()} - Set
75899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezthe output format, specify the default setting or {@link
75999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder.OutputFormat#MPEG_4 MediaRecorder.OutputFormat.MPEG_4}.</li>
76099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            <li>{@link android.media.MediaRecorder#setAudioEncoder(int) setAudioEncoder()} - Set
76199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezthe sound encoding type, specify the default setting or {@link
76299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder.AudioEncoder#AMR_NB MediaRecorder.AudioEncoder.AMR_NB}.</li>
76399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            <li>{@link android.media.MediaRecorder#setVideoEncoder(int) setVideoEncoder()} - Set
76499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezthe video encoding type, specify the default setting or {@link
76599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder.VideoEncoder#MPEG_4_SP MediaRecorder.VideoEncoder.MPEG_4_SP}.</li>
76699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          </ol>
76799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          </li>
76899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          <li>{@link android.media.MediaRecorder#setOutputFile(java.lang.String) setOutputFile()} -
76999b70f3f5d051261229d1792c169a374fc23326bJoe FernandezSet the output file, use {@code getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()} from the example
77099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezmethod in the <a href="#saving-media">Saving Media Files</a> section.</li>
77199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez          <li>{@link android.media.MediaRecorder#setPreviewDisplay(android.view.Surface)
77299b70f3f5d051261229d1792c169a374fc23326bJoe FernandezsetPreviewDisplay()} - Specify the {@link android.view.SurfaceView} preview layout element for
77399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezyour application. Use the same object you specified for <strong>Connect Preview</strong>.</li>
77499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        </ol>
77599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        <p class="caution"><strong>Caution:</strong> You must call these {@link
77699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder} configuration methods <em>in this order</em>, otherwise your
77799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication will encounter errors and the recording will fail.</p>
77899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      </li>
77999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li><strong>Prepare MediaRecorder</strong> - Prepare the {@link android.media.MediaRecorder}
78099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezwith provided configuration settings by calling {@link android.media.MediaRecorder#prepare()
78199b70f3f5d051261229d1792c169a374fc23326bJoe FernandezMediaRecorder.prepare()}.</li>
78299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li><strong>Start MediaRecorder</strong> - Start recording video by calling {@link
78399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder#start() MediaRecorder.start()}.</li>
78499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    </ol>
78599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </li>
78699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Stop Recording Video</strong> - Call the following methods <em>in order</em>, to
78799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezsuccessfully complete a video recording:
78899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    <ol style="list-style-type: lower-alpha;">
78999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li><strong>Stop MediaRecorder</strong> - Stop recording video by calling {@link
79099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder#stop() MediaRecorder.stop()}.</li>
79199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li><strong>Reset MediaRecorder</strong> - Optionally, remove the configuration settings from
79299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezthe recorder by calling {@link android.media.MediaRecorder#reset() MediaRecorder.reset()}.</li>
79399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li><strong>Release MediaRecorder</strong> - Release the {@link android.media.MediaRecorder}
79499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezby calling {@link android.media.MediaRecorder#release() MediaRecorder.release()}.</li>
79599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      <li><strong>Lock the Camera</strong> - Lock the camera so that future {@link
79699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder} sessions can use it by calling {@link android.hardware.Camera#lock()
79799b70f3f5d051261229d1792c169a374fc23326bJoe FernandezCamera.lock()}. Starting with Android 4.0 (API level 14), this call is not required unless the
79899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez{@link android.media.MediaRecorder#prepare() MediaRecorder.prepare()} call fails.</li>
79999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    </ol>
80099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  </li>
80199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Stop the Preview</strong> - When your activity has finished using the camera, stop the
80299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpreview using {@link android.hardware.Camera#stopPreview() Camera.stopPreview()}.</li>
80399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>Release Camera</strong> - Release the camera so that other applications can use
80499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezit by calling {@link android.hardware.Camera#release() Camera.release()}.</li>
80599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ol>
80699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
80799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="note"><strong>Note:</strong> It is possible to use {@link android.media.MediaRecorder}
80899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezwithout creating a camera preview first and skip the first few steps of this process. However,
80999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezsince users typically prefer to see a preview before starting a recording, that process is not
81099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezdiscussed here.</p>
81199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
812452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p class="note"><strong>Tip:</strong> If your application is typically used for recording video, set
813452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera.Parameters#setRecordingHint} to {@code true} prior to starting your
814452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezpreview. This setting can help reduce the time it takes to start recording.</p>
815452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
81699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h4 id="configuring-mediarecorder">Configuring MediaRecorder</h4>
81799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>When using the {@link android.media.MediaRecorder} class to record video, you must perform
81899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezconfiguration steps in a <em>specific order</em> and then call the {@link
81999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder#prepare() MediaRecorder.prepare()} method to check and implement the
82099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezconfiguration. The following example code demonstrates how to properly configure and prepare the
82199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez{@link android.media.MediaRecorder} class for video recording.</p>
82299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
82399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
82499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate boolean prepareVideoRecorder(){
82599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
82699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mCamera = getCameraInstance();
82799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder = new MediaRecorder();
82899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
82999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // Step 1: Unlock and set camera to MediaRecorder
83099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mCamera.unlock();
83199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder.setCamera(mCamera);
83299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
83399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // Step 2: Set sources
83499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
83599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
83699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
83799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
83899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
83999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
84099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // Step 4: Set output file
84199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
84299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
84399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // Step 5: Set the preview output
84499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
84599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
84699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // Step 6: Prepare configured MediaRecorder
84799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    try {
84899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        mMediaRecorder.prepare();
84999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    } catch (IllegalStateException e) {
85099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
85199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        releaseMediaRecorder();
85299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        return false;
85399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    } catch (IOException e) {
85499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
85599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        releaseMediaRecorder();
85699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        return false;
85799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
85899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    return true;
85999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
86099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
86199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
86299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Prior to Android 2.2 (API Level 8), you must set the output format and encoding formats
86399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezparameters directly, instead of using {@link android.media.CamcorderProfile}. This approach is
86499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezdemonstrated in the following code:</p>
86599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
86699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
86799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // Step 3: Set output format and encoding (for versions prior to API Level 8)
86899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
86999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
87099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
87199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
87299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
87399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The following video recording parameters for {@link android.media.MediaRecorder} are given
87499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezdefault settings, however, you may want to adjust these settings for your application:</p>
87599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
87699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ul>
87799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.media.MediaRecorder#setVideoEncodingBitRate(int)
87899b70f3f5d051261229d1792c169a374fc23326bJoe FernandezsetVideoEncodingBitRate()}</li>
87999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.media.MediaRecorder#setVideoSize(int, int) setVideoSize()}</li>
88099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.media.MediaRecorder#setVideoFrameRate(int) setVideoFrameRate()}</li>
88199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.media.MediaRecorder#setAudioEncodingBitRate(int)
88299b70f3f5d051261229d1792c169a374fc23326bJoe FernandezsetAudioEncodingBitRate()}</li>  <li>{@link android.media.MediaRecorder#setAudioChannels(int)
88399b70f3f5d051261229d1792c169a374fc23326bJoe FernandezsetAudioChannels()}</li>
88499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>{@link android.media.MediaRecorder#setAudioSamplingRate(int) setAudioSamplingRate()}</li>
88599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ul>
88699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
887452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<h4 id="start-stop-mediarecorder">Starting and stopping MediaRecorder</h4>
88899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>When starting and stopping video recording using the {@link android.media.MediaRecorder} class,
88999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezyou must follow a specific order, as listed below.</p>
89099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
89199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ol>
89299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>Unlock the camera with {@link android.hardware.Camera#unlock() Camera.unlock()}</li>
89399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>Configure {@link android.media.MediaRecorder} as shown in the code example above</li>
89499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>Start recording using {@link android.media.MediaRecorder#start()
89599b70f3f5d051261229d1792c169a374fc23326bJoe FernandezMediaRecorder.start()}</li>
89699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>Record the video</li>
89799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>Stop recording using {@link
89899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.media.MediaRecorder#stop() MediaRecorder.stop()}</li>
89999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>Release the media recorder with {@link android.media.MediaRecorder#release()
90099b70f3f5d051261229d1792c169a374fc23326bJoe FernandezMediaRecorder.release()}</li>
90199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li>Lock the camera using {@link android.hardware.Camera#lock() Camera.lock()}</li>
90299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ol>
90399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
90499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The following example code demonstrates how to wire up a button to properly start and stop
90599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezvideo recording using the camera and the {@link android.media.MediaRecorder} class.</p>
90699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
90799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="note"><strong>Note:</strong> When completing a video recording, do not release the camera
90899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezor else your preview will be stopped.</p>
90999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
91099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
91199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate boolean isRecording = false;
91299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
91399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez// Add a listener to the Capture button
91499b70f3f5d051261229d1792c169a374fc23326bJoe FernandezButton captureButton = (Button) findViewById(id.button_capture);
91599b70f3f5d051261229d1792c169a374fc23326bJoe FernandezcaptureButton.setOnClickListener(
91699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    new View.OnClickListener() {
91799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        &#64;Override
91899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        public void onClick(View v) {
91999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            if (isRecording) {
92099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                // stop recording and release camera
92199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                mMediaRecorder.stop();  // stop the recording
92299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                releaseMediaRecorder(); // release the MediaRecorder object
92399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                mCamera.lock();         // take camera access back from MediaRecorder
92499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
92599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                // inform the user that recording has stopped
92699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                setCaptureButtonText("Capture");
92799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                isRecording = false;
92899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            } else {
92999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                // initialize video camera
93099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                if (prepareVideoRecorder()) {
93199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                    // Camera is available and unlocked, MediaRecorder is prepared,
93299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                    // now you can start recording
93399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                    mMediaRecorder.start();
93499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
93599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                    // inform the user that recording has started
93699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                    setCaptureButtonText("Stop");
93799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                    isRecording = true;
93899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                } else {
93999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                    // prepare didn't work, release the camera
94099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                    releaseMediaRecorder();
94199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                    // inform user
94299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez                }
94399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            }
94499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
94599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
94699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez);
94799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
94899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
94999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="note"><strong>Note:</strong> In the above example, the {@code prepareVideoRecorder()}
95099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezmethod refers to the example code shown in <a
95199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhref="#configuring-mediarecorder">Configuring MediaRecorder</a>. This method takes care of locking
95299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezthe camera, configuring and preparing the {@link android.media.MediaRecorder} instance.</p>
95399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
95499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
95599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h3 id="release-camera">Releasing the camera</h3>
95699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Cameras are a resource that is shared by applications on a device. Your application can make
95799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezuse of the camera after getting an instance of {@link android.hardware.Camera}, and you must be
95899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezparticularly careful to release the camera object when your application stops using it, and as
95999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezsoon as your application is paused ({@link android.app.Activity#onPause() Activity.onPause()}). If
96099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezyour application does not properly release the camera, all subsequent attempts to access the camera,
96199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezincluding those by your own application, will fail and may cause your or other applications to be
96299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezshut down.</p>
96399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
96499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>To release an instance of the {@link android.hardware.Camera} object, use the {@link
96599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.hardware.Camera#release() Camera.release()} method, as shown in the example code below.</p>
96699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
96799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
96899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpublic class CameraActivity extends Activity {
96999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    private Camera mCamera;
97099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    private SurfaceView mPreview;
97199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    private MediaRecorder mMediaRecorder;
97299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
97399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    ...
974452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
97599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    &#64;Override
97699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    protected void onPause() {
97799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        super.onPause();
97899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        releaseMediaRecorder();       // if you are using MediaRecorder, release it first
97999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        releaseCamera();              // release the camera immediately on pause event
98099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
98199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
98299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    private void releaseMediaRecorder(){
98399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        if (mMediaRecorder != null) {
98499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mMediaRecorder.reset();   // clear recorder configuration
98599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mMediaRecorder.release(); // release the recorder object
98699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mMediaRecorder = null;
98799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mCamera.lock();           // lock camera for later use
98899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
98999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
99099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
99199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    private void releaseCamera(){
99299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        if (mCamera != null){
99399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mCamera.release();        // release the camera for other applications
99499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            mCamera = null;
99599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
99699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
99799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
99899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
99999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
100099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="caution"><strong>Caution:</strong> If your application does not properly release the
100199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcamera, all subsequent attempts to access the camera, including those by your own application, will
100299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezfail and may cause your or other applications to be shut down.</p>
100399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
100499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
100599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<h2 id="saving-media">Saving Media Files</h2>
100699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>Media files created by users such as pictures and videos should be saved to a device's external
100799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezstorage directory (SD Card) to conserve system space and to allow users to access these files
100899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezwithout their device. There are many possible directory locations to save media files on a device,
100999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhowever there are only two standard locations you should consider as a developer:</p>
101099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
101199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<ul>
101299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>{@link android.os.Environment#getExternalStoragePublicDirectory(java.lang.String)
101399b70f3f5d051261229d1792c169a374fc23326bJoe FernandezEnvironment.getExternalStoragePublicDirectory}({@link android.os.Environment#DIRECTORY_PICTURES
101499b70f3f5d051261229d1792c169a374fc23326bJoe FernandezEnvironment.DIRECTORY_PICTURES})</strong> - This method returns the standard, shared and recommended
101599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezlocation for saving pictures and videos. This directory is shared (public), so other applications
101699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezcan easily discover, read, change and delete files saved in this location. If your application is
101799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezuninstalled by the user, media files saved to this location will not be removed. To avoid
101899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezinterfering with users existing pictures and videos, you should create a sub-directory for your
101999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezapplication's media files within this directory, as shown in the code sample below. This method is
102099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezavailable in Android 2.2 (API Level 8), for equivalent calls in earlier API versions, see <a
102199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhref="{@docRoot}guide/topics/data/data-storage.html#SavingSharedFiles">Saving Shared Files</a>.</li>
102299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez  <li><strong>{@link android.content.Context#getExternalFilesDir(java.lang.String)
102399b70f3f5d051261229d1792c169a374fc23326bJoe FernandezContext.getExternalFilesDir}({@link android.os.Environment#DIRECTORY_PICTURES
102499b70f3f5d051261229d1792c169a374fc23326bJoe FernandezEnvironment.DIRECTORY_PICTURES})</strong> - This method returns a standard location for saving
102599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpictures and videos which are associated with your application. If your application is uninstalled,
102699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezany files saved in this location are removed. Security is not enforced for files in this
102799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezlocation and other applications may read, change and delete them.</li>
102899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</ul>
102999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
103099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>The following example code demonstrates how to create a {@link java.io.File} or {@link
103199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.net.Uri} location for a media file that can be used when invoking a device's camera with
103299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezan {@link android.content.Intent} or as part of a <a href="#custom-camera">Building a Camera
103399b70f3f5d051261229d1792c169a374fc23326bJoe FernandezApp</a>.</p>
103499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
103599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<pre>
103699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpublic static final int MEDIA_TYPE_IMAGE = 1;
103799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezpublic static final int MEDIA_TYPE_VIDEO = 2;
103899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
103999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez/** Create a file Uri for saving an image or video */
104099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezprivate static Uri getOutputMediaFileUri(int type){
104199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez      return Uri.fromFile(getOutputMediaFile(type));
104299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
104399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
104499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez/** Create a File for saving an image or video */
10451d56f41c9d1dcccf1faef343047375cb14799678Bill Gruberprivate static File getOutputMediaFile(int type){
104699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // To be safe, you should check that the SDCard is mounted
104799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // using Environment.getExternalStorageState() before doing this.
104899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
104999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
105099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez              Environment.DIRECTORY_PICTURES), "MyCameraApp");
105199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // This location works best if you want the created images to be shared
105299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // between applications and persist after your app has been uninstalled.
105399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
105499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // Create the storage directory if it does not exist
105599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    if (! mediaStorageDir.exists()){
105699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        if (! mediaStorageDir.mkdirs()){
105799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            Log.d("MyCameraApp", "failed to create directory");
105899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez            return null;
105999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        }
106099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
106199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
106299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    // Create a media file name
106399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
106499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    File mediaFile;
106599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    if (type == MEDIA_TYPE_IMAGE){
106699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
106799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        "IMG_"+ timeStamp + ".jpg");
106899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    } else if(type == MEDIA_TYPE_VIDEO) {
106999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
107099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        "VID_"+ timeStamp + ".mp4");
107199b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    } else {
107299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez        return null;
107399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    }
107499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
107599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez    return mediaFile;
107699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez}
107799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez</pre>
107899b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
107999b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p class="note"><strong>Note:</strong> {@link
108099b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.os.Environment#getExternalStoragePublicDirectory(java.lang.String)
108199b70f3f5d051261229d1792c169a374fc23326bJoe FernandezEnvironment.getExternalStoragePublicDirectory()} is available in Android 2.2 (API Level 8) or
108299b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhigher. If you are targeting devices with earlier versions of Android, use {@link
108399b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezandroid.os.Environment#getExternalStorageDirectory() Environment.getExternalStorageDirectory()}
108499b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezinstead. For more information, see <a
108599b70f3f5d051261229d1792c169a374fc23326bJoe Fernandezhref="{@docRoot}guide/topics/data/data-storage.html#SavingSharedFiles">Saving Shared Files</a>.</p>
108699b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez
108799b70f3f5d051261229d1792c169a374fc23326bJoe Fernandez<p>For more information about saving files on an Android device, see <a
1088452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezhref="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a>.</p>
1089452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1090452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1091452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<h2 id="camera-features">Camera Features</h2>
1092452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>Android supports a wide array of camera features you can control with your camera application,
1093452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezsuch as picture format, flash mode, focus settings, and many more. This section lists the common
1094452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezcamera features, and briefly discusses how to use them. Most camera features can be accessed and set
1095452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezusing the through {@link android.hardware.Camera.Parameters} object. However, there are several
1096452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezimportant features that require more than simple settings in {@link
1097452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezandroid.hardware.Camera.Parameters}. These features are covered in the following sections:<p>
1098452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1099452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<ul>
1100452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li><a href="#metering-focus-areas">Metering and focus areas</a></li>
1101452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li><a href="#face-detection">Face detection</a></li>
1102452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li><a href="#time-lapse-video">Time lapse video</a></li>
1103452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</ul>
1104452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1105452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>For general information about how to use features that are controlled through {@link
1106452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezandroid.hardware.Camera.Parameters}, review the <a href="#using-features">Using camera
1107452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezfeatures</a> section. For more detailed information about how to use features controlled through the
1108452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezcamera parameters object, follow the links in the feature list below to the API reference
1109452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezdocumentation.</p>
1110452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1111452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p class="table-caption" id="table1">
1112452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <strong>Table 1.</strong> Common camera features sorted by the Android API Level in which they
1113452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezwere introduced.</p>
1114452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<table>
1115452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1116452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <th>Feature</th>  <th>API Level</th>  <th>Description</th>
1117452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1118452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1119452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td><a href="#face-detection">Face Detection</a></td>
1120452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>14</td>
1121452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Identify human faces within a picture and use them for focus, metering and white
1122452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezbalance</td>
1123452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1124452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1125452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td><a href="#metering-focus-areas">Metering Areas</a></td>
1126452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>14</td>
1127452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Specify one or more areas within an image for calculating white balance</td>
1128452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1129452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1130452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td><a href="#metering-focus-areas">Focus Areas</a></td>
1131452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>14</td>
1132452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Set one or more areas within an image to use for focus</td>
1133452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1134452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1135452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setAutoWhiteBalanceLock White Balance Lock}</td>
1136452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>14</td>
1137452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Stop or start automatic white balance adjustments</td>
1138452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1139452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1140452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setAutoExposureLock Exposure Lock}</td>
1141452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>14</td>
1142452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Stop or start automatic exposure adjustments</td>
1143452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1144452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1145452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera#takePicture Video Snapshot}</td>
1146452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>14</td>
1147452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Take a picture while shooting video (frame grab)</td>
1148452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1149452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1150452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td><a href="#time-lapse-video">Time Lapse Video</a></td>
1151452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>11</td>
1152452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Record frames with set delays to record a time lapse video</td>
1153452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1154452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1155452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera#open(int) Multiple Cameras}</td>
1156452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>9</td>
1157452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Support for more than one camera on a device, including front-facing and back-facing
1158452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezcameras</td>
1159452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1160452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1161452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#getFocusDistances Focus Distance}</td>
1162452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>9</td>
1163452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Reports distances between the camera and objects that appear to be in focus</td>
1164452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1165452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1166452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setZoom Zoom}</td>
1167452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>8</td>
1168452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Set image magnification</td>
1169452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1170452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1171452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setExposureCompensation Exposure
1172452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezCompensation}</td>
1173452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>8</td>
1174452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Increase or decrease the light exposure level</td>
1175452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1176452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1177452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setGpsLatitude GPS Data}</td>
1178452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>5</td>
1179452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Include or omit geographic location data with the image</td>
1180452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1181452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1182452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setWhiteBalance White Balance}</td>
1183452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>5</td>
1184452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Set the white balance mode, which affects color values in the captured image</td>
1185452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1186452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1187452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setFocusMode Focus Mode}</td>
1188452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>5</td>
1189452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Set how the camera focuses on a subject such as automatic, fixed, macro or infinity</td>
1190452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1191452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1192452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setSceneMode Scene Mode}</td>
1193452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>5</td>
1194452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Apply a preset mode for specific types of photography situations such as night, beach, snow
1195452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezor candlelight scenes</td>
1196452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1197452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1198452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setJpegQuality JPEG Quality}</td>
1199452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>5</td>
1200452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Set the compression level for a JPEG image, which increases or decreases image output file
1201452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezquality and size</td>
1202452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1203452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1204452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setFlashMode Flash Mode}</td>
1205452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>5</td>
1206452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Turn flash on, off, or use automatic setting</td>
1207452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1208452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1209452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setColorEffect Color Effects}</td>
1210452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>5</td>
1211452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Apply a color effect to the captured image such as black and white, sepia tone or negative.
1212452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</td>
1213452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1214452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1215452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setAntibanding Anti-Banding}</td>
1216452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>5</td>
1217452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Reduces the effect of banding in color gradients due to JPEG compression</td>
1218452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1219452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1220452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setPictureFormat Picture Format}</td>
1221452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>1</td>
1222452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Specify the file format for the picture</td>
1223452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1224452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <tr>
1225452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>{@link android.hardware.Camera.Parameters#setPictureSize Picture Size}</td>
1226452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>1</td>
1227452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    <td>Specify the pixel dimensions of the saved picture</td>
1228452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  </tr>
1229452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</table>
1230452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1231452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p class="note"><strong>Note:</strong> These features are not supported on all devices due to
1232452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezhardware differences and software implementation. For information on checking the availability
1233452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezof features on the device where your application is running, see <a href="#check-feature">Checking
1234452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezfeature availability</a>.</p>
1235452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1236452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1237452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<h3 id="check-feature">Checking feature availability</h3>
1238452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>The first thing to understand when setting out to use camera features on Android devices is that
1239452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandeznot all camera features are supported on all devices. In addition, devices that support a particular
1240452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezfeature may support them to different levels or with different options. Therefore, part of your
1241452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezdecision process as you develop a camera application is to decide what camera features you want to
1242452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezsupport and to what level. After making that decision, you should plan on including code in your
1243452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezcamera application that checks to see if device hardware supports those features and fails
1244452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezgracefully if a feature is not available.</p>
1245452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1246452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>You can check the availabilty of camera features by getting an instance of a camera’s parameters
1247452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezobject, and checking the relevant methods. The following code sample shows you how to obtain a
1248452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera.Parameters} object and check if the camera supports the autofocus
1249452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezfeature:</p>
1250452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1251452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<pre>
1252452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez// get Camera parameters
1253452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezCamera.Parameters params = mCamera.getParameters();
1254452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1255452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezList&lt;String&gt; focusModes = params.getSupportedFocusModes();
1256452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezif (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
1257452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  // Autofocus mode is supported
1258452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez}
1259452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</pre>
1260452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1261452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>You can use the technique shown above for most camera features. The
1262452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera.Parameters} object provides a {@code getSupported...()}, {@code
1263452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezis...Supported()} or {@code getMax...()} method to determine if (and to what extent) a feature is
1264452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezsupported.</p>
1265452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1266452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>If your application requires certain camera features in order to function properly, you can
1267452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezrequire them through additions to your application manifest. When you declare the use of specific
12684d7bc65538c7cd9fbb1fbbcf22d1da47fcee1219Dirk Doughertycamera features, such as flash and auto-focus, Google Play restricts your application from
1269452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezbeing installed on devices which do not support these features. For a list of camera features that
1270452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezcan be declared in your app manifest, see the manifest
1271452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<a href="{@docRoot}guide/topics/manifest/uses-feature-element.html#hw-features"> Features
1272452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezReference</a>.</p>
1273452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1274452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<h3 id="using-features">Using camera features</h3>
1275452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>Most camera features are activated and controlled using a {@link
1276452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezandroid.hardware.Camera.Parameters} object. You obtain this object by first getting an instance of
1277452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezthe {@link android.hardware.Camera} object, calling the {@link
1278452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezandroid.hardware.Camera#getParameters getParameters()} method, changing the returned parameter
1279452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezobject and then setting it back into the camera object, as demonstrated in the following example
1280452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezcode:</p>
1281452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1282452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<pre>
1283452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez// get Camera parameters
1284452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezCamera.Parameters params = mCamera.getParameters();
1285452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez// set the focus mode
1286452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezparams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
1287452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez// set Camera parameters
1288452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezmCamera.setParameters(params);
1289452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</pre>
1290452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1291452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>This technique works for nearly all camera features, and most parameters can be changed at any
1292452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandeztime after you have obtained an instance of the {@link android.hardware.Camera} object. Changes to
1293452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezparameters are typically visible to the user immediately in the application’s camera preview.
1294452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezOn the software side, parameter changes may take several frames to actually take effect as the
1295452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezcamera hardware processes the new instructions and then sends updated image data.</p>
1296452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1297452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p class="caution"><strong>Important:</strong> Some camera features cannot be changed at will. In
1298452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezparticular, changing the size or orientation of the camera preview requires that you first stop the
1299452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezpreview, change the preview size, and then restart the preview. Starting with Android 4.0 (API
1300452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezLevel 14) preview orientation can be changed without restarting the preview.</p>
1301452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1302452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>Other camera features require more code in order to implement, including:</p>
1303452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<ul>
1304452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li>Metering and focus areas</li>
1305452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li>Face detection</li>
1306452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li>Time lapse video</li>
1307452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</ul>
1308452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>A quick outline of how to implement these features is provided in the following sections.</p>
1309452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1310452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1311452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<h3 id="metering-focus-areas">Metering and focus areas</h3>
1312452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>In some photographic scenarios, automatic focusing and light metering may not produce the
1313452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezdesired results. Starting with Android 4.0 (API Level 14), your camera application can provide
1314452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezadditional controls to allow your app or users to specify areas in an image to use for determining
1315452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezfocus or light level settings and pass these values to the camera hardware for use in capturing
1316452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezimages or video.</p>
1317452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1318452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>Areas for metering and focus work very similarly to other camera features, in that you control
1319452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezthem through methods in the {@link android.hardware.Camera.Parameters} object. The following code
1320452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezdemonstrates setting two light metering areas for an instance of
1321452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera}:</p>
1322452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1323452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<pre>
1324452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez// Create an instance of Camera
1325452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezmCamera = getCameraInstance();
1326452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1327452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez// set Camera parameters
1328452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezCamera.Parameters params = mCamera.getParameters();
1329452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1330452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezif (params.getMaxNumMeteringAreas() > 0){ // check that metering areas are supported
1331452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    List&lt;Camera.Area&gt; meteringAreas = new ArrayList&lt;Camera.Area&gt;();
1332452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1333452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    Rect areaRect1 = new Rect(-100, -100, 100, 100);    // specify an area in center of image
1334452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    meteringAreas.add(new Camera.Area(areaRect1, 600)); // set weight to 60%
1335452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    Rect areaRect2 = new Rect(800, -1000, 1000, -800);  // specify an area in upper right of image
1336452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    meteringAreas.add(new Camera.Area(areaRect2, 400)); // set weight to 40%
1337452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    params.setMeteringAreas(meteringAreas);
1338452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez}
1339452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1340452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezmCamera.setParameters(params);
1341452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</pre>
1342452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1343452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>The {@link android.hardware.Camera.Area} object contains two data parameters: A {@link
1344452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezandroid.graphics.Rect} object for specifying an area within the camera’s field of view and a weight
1345452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezvalue, which tells the camera what level of importance this area should be given in light metering
1346452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezor focus calculations.</p>
1347452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1348452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>The {@link android.graphics.Rect} field in a {@link android.hardware.Camera.Area} object
1349452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezdescribes a rectangular shape mapped on a 2000 x 2000 unit grid. The coordinates -1000, -1000
1350452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezrepresent the top, left corner of the camera image, and coordinates 1000, 1000 represent the
1351452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezbottom, right corner of the camera image, as shown in the illustration below.</p>
1352452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1353452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<img src='images/camera-area-coordinates.png' />
1354452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p class="img-caption">
1355452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <strong>Figure 1.</strong> The red lines illustrate the coordinate system for specifying a
1356452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera.Area} within a camera preview. The blue box shows the location and
1357452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezshape of an camera area with the {@link android.graphics.Rect} values 333,333,667,667.
1358452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</p>
1359452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1360452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>The bounds of this coordinate system always correspond to the outer edge of the image visible in
1361452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezthe camera preview and do not shrink or expand with the zoom level. Similarly, rotation of the image
1362452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezpreview using {@link android.hardware.Camera#setDisplayOrientation Camera.setDisplayOrientation()}
1363452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezdoes not remap the coordinate system.</p>
1364452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1365452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1366452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<h3 id="face-detection">Face detection</h3>
1367452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>For pictures that include people, faces are usually the most important part of the picture, and
1368452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezshould be used for determining both focus and white balance when capturing an image. The Android 4.0
1369452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez(API Level 14) framework provides APIs for identifying faces and calculating picture settings using
1370452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezface recognition technology.</p>
1371452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1372452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p class="note"><strong>Note:</strong> While the face detection feature is running,
1373452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera.Parameters#setWhiteBalance},
1374452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera.Parameters#setFocusAreas} and
1375452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera.Parameters#setMeteringAreas} have no effect.</p>
1376452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1377452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>Using the face detection feature in your camera application requires a few general steps:</p>
1378452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<ul>
1379452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li>Check that face detection is supported on the device</li>
1380452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li>Create a face detection listener</li>
1381452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li>Add the face detection listener to your camera object</li>
1382452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez  <li>Start face detection after preview (and after <em>every</em> preview restart)</li>
1383452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</ul>
1384452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1385452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>The face detection feature is not supported on all devices. You can check that this feature is
1386452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezsupported by calling {@link android.hardware.Camera.Parameters#getMaxNumDetectedFaces}. An
1387452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezexample of this check is shown in the {@code startFaceDetection()} sample method below.</p>
1388452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1389452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>In order to be notified and respond to the detection of a face, your camera application must set
1390452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandeza listener for face detection events. In order to do this, you must create a listener class that
1391452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezimplements the {@link android.hardware.Camera.FaceDetectionListener} interface as shown in the
1392452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezexample code below.</p>
1393452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1394452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<pre>
1395452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezclass MyFaceDetectionListener implements Camera.FaceDetectionListener {
1396452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1397452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    &#064;Override
1398452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    public void onFaceDetection(Face[] faces, Camera camera) {
1399452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        if (faces.length > 0){
1400452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez            Log.d("FaceDetection", "face detected: "+ faces.length +
1401452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez                    " Face 1 Location X: " + faces[0].rect.centerX() +
1402452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez                    "Y: " + faces[0].rect.centerY() );
1403452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        }
1404452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    }
1405452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez}
1406452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</pre>
1407452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1408452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>After creating this class, you then set it into your application’s
1409452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera} object, as shown in the example code below:</p>
1410452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1411452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<pre>
1412452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezmCamera.setFaceDetectionListener(new MyFaceDetectionListener());
1413452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</pre>
1414452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1415452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>Your application must start the face detection function each time you start (or restart) the
1416452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezcamera preview. Create a method for starting face detection so you can call it as needed, as shown
1417452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezin the example code below.</p>
1418452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1419452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<pre>
1420452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezpublic void startFaceDetection(){
1421452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    // Try starting Face Detection
1422452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    Camera.Parameters params = mCamera.getParameters();
1423452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1424452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    // start face detection only *after* preview has started
1425452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    if (params.getMaxNumDetectedFaces() > 0){
1426452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        // camera supports face detection, so can start it:
1427452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        mCamera.startFaceDetection();
1428452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    }
1429452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez}
1430452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</pre>
1431452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1432452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>You must start face detection <em>each time</em> you start (or restart) the camera preview. If
1433452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezyou use the preview class shown in <a href="#camera-preview">Creating a preview class</a>, add your
1434452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera#startFaceDetection startFaceDetection()} method to both the
1435452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.view.SurfaceHolder.Callback#surfaceCreated surfaceCreated()} and {@link
1436452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezandroid.view.SurfaceHolder.Callback#surfaceChanged surfaceChanged()} methods in your preview class,
1437452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezas shown in the sample code below.</p>
1438452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1439452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<pre>
1440452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezpublic void surfaceCreated(SurfaceHolder holder) {
1441452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    try {
1442452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        mCamera.setPreviewDisplay(holder);
1443452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        mCamera.startPreview();
1444452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1445452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        startFaceDetection(); // start face detection feature
1446452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1447452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    } catch (IOException e) {
1448452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        Log.d(TAG, "Error setting camera preview: " + e.getMessage());
1449452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    }
1450452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez}
1451452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1452452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezpublic void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
1453452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1454452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    if (mHolder.getSurface() == null){
1455452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        // preview surface does not exist
1456452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        Log.d(TAG, "mHolder.getSurface() == null");
1457452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        return;
1458452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    }
1459452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1460452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    try {
1461452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        mCamera.stopPreview();
1462452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1463452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    } catch (Exception e){
1464452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        // ignore: tried to stop a non-existent preview
1465452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        Log.d(TAG, "Error stopping camera preview: " + e.getMessage());
1466452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    }
1467452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1468452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    try {
1469452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        mCamera.setPreviewDisplay(mHolder);
1470452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        mCamera.startPreview();
1471452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1472452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        startFaceDetection(); // re-start face detection feature
1473452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1474452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    } catch (Exception e){
1475452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        // ignore: tried to stop a non-existent preview
1476452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
1477452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez    }
1478452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez}
1479452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</pre>
1480452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1481452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p class="note"><strong>Note:</strong> Remember to call this method <em>after</em> calling
1482452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez{@link android.hardware.Camera#startPreview startPreview()}. Do not attempt to start face detection
1483452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezin the {@link android.app.Activity#onCreate onCreate()} method of your camera app’s main activity,
1484452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezas the preview is not available by this point in your application's the execution.</p>
1485452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1486452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1487452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<h3 id="time-lapse-video">Time lapse video</h3>
1488452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>Time lapse video allows users to create video clips that combine pictures taken a few seconds or
1489452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezminutes apart. This feature uses {@link android.media.MediaRecorder} to record the images for a time
1490452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezlapse sequence. </p>
1491452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1492452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>To record a time lapse video with {@link android.media.MediaRecorder}, you must configure the
1493452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezrecorder object as if you are recording a normal video, setting the captured frames per second to a
1494452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezlow number and using one of the time lapse quality settings, as shown in the code example below.</p>
1495452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1496452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<pre>
1497452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
1498452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezmMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_HIGH));
1499452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez...
1500452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez// Step 5.5: Set the video capture rate to a low number
1501452e41f60754f9cd546e308e291ecda6f0f02090Joe FernandezmMediaRecorder.setCaptureRate(0.1); // capture a frame every 10 seconds
1502452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez</pre>
1503452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez
1504452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandez<p>These settings must be done as part of a larger configuration procedure for {@link
1505452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezandroid.media.MediaRecorder}. For  a full configuration code example, see <a
1506452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezhref="#configuring-mediarecorder">Configuring MediaRecorder</a>. Once the configuration is complete,
1507452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezyou start the video recording as if you were recording a normal video clip. For more information
1508452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezabout configuring and running {@link android.media.MediaRecorder}, see <a
1509452e41f60754f9cd546e308e291ecda6f0f02090Joe Fernandezhref="#capture-video">Capturing videos</a>.</p>
1510