Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera Preview -- How to 'freeze' the camera?

I'm currently trying to build an android application to take pictures and I need to freeze the camera preview on a given event (i.e. picture taken) and restart it only after another event.

What I want, in other words, is for the view to display whatever the camera sees until the freeze event occurs and then to freeze the image (i.e. display whatever was on screen at the time of this event -- as if a picture was taken) until the unfreeze event occurs.

Now, I'm currently using a SurfaceView with a SurfaceHolder.Callback to do this and I tried to use a PreviewCallback to freeze the screen, but unfortunately, I can't find an example or a tutorial and I'm really stuck at this point.

If anyone has a guide or some pointers on how to get this done, I would really appreciate the help...

I'm pasting the relevant parts of my code below:

public class CustomCameraView extends SurfaceView {

Camera camera;
SurfaceHolder previewHolder;

//Callback for the surfaceholder
SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback() {
    public void surfaceCreated(SurfaceHolder holder) {
        camera=Camera.open();

        try 
        {
            camera.setPreviewDisplay(previewHolder);
        }
        catch (Throwable t) {

        }
    }

    public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int w, int h)
    {
        Parameters params = camera.getParameters();
        params.setPictureFormat(PixelFormat.JPEG);
            camera.setParameters(params);
            camera.startPreview();
    }

    public void surfaceDestroyed(SurfaceHolder arg0)
    {
        camera.stopPreview();
        camera.release();
    }
};
public CustomCameraView(Context ctx)
{
    super(ctx);

    previewHolder = this.getHolder();
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        previewHolder.addCallback(surfaceHolderListener);
        setBackgroundColor(Color.TRANSPARENT);
}
public CustomCameraView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}
protected void onDraw (Canvas canvas)
{
}
public void closeCamera()
{
    if(camera != null)
        camera.release();
}
public void dispatchDraw(Canvas c)
{
    super.dispatchDraw(c);
}

}

Thank you very much for your help!

-Billy

like image 439
user510159 Avatar asked Nov 16 '10 23:11

user510159


People also ask

How to set camera preview orientation in Android?

To force portrait orientation: set android:screenOrientation="portrait" in your AndroidManifest. xml and call camera. setDisplayOrientation(90); before calling camera.

How do you pause Camerax?

stopRepeating() and the TextureView will stop getting input from the camera.

What is camera preview in Android?

PreviewView is a subclass of FrameLayout . To display the camera feed, it uses either a SurfaceView or TextureView , provides a preview surface to the camera when it's ready, tries to keep it valid as long as the camera is using it, and when released prematurely, provides a new surface if the camera is still in use.

What is preview in camera?

Live preview is a feature that allows a digital camera's display screen to be used as a viewfinder. This provides a means of previewing framing and other exposure before taking the photograph.


1 Answers

Old question, I know, but answering for posterity. You should be able to simply call

camera.stopPreview();

The preview will freeze on whatever you're looking at until you call startPreview() again.

like image 148
Mike Avatar answered Oct 01 '22 15:10

Mike