mCameraManager.registerAvailabilityCallback(mAvailabilityCallback, mHandler); /** * Register a callback to be notified about camera device availability. * * @param callback the new callback to send camera availability notices to * @param handler The handler on which the callback should be invoked, or {@code null} to use * the current thread's {@link android.os.Looper looper}. */ public void registerAvailabilityCallback(@NonNull AvailabilityCallback callback,@Nullable Handler handler)
Android Marshmallow(6.0)及以上监听Torch可用回调:
1 2 3 4 5 6 7 8 9
mCameraManager.registerTorchCallback(mTorchCallback, mHandler); /** * Register a callback to be notified about torch mode status. * * @param callback The new callback to send torch mode status to * @param handler The handler on which the callback should be invoked, or {@code null} to use * the current thread's {@link android.os.Looper looper}. */ public void registerTorchCallback(@NonNull TorchCallback callback, @Nullable Handler handler)
/** * Open a connection to a camera with the given ID. * * <p>Once the camera is successfully opened, {@link CameraDevice.StateCallback#onOpened} will * be invoked with the newly opened {@link CameraDevice}. The camera device can then be set up * for operation by calling {@link CameraDevice#createCaptureSession} and * {@link CameraDevice#createCaptureRequest}</p> * * <!-- * <p>Since the camera device will be opened asynchronously, any asynchronous operations done * on the returned CameraDevice instance will be queued up until the device startup has * completed and the callback's {@link CameraDevice.StateCallback#onOpened onOpened} method is * called. The pending operations are then processed in order.</p> * --> * <p>If the camera becomes disconnected during initialization * after this function call returns, * {@link CameraDevice.StateCallback#onDisconnected} with a * {@link CameraDevice} in the disconnected state (and * {@link CameraDevice.StateCallback#onOpened} will be skipped).</p> * * <p>If opening the camera device fails, then the device callback's * {@link CameraDevice.StateCallback#onError onError} method will be called, and subsequent * calls on the camera device will throw a {@link CameraAccessException}.</p> * * @param cameraId * The unique identifier of the camera device to open * @param callback * The callback which is invoked once the camera is opened * @param handler * The handler on which the callback should be invoked, or * {@code null} to use the current thread's {@link android.os.Looper looper}. */ @RequiresPermission(android.Manifest.permission.CAMERA) public void openCamera(@NonNull String cameraId, @NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler)
mSurface = new Surface(mSurfaceTexture); ArrayList<Surface> outputs = new ArrayList<>(1); outputs.add(mSurface); mCameraDevice.createCaptureSession(outputs, mSessionListener, mHandler);
/** * <p>Create a new camera capture session by providing the target output set of Surfaces to the * camera device.</p> * * @param outputs The new set of Surfaces that should be made available as * targets for captured image data. * @param callback The callback to notify about the status of the new capture session. * @param handler The handler on which the callback should be invoked, or {@code null} to use * the current thread's {@link android.os.Looper looper}. */ public abstract void createCaptureSession(@NonNull List<Surface> outputs, @NonNull CameraCaptureSession.StateCallback callback, @Nullable Handler handler) throws CameraAccessException;
/** * <p>Submit a request for an image to be captured by the camera device.</p> * * @param request the settings for this capture * @param listener The callback object to notify once this request has been * processed. If null, no metadata will be produced for this capture, * although image data will still be produced. * @param handler the handler on which the listener should be invoked, or * {@code null} to use the current thread's {@link android.os.Looper * looper}. * * @return int A unique capture sequence ID used by * {@link CaptureCallback#onCaptureSequenceCompleted}. */ public abstract int capture(@NonNull CaptureRequest request, @Nullable CaptureCallback listener, @Nullable Handler handler) throws CameraAccessException;
关闭闪光灯较为简单,关闭CameraDivice,释放Surface实例等资源即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if (mCameraDevice != null) { mCameraDevice.close(); teardown(); }
private fun turnOn() { try { if (mCamera == null) { mCamera = Camera.open(mCameraId.toInt()) } mCamera?.let { var parameters = it.getParameters() parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH) it.setParameters(parameters) it.startPreview() } } catch (e: Throwable) { dispatchError() } } /** * Creates a new Camera object to access a particular hardware camera. If * the same camera is opened by other applications, this will throw a * RuntimeException. * * <p>You must call {@link #release()} when you are done using the camera, * otherwise it will remain locked and be unavailable to other applications. * * <p>Your application should only have one Camera object active at a time * for a particular hardware camera. * * <p>Callbacks from other methods are delivered to the event loop of the * thread which called open(). If this thread has no event loop, then * callbacks are delivered to the main application event loop. If there * is no main application event loop, callbacks are not delivered. * * <p class="caution"><b>Caution:</b> On some devices, this method may * take a long time to complete. It is best to call this method from a * worker thread (possibly using {@link android.os.AsyncTask}) to avoid * blocking the main application UI thread. * * @param cameraId the hardware camera to access, between 0 and * {@link #getNumberOfCameras()}-1. * @return a new Camera object, connected, locked and ready for use. */ public static Camera open(int cameraId) { return new Camera(cameraId); }
mCameraManager.setTorchMode(mCameraId, enabled); /** * Set the flash unit's torch mode of the camera of the given ID without opening the camera device. * * <p>Use {@link #getCameraIdList} to get the list of available camera devices and use * {@link #getCameraCharacteristics} to check whether the camera device has a flash unit. * Note that even if a camera device has a flash unit, turning on the torch mode may fail * if the camera device or other camera resources needed to turn on the torch mode are in use. * </p> * * <p> If {@link #setTorchMode} is called to turn on or off the torch mode successfully, * {@link CameraManager.TorchCallback#onTorchModeChanged} will be invoked. * However, even if turning on the torch mode is successful, the application does not have the * exclusive ownership of the flash unit or the camera device. The torch mode will be turned * off and becomes unavailable when the camera device that the flash unit belongs to becomes * unavailable or when other camera resources to keep the torch on become unavailable ( * {@link CameraManager.TorchCallback#onTorchModeUnavailable} will be invoked). Also, * other applications are free to call {@link #setTorchMode} to turn off the torch mode ( * {@link CameraManager.TorchCallback#onTorchModeChanged} will be invoked). If the latest * application that turned on the torch mode exits, the torch mode will be turned off. * * @param cameraId * The unique identifier of the camera device that the flash unit belongs to. * @param enabled * The desired state of the torch mode for the target camera device. Set to * {@code true} to turn on the torch mode. Set to {@code false} to turn off the * torch mode. */ public void setTorchMode(@NonNull String cameraId, boolean enabled)