Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom Camera in SurfaceView-Preview

I know the camera functions getMaxZoom(), isZoomSupported() or get/setZoom(). I can use these functions but nothing happens in my Surface-Camera-Preview. I want that my preview starts in zoom all time.I use Android 2.3.3. Do I forget something?!

Code is:

Camera.Parameters parameters = camera.getParameters();
if(parameters.isZoomSupported())
          {
              int maxZoom= parameters.getMaxZoom();
              parameters.setZoom(maxZoom);
              camera.setParameters(parameters);
          }

Hope anybody has an idea...

Thanks!

like image 449
Tschakle Avatar asked Feb 14 '12 14:02

Tschakle


People also ask

What is a camera preview screen?

PreviewView — Implementation modes 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.


2 Answers

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CustomCameraView extends SurfaceView
{
    Camera camera;
    SurfaceHolder previewHolder;

    public void zoom()
    {
        Parameters params=camera.getParameters();
        params.setZoom(params.getMaxZoom());
        camera.setParameters(params);   
    }

    public void unzoom()
    {
        Parameters params=camera.getParameters();
        params.setZoom(0);
        camera.setParameters(params);
    }

    public CustomCameraView(Context context) 
    {
        super(context);
        previewHolder=this.getHolder();
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        SurfaceHolder.Callback surfaceHolderListener=new SurfaceHolder.Callback()
        {
            public void surfaceCreated(SurfaceHolder holder) 
            {
                camera=Camera.open();
                try{
                    camera.setPreviewDisplay(previewHolder);
                }
                catch (Exception e) {
                }
            }

            public void surfaceChanged(SurfaceHolder holder, int format,
                    int width, int height) {
                Parameters params=camera.getParameters();
                params.setPreviewSize(width, height);
                params.setPictureFormat(PixelFormat.JPEG);

                //if you want the preview to be zoomed from start :
                params.setZoom(params.getMaxZoom());

                camera.setParameters(params);
                camera.startPreview();
            }

            public void surfaceDestroyed(SurfaceHolder holder) {
                camera.stopPreview();
                camera.release();
            }
        };
        previewHolder.addCallback(surfaceHolderListener);
    }

}
like image 132
Aditya Pawade Avatar answered Oct 25 '22 12:10

Aditya Pawade


if (params.isZoomSupported()) {
    final int maxZoomLevel = params.getMaxZoom();
    Log.i("max ZOOM ", "is " + maxZoomLevel);
    zoomControls.setIsZoomInEnabled(true);
    zoomControls.setIsZoomOutEnabled(true);

    zoomControls.setOnZoomInClickListener(new OnClickListener(){
        public void onClick(View v){
            if(currentZoomLevel < maxZoomLevel){
                currentZoomLevel++;
                //mCamera.startSmoothZoom(currentZoomLevel);
                params.setZoom(currentZoomLevel);
                mCamera.setParameters(params);
            }
        }
    });

    zoomControls.setOnZoomOutClickListener(new OnClickListener(){
        public void onClick(View v){
            if(currentZoomLevel > 0){
                currentZoomLevel--;
                params.setZoom(currentZoomLevel);
                mCamera.setParameters(params);
            }
        }
    });    
}
else
    zoomControls.setVisibility(View.GONE);
like image 31
ROHIT PARMAR Avatar answered Oct 25 '22 14:10

ROHIT PARMAR