|
|
|
Android Camera2 API CallBack
Android Source Code
Java program for surveillance using an Android smartphone. Webcam with FTP access to the photo storage.
Android Safety SYSTEM >>
When working with a camera, different callbacks are triggered. Let's see how each callback interacts with the Camera2 API when we grab an image from a device.
Five callbacks are the minimum that you need to understand to work with the camera.
Let's consider callbacks
private CameraDevice.StateCallback mCameraCallback = new CameraDevice.StateCallback() { //As soon as the camera is ready, that callback is triggered
We initialize the CameraDevice.StateCallback object in our application, one of the methods of which will be called after the camera is opened on the device.
@Override
public void onOpened(@NonNull CameraDevice cameraDevice){...}
We do this
public void onOpened(CameraDevice camera) { // When the camera is OPEN and can do something there: display the image from the camera on a preview, forward it further for saving, and so on.
mCameraDevice = camera;
Log.i(LOG_TAG, "== Open camera with id:"+mCameraDevice.getId()); //Which camera will be used
createCameraPreviewSession();
}
We need to keep track of the moment when the picture taken by the camera becomes available to us. Organizing the transfer of the image from the camera to other objects is a rather resource-intensive process, so it will also be performed in the background. For this there is a CameraCaptureSession.CaptureCallback object, its onCaptureCompleted method will be called after the camera captures an image.
mCameraDevice.createCaptureSession(Arrays.asList(surface,mImageReader.getSurface()) - In order to be able to view or take pictures, you need to create a session to work with the camera. Call the createCaptureSession () method for the selected camera.
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) { // Take a picture here
...}
A situation when the camera has already turned on and can send data:
@Override
onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_START);
mCaptureSession.capture(mPreviewRequestBuilder.build(),
mCaptureCallback, mBackgroundHandler)
...
};
Example
CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
}
};
CameraCaptureSession.CaptureCallback (class) - A callback object for tracking the progress of a CaptureRequest submitted to the camera device.
CameraCaptureSession.StateCallback (class) - A callback object for receiving updates about the state of a camera capture session
Determining when the image of the photo is ready
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener // Listen when the image is ready
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile));
//============ Write to telemetry ===============
PrintTelemetry npt = new PrintTelemetry();
printTxtView = "Image Ready - OK";
npt.printTV(printTxtView);
//==================================================
Log.i(LOG_TAG, "==| onImageAvailable |== - Image Ready - OK ");
}
};
Android Camera2 API CallBack
|
|
|
|
|
|
|
Application Rescue PHOTO has significant commercial potential. A huge number of old and damaged smartphones are sent to a distant box, landfill or recycling, without creating any cashback for you. Having zero cost, such smartphones can be used for photographic observation of objects via the Internet. Those. can be used to create low-cost commercial products. A company that creates such software can have positive dynamics in the market. Using the basic software product Rescue PHOTO in Java, you can create software for any needs of your customers.
Many useful functions can be added to the basic Java code based on additional agreements with clients. This allows you to monetize the free Rescue PHOTO remote surveillance app.
|
|
|
|