In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use amera2 Preview in Android development, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Preface
Camera2 is the new Camera framework of Android. On the whole, Camera2 provides many standard interfaces for applications, so that more functions can be controlled by parameters; however, flexibility also brings architectural complexity. This article discusses the minimum set of modules used in Camera2 by discussing the implementation of the Preview function of Camera2.
1. Which modules are needed for Camera2 Preview
To sum up, the following modules are used
SurfaceTextureListener of SurfaceTexture
CameraManager
StateCallback of CameraDevice
CreateCaptureSession of CameraDevice
CaptureRequest.Builder 、 CaptureRequest
Second, the function of each module and the relationship between them
The functions of these modules and the relationship between them are analyzed in detail below
2.1 SurfaceTextureListener2.1.1 of SurfaceTexture first take a look at the instructions on SurfaceTexture
The main purpose is to receive the data of camera preview and display it on UI.
Captures frames from an image stream as an OpenGL ES texture.
Capture frames from the image stream as OpenGL ES textures.
The image stream may come from either camera preview or video decode. An android.view.Surface created from a SurfaceTexture can be used as an output destination for the android.hardware.camera2, android.media.MediaCodec, android.media.MediaPlayer, and android.renderscript.Allocation APIs. When updateTexImage is called, the contents of the texture object specified when the SurfaceTexture was created are updated to contain the most recent image from the image stream. This may cause some frames of the stream to be skipped.
2.1.2 use of SurfaceTextureListener
You can use this Surface Texture to be notified when the surface texture associated with this Listener is available.
When you receive a notification that the SurfaceTexture is available, the action of initializing the camera is performed.
The sample code is as follows
Private final SurfaceTextureListener mSurfaceTextureListener = new SurfaceTextureListener () {@ Override public void onSurfaceTextureAvailable (@ NonNull SurfaceTexture surface, int width, int height) {Log.i (TAG, "onSurfaceTextureAvailable: + +"); try {Log.i (TAG, "onCreate: call initCamera ()"); initCamera ();} catch (CameraAccessException e) {e.printStackTrace () } @ Override public void onSurfaceTextureSizeChanged (@ NonNull SurfaceTexture surface, int width, int height) {Log.i (TAG, "onSurfaceTextureSizeChanged: + +");} @ Override public boolean onSurfaceTextureDestroyed (@ NonNull SurfaceTexture surface) {Log.i (TAG, "onSurfaceTextureDestroyed: + +"); return false } @ Override public void onSurfaceTextureUpdated (@ NonNull SurfaceTexture surface) {Log.i (TAG, "onSurfaceTextureUpdated: + +");}}; 2.2 the role of CameraManager2.2.1 CameraManager
A system service manager for detecting, characterizing, and connecting to CameraDevices.
A system service manager for detecting, characterizing, and connecting imaging devices.
2.2.2 Open Camera using CameraManager
The sample code is as follows
Note:
The first parameter of openCamera is which camera to turn on. 0 represents the rear camera, 1 represents the front camera, and 2 represents the external camera. What's on here is the front camera.
The second parameter is the StateCallback of CameraDevice, which will be parsed later
The third parameter is Handler, where we put Handler in a newly created thread
CameraManager manager = (CameraManager) this.getSystemService (Context.CAMERA_SERVICE); manager.openCamera ("1", stateCallback, mBackgroundHander); 2.3The role of StateCallback2.3.1 StateCallback of CameraDevice
A callback objects for receiving updates about the state of a camera device.
A callback object used to receive status updates of the camera device.
2.3.2 sample code for StateCallback
We will create a Preview Session in the onOpened () function!
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback () {@ Override public void onOpened (@ NonNull CameraDevice camera) {Log.i (TAG, "onOpened: + +"); mCameraDevice = camera; createPreviewSession () } @ Override public void onDisconnected (@ NonNull CameraDevice camera) {Log.i (TAG, "onDisconnected: + +");} @ Override public void onError (@ NonNull CameraDevice camera, int error) {Log.i (TAG, "onError: +") }}; 2.4createCaptureSession2.4.1 of CameraDevice create a new CaptureRequest.Builder
A builder for capture requests.
2.4.2 createCaptureSession of the new CameraDevice
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.
2.4.3 create CaptureRequest
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.
The sample code for this section is as follows
Private void createPreviewSession () {SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture (); / / should be preview size, will got it later surfaceTexture.setDefaultBufferSize (mTextureView.getWidth (), mTextureView.getHeight ()); mImageReader = ImageReader.newInstance (mTextureView.getWidth (), mTextureView.getHeight (), ImageFormat.JPEG, / * maxImages*/2); Surface surface = new Surface (surfaceTexture) Try {mPreviewRequstBuilder = mCameraDevice.createCaptureRequest (CameraDevice.TEMPLATE_PREVIEW); mPreviewRequstBuilder.addTarget (surface) MCameraDevice.createCaptureSession (Arrays.asList (surface, mImageReader.getSurface ()), new CameraCaptureSession.StateCallback () {@ Override public void onConfigured (@ NonNull CameraCaptureSession session) {Log.i (TAG, "onConfigured:") If (mCameraDevice = = null) {return;} else {mCaptureSession = session;} mPreviewRequest = mPreviewRequstBuilder.build () Try {mCaptureSession.setRepeatingRequest (mPreviewRequest, null, mBackgroundHander);} catch (CameraAccessException e) {e.printStackTrace () } @ Override public void onConfigureFailed (@ NonNull CameraCaptureSession session) {Log.i (TAG, "onConfigureFailed:");}}, mBackgroundHander) } catch (CameraAccessException e) {e.printStackTrace ();}} these are all the contents of the article "how to use amera2 Preview in Android Development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.