1 page.title=Camera 2 @jd:body 3 4 <!-- 5 Copyright 2015 The Android Open Source Project 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 --> 19 <div id="qv-wrapper"> 20 <div id="qv"> 21 <h2>In this document</h2> 22 <ol id="auto-toc"> 23 </ol> 24 </div> 25 </div> 26 27 <img style="float: right; margin: 0px 15px 15px 15px;" src="images/ape_fwk_hal_camera.png" alt="Android Camera HAL icon"/> 28 29 <p>Android's camera Hardware Abstraction Layer (HAL) connects the higher level 30 camera framework APIs in <a href="http://developer.android.com/reference/android/hardware/package-summary.html">android.hardware</a> to your underlying camera driver and hardware. The camera subsystem includes implementations for camera pipeline components while the camera HAL provides interfaces for use in implementing your version of these components.</p> 31 32 <h2 id="architecture">Architecture</h2> 33 <p>The following figure and list describe the components involved and where to find the source for each: 34 </p> 35 36 <img src="images/ape_fwk_camera.png" alt="Android camera architecture" id="figure1" /> 37 <p class="img-caption"> 38 <strong>Figure 1.</strong> Camera architecture 39 </p> 40 41 <dl> 42 43 <dt>Application framework</dt> 44 <dd>At the application framework level is the app's code, which utilizes the <a 45 href="http://developer.android.com/reference/android/hardware/Camera.html">android.hardware.Camera</a> 46 API to interact with the camera hardware. Internally, this code calls a corresponding JNI glue class 47 to access the native code that interacts with the camera.</dd> 48 49 <dt>JNI</dt> 50 <dd>The JNI code associated with <a 51 href="http://developer.android.com/reference/android/hardware/Camera.html">android.hardware.Camera</a> is located in 52 <code>frameworks/base/core/jni/android_hardware_Camera.cpp</code>. This code calls the lower level 53 native code to obtain access to the physical camera and returns data that is used to create the 54 <a href="http://developer.android.com/reference/android/hardware/Camera.html">android.hardware.Camera</a> object at the framework level.</dd> 55 56 <dt>Native framework<dt> 57 <dd>The native framework defined in <code>frameworks/av/camera/Camera.cpp</code> provides a native equivalent 58 to the <a href="http://developer.android.com/reference/android/hardware/Camera.html">android.hardware.Camera</a> class. 59 This class calls the IPC binder proxies to obtain access to the camera service.</dd> 60 61 <dt>Binder IPC proxies</dt> 62 <dd>The IPC binder proxies facilitate communication over process boundaries. There are three camera binder 63 classes that are located in the <code>frameworks/av/camera</code> directory that calls into 64 camera service. ICameraService is the interface to the camera service, ICamera is the interface 65 to a specific opened camera device, and ICameraClient is the device's interface back to the application framework.</dd> 66 67 <dt>Camera service</dt> 68 <dd>The camera service, located in <code>frameworks/av/services/camera/libcameraservice/CameraService.cpp</code>, is the actual code that interacts with the HAL.</p> 69 70 <dt>HAL</dt> 71 <dd>The hardware abstraction layer defines the standard interface that the camera service calls into and that 72 you must implement to have your camera hardware function correctly. 73 </dd> 74 75 <dt>Kernel driver</dt> 76 <dd>The camera's driver interacts with the actual camera hardware and your implementation of the HAL. The 77 camera and driver must support YV12 and NV21 image formats to provide support for 78 previewing the camera image on the display and video recording.</dd> 79 </dl> 80 81 82 <h2 id="implementing">Implementing the HAL</h2> 83 <p>The HAL sits between the camera driver and the higher level Android framework 84 and defines an interface that you must implement so that apps can 85 correctly operate the camera hardware. The HAL interface is defined in the 86 <code>hardware/libhardware/include/hardware/camera.h</code> and 87 <code>hardware/libhardware/include/hardware/camera_common.h</code> header files. 88 </p> 89 90 <p> 91 <code>camera_common.h</code> defines an important struct, <code>camera_module</code>, which defines a standard 92 structure to obtain general information about the camera, such as its ID and properties 93 that are common to all cameras such as whether or not it is a front or back-facing camera. 94 </p> 95 96 <p> 97 <code>camera.h</code> contains the code that corresponds mainly with 98 <a href="http://developer.android.com/reference/android/hardware/Camera.html">android.hardware.Camera</a>. This header file declares a <code>camera_device</code> 99 struct that contains a<code>camera_device_ops</code> struct with function pointers 100 that point to functions that implement the HAL interface. For documentation on the 101 different types of camera parameters that a developer can set, 102 see the <code>frameworks/av/include/camera/CameraParameters.h</code> file. 103 These parameters are set with the function pointed to by 104 <code>int (*set_parameters)(struct camera_device *, const char *parms)</code> in the HAL. 105 </p> 106 107 <p>For an example of a HAL implementation, see the implementation for the Galaxy Nexus HAL in 108 <code>hardware/ti/omap4xxx/camera</code>.</p> 109 110 111 <h2 id="configuring">Configuring the Shared Library</h2> 112 <p>You need to set up the Android build system to 113 correctly package the HAL implementation into a shared library and copy it to the 114 appropriate location by creating an <code>Android.mk</code> file: 115 116 <ol> 117 <li>Create a <code>device/<company_name>/<device_name>/camera</code> directory to contain your 118 library's source files.</li> 119 <li>Create an <code>Android.mk</code> file to build the shared library. Ensure that the Makefile contains the following lines: 120 <pre> 121 LOCAL_MODULE := camera.<device_name> 122 LOCAL_MODULE_RELATIVE_PATH := hw 123 </pre> 124 <p>Notice that your library must be named <code>camera.<device_name></code> (<code>.so</code> is appended automatically), 125 so that Android can correctly load the library. For an example, see the Makefile 126 for the Galaxy Nexus camera located in <code>hardware/ti/omap4xxx/Android.mk</code>.</p> 127 128 </li> 129 <li>Specify that your device has camera features by copying the necessary feature XML files in the 130 <code>frameworks/native/data/etc</code> directory with your 131 device's Makefile. For example, to specify that your device has a camera flash and can autofocus, 132 add the following lines in your device's 133 <code><device>/<company_name>/<device_name>/device.mk</code> Makefile: 134 135 <pre class="no-pretty-print"> 136 PRODUCT_COPY_FILES := \ ... 137 138 PRODUCT_COPY_FILES += \ 139 frameworks/native/data/etc/android.hardware.camera.flash-autofocus.xml:system/etc/permissions/android.hardware.camera.flash-autofocus.xml \ 140 </pre> 141 142 <p>For an example of a device Makefile, see <code>device/samsung/tuna/device.mk</code>.</p> 143 </li> 144 145 <li>Declare your cameras media codec, format, and resolution capabilities in 146 <code>device/<company_name>/<device_name>/media_profiles.xml</code> and 147 <code>device/<company_name>/<device_name>/media_codecs.xml</code> XML files. 148 For more information, see <a href="{@docRoot}devices/media.html#expose"> Exposing 149 Codecs and Profiles to the Framework</a> for information on how to do this. 150 </p></code> 151 152 </li> 153 154 <li>Add the following lines in your device's 155 <code>device/<company_name>/<device_name>/device.mk</code> 156 Makefile to copy the <code>media_profiles.xml</code> 157 and <code>media_codecs.xml</code> files to the appropriate location: 158 <pre> 159 # media config xml file 160 PRODUCT_COPY_FILES += \ 161 <device>/<company_name>/<device_name>/media_profiles.xml:system/etc/media_profiles.xml 162 163 # media codec config xml file 164 PRODUCT_COPY_FILES += \ 165 <device>/<company_name>/<device_name>/media_codecs.xml:system/etc/media_codecs.xml 166 </pre> 167 </li> 168 169 <li> 170 <p>Declare that you want to include the Camera app in your device's system image by 171 specifying it in the <code>PRODUCT_PACKAGES</code> variable in your device's 172 <code>device/<company_name>/<device_name>/device.mk</code> 173 Makefile:</p> 174 <pre> 175 PRODUCT_PACKAGES := \ 176 Gallery2 \ 177 ... 178 </pre> 179 </li> 180 181 </ol> 182