We are working on a project that utilizes Android preview feature. We are capturing preview at maximum available resolution (1600 x 1200 at present).
Requirement is to save each and every frame of preview on secondary storage. OnPreviewFrame
is capable of giving preview at 30 FPS (approx. 30 milliseconds/frame) but when code is added to save the data, it limits the FPS.
Tested so far:
Both OnPreviewFrame
and Saving JPEG in the Main thread. In this case, saving roughly takes 250-260 milliseconds. So, FPS becomes 5-6.
OnPreviewFrame
in main thread and Saving JPEG in another thread. So, a new thread is created for each frame for saving. It creates a lot if threads in memory (each taking approx. 450-500 ms for quality factor 60) and when memory is freed, a gap is observed in the preview.
So, it there any possible workaround by which we can save all the frames at 30 FPS?
YuvImage yuvImage = new YuvImage(data, activity.previewFormat, activity.previewWidth, activity.previewHeight, null);
FileOutputStream fos = new FileOutputStream(this.file);
yuvImage.compressToJpeg(new Rect(0, 0, activity.previewWidth, activity.previewHeight), 60, fos);
fos.flush();
fos.close();
You are doing a lot of resource consuming operations. some can't be avoided like the disk I/O, the others can be significantly reduced.