A configured capture session for a CameraDevice, used for capturing images from the camera or reprocessing images captured from the camera in the same session previously
A representation of a single camera connected to an Android device
The properties describing a CameraDevice. These properties are fixed for a given CameraDevice, and can be queried through the CameraManager interface with getCameraCharacteristics(String)
A system service manager for detecting, characterizing, and connecting to CameraDevices. You can get an instance of this class by calling Context.getSystemService()
An immutable package of settings and outputs needed to capture a single image from the camera device. Contains the configuration for the capture hardware (sensor, lens, flash), the processing pipeline, the control algorithms, and the output buffers. Also contains the list of target Surfaces to send image data to for this capture. Can be created by using a CaptureRequest.Builder instance, obtained by calling createCaptureRequest(int)
The subset of the results of a single image capture from the image sensor. Contains a subset of the final configuration for the capture hardware (sensor, lens, flash), the processing pipeline, the control algorithms, and the output buffers. It is produced by a CameraDevice after processing a CaptureRequest
Camera2 APIs are available in API 21+ (Lollipop and beyond)
Even if an Android device has a 21+ ROM officially, there is no guarantee that it implements Camera2 APIs, it's totally up to the manufacturer to implement it or not (Example: LG G2 has official Lollipop support, but no Camera2 APIs)
With Camera2, Camera ("Camera1") is deprecated
With great power comes great responsability: It's easier to mess it up when using this APIs.
Remember, if you only want to take a photo in your app, and simply get it, you don't need to implement Camera2, you can open the device's camera app via an Intent, and receive it back
Preview the main camera in a TextureView
In this case, building against API 23, so permissions are handled too.
You must add in the Manifest the following permission (wherever the API level you're using):
We're about to create an activity (Camera2Activity.java) that fills a TextureView with the preview of the device's camera.
The Activity we're going to use is a typical AppCompatActivity:
Attributes (You may need to read the entire example to understand some of it)
The MAX_PREVIEW_SIZE guaranteed by Camera2 API is 1920x1080
TextureView.SurfaceTextureListener handles several lifecycle events on a
TextureView. In this case, we're listening to those events. When the SurfaceTexture is ready, we initialize the camera. When it size changes, we setup the preview coming from the camera accordingly
A CameraDevice represent one physical device's camera. In this attribute, we save the ID of the current CameraDevice
This is the view (TextureView) that we'll be using to "draw" the preview of the Camera
The CameraCaptureSession for camera preview
A reference to the opened CameraDevice
The Size of camera preview.
CameraDevice.StateCallback is called when CameraDevice changes its state
An additional thread for running tasks that shouldn't block the UI
A Handler for running tasks in the background
An ImageReader that handles still image capture
CaptureRequest.Builder for the camera preview
CaptureRequest generated by mPreviewRequestBuilder
A Semaphore to prevent the app from exiting before closing the camera.
Constant ID of the permission request
Android Lifecycle methods
Camera2 related methods
Those are methods that uses the Camera2 APIs
Closes the current camera
Sets up member variables related to camera
Creates a new CameraCaptureSession for camera preview
Permissions related methods For Android API 23+
Background thread / handler methods
Given choices of Sizes supported by a camera, choose the smallest one that is at least at large as the respective texture view size, and that is as most as large as the respective max size, and whose aspect ratio matches with the specified value. If doesn't exist, choose the largest one that is at most as large as the respective max size, and whose aspect ratio matches with the specified value
This method congfigures the neccesary Matrix transformation to mTextureView
This method compares two Sizes based on their areas.
Not much to see here
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0