In this article, I will introduce a very important component in CameraX, which is Vendor Extensions .
1. Introduction
In recent phone introductions of Android phone manufacturers such as Samsung or Google and Huawei, you are no stranger to hegemony photography features. HDR , Bokeh , Night , Beauty , Auto . In addition to the hardware system including a set of conventional cameras, telephoto cameras, wide angle cameras or macro cameras, the built-in software also makes the images more magical.
In cooperation with some original equipment manufacturers (OEMs) such as LG and Samsung; Google has developed an extension feature for mobile cameras. CameraX offers a number of API extensions like Bokeh, HDR and some features implemented by each manufacturer specifically for their device.
Devices that support vendor extensions must meet the following conditions:
- Effects must have support library from device manufacturer (OEM)
- OEM library must be installed on current device
- The OEM library reports the device that supports the extension
- The device has the operating system version the library requires
2. Extension architecture
Extensions are separated from Camera2 core of CameraX. In the picture, the red arrow lines show the path when a user activates an extension feature (eg HDR)
3. Implementation
To use extensions, you must implement the support library:
1 2 | implementation <span class="token string">"androidx.camera:camera-extensions:1.0.0-alpha08"</span> |
To use the extension in CameraX, you must create the corresponding Extender object that suits your needs:
- AutoImageCaptureExtender
- BeautyImageCaptureExtender
- BokehImageCaptureExtender
- HdrImageCaptureExtender
- NightImageCaptureExtender
To enable extension, use the enableExtension () method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | val cameraSelector <span class="token operator">=</span> CameraSelector <span class="token punctuation">.</span> <span class="token function">Builder</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token function">requireLensFacing</span> <span class="token punctuation">(</span> CameraSelector <span class="token punctuation">.</span> LENS_FACING_BACK <span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token function">build</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> val imageCaptureBuilder <span class="token operator">=</span> ImageCapture <span class="token punctuation">.</span> <span class="token function">Builder</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> val bokehImageCapture <span class="token operator">=</span> BokehImageCaptureExtender <span class="token punctuation">.</span> <span class="token function">create</span> <span class="token punctuation">(</span> imageCaptureBuilder <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> bokehImageCapture <span class="token punctuation">.</span> <span class="token function">isExtensionAvailable</span> <span class="token punctuation">(</span> cameraSelector <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> bokehImageCapture <span class="token punctuation">.</span> <span class="token function">enableExtension</span> <span class="token punctuation">(</span> cameraSelector <span class="token punctuation">)</span> Log <span class="token punctuation">.</span> <span class="token function">d</span> <span class="token punctuation">(</span> TAG <span class="token punctuation">,</span> <span class="token string">"Bokeh extension is enabled"</span> <span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> Log <span class="token punctuation">.</span> <span class="token function">d</span> <span class="token punctuation">(</span> TAG <span class="token punctuation">,</span> <span class="token string">"Sorry! This device didn't support Bokeh extension"</span> <span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span> <span class="token class-name">e</span> <span class="token operator">:</span> Exception <span class="token punctuation">)</span> <span class="token punctuation">{</span> Log <span class="token punctuation">.</span> <span class="token function">d</span> <span class="token punctuation">(</span> TAG <span class="token punctuation">,</span> e <span class="token punctuation">.</span> message <span class="token punctuation">)</span> <span class="token punctuation">}</span> |
In the above code, I have created cameraSelector to take photos from the rear camera LENS_FACING_BACK and use the Bokeh effect when the device supports it. If you want to use other effects, you absolutely can with the Extender object I listed above.
Refer to https://developer.android.com/training/camerax/vendor-extensions