Search This Blog

Friday 22 August 2014

develop camera part 2

How to create the camera view:

        Here i will show you how to create the camera view go to this link you will see that how to open the camera object   
http://androidbasedapplication.blogspot.in/2014/07/develop-camera-using-eclips.html

         Normally when we use the camera we need to see the preview of that subject before we clicking on shutter. you can see the  SurfaceView to draw of preview of what the camera sensor is picking up. here i will develop the camera view.

Preview class:  
         
          To get start with with displaying a preview, you need a preview class.it requires the implement of android.view.SurfaceHolder.Callback  the interface, which used to pass image data from the camera hardware to application.the preview class must be pass to the camera object before the live image preview can be started .

class Preview extends ViewGroup implements SurfaceHolder.Callback {

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;

    Preview(Context context) {
        super(context);

        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
...
}

set and start preview:

A camera is instance and its related preview must be created in specific Oder with the camera object being the first.in the below snippet, the process of initializing the camera is encapsulated    so that Camera.startPreview()is called by the setCamera()  method.whenever user does something to change in the camera. The preview must also be restarted in the preview class surfaceChanged() call back method.

public void setCamera(Camera camera) {
    if (mCamera == camera) { return; }
    
    stopPreviewAndFreeCamera();
    
    mCamera = camera;
    
    if (mCamera != null) {
        List<Size> localSizes = mCamera.getParameters().getSupportedPreviewSizes();
        mSupportedPreviewSizes = localSizes;
        requestLayout();
      
        try {
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            e.printStackTrace();
        }
      
        // Important: Call startPreview() to start updating the preview
        // surface. Preview must be started before you can take a picture.
        mCamera.startPreview();
    }
}

Modify camera setting :

camera setting changes the way that the camera takes picture, from the zoom level to exposure compensation. this example changes only preview size , see the source code of the camera application for many more.
 
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Now that the size is known, set up the camera parameters and begin
    // the preview.
    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
    requestLayout();
    mCamera.setParameters(parameters);

    // Important: Call startPreview() to start updating the preview surface.
    // Preview must be started before you can take a picture.
    mCamera.startPreview();
}
      

set the preview orientation : 

most of camera application lock the display into landscape mode becuse that is natural orientation of the camera sensor. this setting does not prevent you from taking portrait-mode photos,   because the orientation the device is recorded in the EXIF header. the method lets you change how the preview is displayed with out affecting how the images is recorded . However in android prior level to API level .you must stop your preview before changing the orientation and then restart it. 


No comments:

Post a Comment