Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOneShotPreviewCallback not calling back my method

I'm trying to create an application that accesses the Android camera, gets a frame(image), processes it, then restarts the process, all this while providing a smooth preview on the screen.

I do this by getting a frame by calling camera.setOneShotPreviewCallback(...), processing the data received in my callback, then calling setOneShotPreviewCallback again.

The problem is, I can't start the process by putting setOneShotPreviewCallback in the main activity's onResume() function. If i do that, my callback is never called.

See the following test code:

public void onResume() {
    super.onResume();
    camera = getCameraInstance();

    cameraPreviewSurface = new CameraPreview(this, camera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.removeAllViews();
    preview.addView(cameraPreviewSurface);
    camera.setOneShotPreviewCallback(cameraPreviewCallback);

    Button button = (Button) findViewById(R.id.button_capture);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            camera.setOneShotPreviewCallback(cameraPreviewCallback);
        }
    });
}

When the application starts, the callback is never called, but as soon as the button is pressed, I get a callback.

I have tried using setOneShotPreviewCallback inside my CameraPreview class, after camera.startPreview();, but still my method was not getting called.

like image 391
Twinsen Avatar asked Oct 05 '22 10:10

Twinsen


1 Answers

I found the problem. For some reason the callback must be set after the preview surface is initialized.

In my case, since I was using a custom SurfaceView I had to apply the callback both in the surfaceCreated and surfaceChanged functions.

camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
camera.setOneShotPreviewCallback(cameraPreviewCallback);
like image 111
Twinsen Avatar answered Oct 18 '22 15:10

Twinsen