Reputation: 15
I'm developing an Android application, and I need to get the max of fps from the Camera.
I've been trying to implement the setPreviewCallbackWithBuffer but I can't get it to work.
Can anyone give me an example or an explanation?
I've set a timer, that runs every 10 milliseconds and there I run takePicture().Is that correct, or what is the way to get max fps ?
I already searched everywhere but I can't find an explanation that works for me.
Thank You.
Edit 2:
startRecording.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
if(!recording)
{
recording = true;
thread.start();
cam.startPreview();
Size previewSize=cam.getParameters().getPreviewSize();
int dataBufferSize=(int)(previewSize.height*previewSize.width*(ImageFormat.getBitsPerPixel(cam.getParameters().getPreviewFormat())/8.0));
cam.addCallbackBuffer(new byte[dataBufferSize]);
cam.addCallbackBuffer(new byte[dataBufferSize]);
cam.addCallbackBuffer(new byte[dataBufferSize]);
cam.setPreviewCallbackWithBuffer(new PreviewCallback() {
public void onPreviewFrame(byte[] imageData, Camera arg1) {
try {
photos.add(imageData);
} catch(Exception e) { System.out.println("ERROR!");
}
}
});
}
(...)
Thank You!
Upvotes: 0
Views: 1178
Reputation: 1007359
Quoting the documentation for setPreviewCallbackWithBuffer()
:
Installs a callback to be invoked for every preview frame, using buffers supplied with addCallbackBuffer(byte[]), in addition to displaying them on the screen. The callback will be repeatedly called for as long as preview is active and buffers are available. Any other preview callbacks are overridden.
So, delete your Timer
. Call setPreviewCallbackWithBuffer()
once, and you will be called with preview frames, as they are ready for processing.
Quoting the documentation for addCallbackBuffer()
:
For formats besides YV12, the size of the buffer is determined by multiplying the preview image width, height, and bytes per pixel. The width and height can be read from getPreviewSize(). Bytes per pixel can be computed from getBitsPerPixel(int) / 8, using the image format from getPreviewFormat().
If using the YV12 format, the size can be calculated using the equations listed in setPreviewFormat(int).
Please follow those instructions, rather than guessing at a buffer size.
Further quoting the documentation for addCallbackBuffer()
:
Adds a pre-allocated buffer to the preview callback buffer queue. Applications can add one or more buffers to the queue. When a preview frame arrives and there is still at least one available buffer, the buffer will be used and removed from the queue. Then preview callback is invoked with the buffer. If a frame arrives and there is no buffer left, the frame is discarded. Applications should add buffers back when they finish processing the data in them.
Call addCallbackBuffer()
for a few buffers up front, when you call setPreviewCallbackWithBuffer()
. In onPreviewFrame()
, arrange for a background thread to process that frame's buffer. When that work is done, add that same buffer back to the pool by calling addCallbackBuffer()
. Do NOT just keep allocating new buffers.
Upvotes: 2