content | Camera1.0 | Camera2.0 |
---|---|---|
Competence | android.permission.CAMERA | android.permission.CAMERA |
layout | SurfaceView | TextureView |
Implement an interface | SurfaceHolder.Callback 1.surfaceCreated 2.surfaceChanged 3.surfaceDestroyed | SurfaceTextureListener 1.onSurfaceTextureAvailable 2.onSurfaceTextureSizeChanged 3.onSurfaceTextureDestroyed 4.onSurfaceTextureUpdated |
Camera parameters (settings, view) | Camera.Parameters Camera.Size | See: CameraCharacteristics in getCameraCharacteristics (CameraID) provided: CaptureRequest.Builder the void set (Key key, T value) Example: Exposure: CaptureReqBuilder.set (CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, 2); |
Open the camera | surfaceCreated中Camera.open(CameraID) | onSurfaceTextureAvailable中CameraManager.openCamera(CameraId,CameraDevice.StateCallback,Handler) |
Start preview | Camera.startPreview() | CaptureReqBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);CaptureReqBuilder.addTarget(Surface);Camera.createCaptureSession(Arrays.asList(surface),CaptureSessionStateCallback, Handler); |
Set Preview direction | Camera.setDisplayOrientation(degrees) | And there is no direct way to set the preview direction, but TextureView itself is a View, support rotation, translation, scaling, and then rewrite method onMeasure |
The original image data byte [] real-time access | Camera.PreviewCallback中onPreviewFrame(byte[],Camera) | 1. onSurfaceTextureUpdated used TextureView of getBitmap () method, but to get here is the Bitmap object, but what I need is the raw byte [], so this method is not applicable. 2. Set ImageReader.setOnImageAvailableListener monitoring, method to get an Image object in onImageAvailable (ImageReader) passed through ImageReader.acquireLatestImage callback () (do not forget to close (), otherwise the screen will be stuck, stop refreshing), and then Image.getPlanes () [0] .getBuffer () returns a ByteBuffer objects, the new byte [buffer.remaining ()] to obtain the original image byte []. Do not forget to CaptureReqBuilder.addTarget (ImageReader.getSurface ()); otherwise do not see results |
Camera image preview size setting | Camera.Parameters.setPreviewSize(width, height) | TextureView. GetSurfaceTexture () to get SurfaceTexture () object through the setDefaultBufferSize (width, height) is set. |
In the future to get a picture size settings | Camera.Parameters.setPictureSize(width, height); | ImageReader.newInstance(width, height,ImageFormat.YUV_420_888, MAX_IMAGES); |
Formatting the future to get a picture of | Camera.Parameters..setPictureFormat(ImageFormat.JPEG); | ImageReader.newInstance(width, height,ImageFormat.YUV_420_888, MAX_IMAGES); |
-
Camera2 camera device object is to manage to get through the system CameraManager service, a preview of the camera, the camera is to request a session (CaptureSession.StateCallback, when the camera was turned on by the output surface of the device's camera) sends a request (CaptureRequest.Builder). It needs to be processed in the callback onConfigured, such as preview, if you write CameraCaptureSession.setRepeatingRequest (mCaptureReqBuilder.build (), null, mHandler) is not in this method; the preview will not be successful.
-
In addition, the session is created, set ImageReader listen, we need to pass a Handler object, the Handler object determines these sessions, the callback method listeners will be called in which thread, if passed is NULL, then the callback will be called in the current thread .
Flash lights control mode
Camera1:
This code can be a camera flash mode supported
List supportedFlashModes = params.getSupportedFlashModes ();
a method for controlling the flash:
params.setFlashMode (Parameters.FLASH_MODE_TORCH); // open flash
Parameters.FLASH_MODE_TORCH: flash normally open
Parameters.FLASH_MODE_ON: open only when the camera flash
Camera2:
This code can be used to detect whether the currently open camera with flash
boolean flashAvailable = cameraCharacteristics.get (CameraCharacteristics.FLASH_INFO_AVAILABLE);
a method for controlling the flash:
case 0:
mBtnFlash.setImageResource(R.drawable.btn_flash_off);
mPreviewBuilder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
mPreviewBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
break;
case 1:
mBtnFlash.setImageResource(R.drawable.btn_flash_on);
mPreviewBuilder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
mPreviewBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_SINGLE);
break;
case 2:
mBtnFlash.setImageResource(R.drawable.btn_flash_all_on);
mPreviewBuilder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
mPreviewBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
break;