Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get frames from camera's phone in android

Tags:

android

camera

I would like to get frames from camera's phone. So, i try to capture video and i use matlab to find frames per second of this video, i got 250 frames per 10 seconds. But when i use

public void onPreviewFrame(byte[] data, Camera camera) {}

on Android, i only get 70 frames per 10 seconds. Do you know why? I put my code below:

private Camera.PreviewCallback previewCallBack = new Camera.PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {

        System.out.println("Get frame " + frameNumber);
        if (data == null)
            throw new NullPointerException();
        Camera.Parameters p = camera.getParameters();
        Camera.Size size = p.getPreviewSize();
        if (frameNumber == 0) {
            startTime = System.currentTimeMillis();
        }
        // Log.e("GetData", "Get frame " + frameNumber);
        frameNumber++;

        camera.addCallbackBuffer(data);

            }
      }
like image 223
user3629083 Avatar asked Feb 13 '23 23:02

user3629083


1 Answers

That's true; Android video recorder does not use Camera.PreviewCallback, and it may be much faster than what you get with Java callbacks. The reason is that it can send the video frame from camera to the hardware encoder inside the kernel, without ever putting the pixels into user space.

However, I have reliably achieved 30 FPS in Java on advanced devices, like Nexus 4 or Galaxy S3. The secrets are: to avoid garbage collection by using Camera.setPreviewCallbackWithBuffer(), and to push the callbacks off the UI thread by using an HandlerThread.

Naturally, the preview callback itself should be optimized as thoroughly as possible. In your sample, the calls to camera.getParameters() is slow and can be avoided. No allocations (new) should be made.

like image 145
Alex Cohn Avatar answered Feb 27 '23 10:02

Alex Cohn