How to get a frame when the camera is previewing, without capture still picture?
I need to regoznize a frame when camera is previewing, now I know how to get the still picture, but when the capture picture request is started, the preview can't be smoothed, it means the preview repeating request is interrupt by the Capture still picture request. So is theri any way to get the frame under the perview and didn't interrupt the preview?
@tjohns @ryanseys @shailen do all you know how to get a frame not a still picture under the previewing state and didn't interrupt the preview
Hello, I have the same issue. Could anybody to help to get frame from preview? ImageReader.OnImageAvailableListener is not a right way, because you will not getting 30 frame during seconds. But if you're using Camera.PreviewCallback you can getting a lot of frames during a second and use it in real time.
@sinnuswong @syakimchik there are several things that you can do to address this problem. If I'm understanding correctly, you are trying to do some sort of frame analysis while showing the preview on a SurfaceView (or TextureView).
when the capture picture request is started, the preview can't be smoothed
There are a few ways to address this problem. You can use a template designed not to interrupt the preview stream, like ZSL. It is also recommended that you add the Surface from your SurfaceView as a target to your still picture capture request. For more details, take a look at the "Interleaving Requests" section in our latest blog post.
ImageReader.OnImageAvailableListener is not a right way, because you will not getting 30 frame during seconds
If you setup your capture session and image processing pipeline correctly, you should be able to maintain ~30 FPS on most if not all devices -- the actual expected FPS can be computed by taking into consideration StreamConfigurationMap.getOutputMinFrameDuration() and StreamConfigurationMap.getOutputStallDuration(). If you use 3-5 frames as a buffer in your ImageReader by passing that number as the maxImages parameter in the ImageReader constructor and you keep your average processing time per frame at a rate higher than 30 FPS, you should be seeing 30 FPS output.
The problem here is that if you also want to display that same output to the user in real time then you need to either setup two separate targets, in which case you may not be able to do both the analysis and the preview shown to the user at maximum resolution; or use a single output target and copy each frame as it comes into a View (which you then show to the user), but then the copy alone will be taking up a lot of the scarce processing time necessary to maintain 30 FPS.
Hi @sinnuswong One way I used in the preview session is by calling getBitmap() inside the capture session's callback onCaptureCompleted as long as the current class inherits from TextureView. (Like the demo).
I believe for each frame we could access its bitmap.
mPreviewSession.setRepeatingRequest(previewBuilder.build(), new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); Bitmap bitmap = getBitmap(); //do your own logic there } }, backgroundHandler);
Hi @liute62,
I've tried you way to get Bitmap from SurfaceView on onCaptureCompleted method. But it also causes a freezing UI. It's not a good way. Camera1 API is using onPreviewFrame method which is not caused any UI freezing.
@syakimchik have you some news for this?
I tried to get the frame as @liute62 suggested and it works really well for a start. I do prefer to get it from the ImageReader.OnImageAvailableListener as I request my pictures to be a minimum of 480 height.
@the moment I'm getting the texture.getBitmap()(The docs itself says to try to avoid it) from the public void onSurfaceTextureUpdated(SurfaceTexture texture) instead of the captureCalback as @liute62 pointed.
If you do it on the captureCallback you will have to do a runOnUiThread(new Runnable().
i wait for a frame to be processed and failed to get another frame for processing and add to a AsyncTask. The UI gets blocked but I can live with that until I find a more suitable method from the ImageReader.OnImageAvailableListener.
Uploaded a video if you want to check it out, at the end I move shake it a bit for the blocking to be more noticeable. I think is a start.
I manage to get it working with Camera2 by creating a captureStillPicture() like in the sample:
private void captureStillPicture() {
try {
if (cameraDevice == null) {
return;
}
int rotation = getWindowManager().getDefaultDisplay().getRotation();
CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureBuilder.addTarget(imageReader.getSurface());
captureBuilder.set(CaptureRequest.FLASH_MODE, torchStatus);
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
captureBuilder.set(CaptureRequest.JPEG_QUALITY, (byte) 100);
captureBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, range);
CameraCaptureSession.CaptureCallback captureStillCallback = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
}
@Override
public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
super.onCaptureStarted(session, request, timestamp, frameNumber);
}
};
captureSession.capture(captureBuilder.build(), captureStillCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
Which gets called from the preview captureCallback, as is, from the sample, since I'm using OCR with MLKit I don't really need much settings, i get really good results as you can see in this video However in my LGM250 which reported to be CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY was REALLY horrible. So I thought on using the deprecated Camera API for LEGACY and LIMITED when found out of CameraView and oh lord sweet baby Jesus!!!
Do yourself a favor if you don't need any of the fancy stuff you can get with Camera2 for frame processing use CameraView it reduced my camera logic to 30% and is LIGHTNNING FAST!!!!! and is virtually very very easy to implement, take it from me ;-)
@GuanacoDevs Can you look into my stackoverflow's problem? I can't even have the camera set to the setting I want correctly and can you guide me more regarding getting frame by frame? https://stackoverflow.com/questions/56539650/why-turning-on-flash-change-the-focus-mode-of-the-camera
@chungyau97 To be honest is been awhile since I tinkered around with Camera2 since I'm using [CameraView](https://github.com/natario1/CameraView), but I did post an answer that might help.
On a side note I think is better if you drop Camera2 and start using [CameraX](https://developer.android.com/training/camerax) I haven't used yet but looking forward to, seems promising.
On the frame by frame side, I did got to get good results after two or three weeks of working with it until I found out about CameraView but the reality is that Camera2 is not good for frame processing(you will make it work, but it will be a real pain in the ass), it will make you work really hard, when now CameraX seems a better bet is Camera2 simplified(as stated in the link), so I'd suggest, no, I BEG YOU!!!! to switch to CameraX. It also comes with frame processing!!!
This sample has been migrated to a new location where we can accept Pull Requests (check README for more information).
As recommended by GitHub, we are closing all issues and pull requests now that this older repo will be archived.
If you still see this issue in the updated repo, please reopen the issue/PR there. Thank you!