[contact-form-7 404 "Not Found"]
Other Ways To Meet Us :info@zealousys.com
Our Blog
Feb 27


By zealousys

This article describes the usefulness of SurfaceView in Android applications. SurfaceView provides a dedicated Surface for drawing and one can utilize SurfaceView to customize the built-in Camera in an Android Device. You can also use Intent Activity available in Android API for taking pictures but SurfaceView makes customisation possible.

The Default camera provided by Android sdk is just not enough for today’s application related to Social Networking and Photography. One need some sort of facility to open camera in one portion of the view and render other widgets on the other portion .That is where SurfaceView comes into picture.

We need to create some sort of Surface for camera to draw view finder or preview images on. SurfaceView is a concrete class providing surface inside a Parent or Standard View. Along with the SurfaceView, we will use Camera class which allows access to the camera hardware.

Make one XML file in path res/layout which Include one simple SurfaceView

  1. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=http://schemas.android.com/tools android:id=”@+id/LinearLayout1″ android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”vertical” tools:context=”.MainActivity” >
  2.    <Button android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello_world” android:gravity=”center”/>
  3.    <SurfaceView android:id=”@+id/camera_surface” android:layout_width=”fill_parent” android:layout_height=”400dp”/>
  4.    <TextView android:id=”@+id/textview” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:gravity=”center”/>
  5. </LinearLayout>

Camera Implementation code

/** Create an activity called CameraView that implements the interface ‘SurfaceHolder.Callback’. This will override some methods which control life cycle of Surface which later will be associated with Camera. **/

public class CamaraView extends Activity implements SurfaceHolder.Callback

  • The interface “SurfaceHolder.Callback” is used to receive information about changes that occur in the surface (in this case, the camera preview). SurfaceHolder.Callback implements three methods:
    • SurfaceChanged -This method is called when the size or the format of the surface changes.
    • SurfaceCreated – When, in the first instance, the surface is created, this method is called. Implementations of this should start up whatever rendering code they desire. Note that only one thread can ever draw into a Surface so you should not draw into the Surface here if your normal rendering will be in another thread.
    • SurfaceDestroyed – This is called when the surface is destroyed.

All of the above method takes SurfaceHolder as a Parameter.

SurfaceHolder is an Abstract interface to someone holding a display surface, allow you to control the surface size and format, edit the pixels in the surface and monitor changes to the surface. This interface is typically available through the SurfaceView class.

/** We will write following code in onCreate() method of our activity in order to initialize Surface, get SutrfaceHolder associated with it and CallBack to SurfaceHolder.**/

  1. super.onCreate(savedInstanceState);
  2. setContentView(R.layout.activity_main);
  3. mSurfaceView = (SurfaceView) findViewById(R.id.camera_surface);
  4. mSurfaceHolder = mSurfaceView.getHolder();
  5. mSurfaceHolder.addCallback(this); //this means our activity is going to manage holder.
  6. mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

/** Below code shows implementation of CallBack Functions. In order to use camera hardware, we have to specify it in the Android.Manifest file <uses-permission android:name=”android.permission.CAMERA”/> **/

  1. public void surfaceChanged(SurfaceHolder holder, int format, int width,int height){
  2.    // TODO Auto-generated method stub
  3.    Camera.Parameters p = mCamera.getParameters();
  4.    p.setPreviewSize(width, height);
  5.    mCamera.setParameters(p);
  6.    try {
  7.       mCamera.setPreviewDisplay(mSurfaceHolder);
  8.    } catch (IOException e) {
  9.       e.printStackTrace();
  10.    }
  11. }
  13. public void surfaceCreated(SurfaceHolder holder) {
  14.    // TODO Auto-generated method stub
  15.    try {
  16.       Log.d(“TAG”, “surfaceCreated(),holder”+holder.toString());
  17.       mCamera = null;
  18.       mCamera = Camera.open();
  19.       Log.d(“TAG”, “surfaceCreated(), mCamera=”+mCamera);
  20.       mCamera.setDisplayOrientation(90);
  21.       mCamera.setPreviewDisplay(mSurfaceHolder);
  22.       Camera.Parameters params= mCamera.getParameters();
  23.       params.set(“jpeg-quality”, 72);
  24.       params.set(“rotation”, 90);
  25.       params.set(“orientation”, “portrait”);
  26.       params.setPictureFormat(PixelFormat.JPEG);
  27.       mCamera.setParameters(params);
  28.       mCamera.startPreview();
  29.    } catch (Exception e) {
  31.       Toast.makeText(MainActivity.this,” surfaceCreated ” + e.getMessage(), Toast.LENGTH_LONG).show();
  32.       e.printStackTrace();
  33.    }
  34. }
  36. public void surfaceDestroyed(SurfaceHolder holder) {
  37.    // TODO Auto-generated method stub
  38.    mCamera.stopPreview();
  39.    mCamera.release();
  40. }

What Can We Build for You?


NDA From The Start
We keep your idea safe. We're always willing to sign a Non-Disclosure Agreement with you and fully protect your intellectual property.



For sales inquiry