Download Code

Android Studio SurfaceView Example

In android studio surfaceview example you will learn how to preview camera on surfaceview in android application using surface-view. How to apply color effects on camera preview.First of all i’ll give you explanation everything concerning classes of android surfaceview example tutorial.

These are the concerning things in this android studio surfaceview example tutorial.

  • Camera
  • Surface View
  • SurfaceHolder
  • Callback
  • FileOutputStream


Used to capture image and video.Some Android devices have front or some are not having that camera facility.Basic use of Cam.There are alot of Cam feature like you can detect face,apply color effects etc.So let’s talk about Effect.Here is the list of color effects method on camera hardware some might be supported on your device some might be not.

  • Camera.Parameters.EFFECT_AQUA
  • Parameters.EFFECT_NEGATIVE
  • Parameters.EFFECT_NONE
  • Parameters.EFFECT_MONO
  • Parameters.EFFECT_SEPIA
  • Parameters.EFFECT_SOLARIZE

Surface View

Surface View class provides dedicated surface within view hierarchy.Surface View is basically extends from View class.Surface View can be used to place overlays buttons or any other like camera preview.

Surface Holder

Surface Holder interface is used to hold display surface.Surface Holder can be used to edit pixels,format or changes to surface.Access to surface is basically retrieved using getholder() method.


Callback is kind of confusing term or you can say interface method.Basically Callback is a piece of executable code means an object which will passed to any function as argument.For example let’s you have two buckets.If you hit first then something happen and if you hit second nothing happen.It means when first event occur(hit) it’s function will be called.

Second Description might clear good enough.
You use an interface of which receiver object will call one or more methods connected to that object.


This class is used to write stream of bytes to a file.If that file already exist it’ll overwrite otherwise create new file.For more detail.

Ok you are done with classes information which you will use in this android surfaceview example tutorial.

Create new android studio surfaceview example application project.Then go to your activity_main.xml file.Add Surfaceview and a button to load/open your camera.


android studio surfaceview,android studio surfaceview example,surfaceview example,surfaceview,

After that go to your class and do the following steps.

  • Create button variable and refer id of button.
  • Implement an interface SurfaceHolder.Callback then add unimplemented methods of Surface.
  • Declare variables for Camera,SurfaceView,SurfaceHolder and Camera.
  • Use getWindow() method to get preview window.
  • Set it pixel format to UNKNOWN.
  • Refer the surfaceView which you have used in XML and provide access to the surfaceHolder using getholder() method.
  • Add callback method to that particular context means class

Now When First surface created your camera will be at display on surfaceView using open() method.Set it’s orientation to 90 degree means vertical within the surfaceCreated() method.When you leave the application or stop the application you will stop camera within surfaceDestroyed() method.

Next is surfaceChanged() method.Within this method we put code to set camera parameters like color effect and preview to surfaceHolder.Set camera condition to true.

Till here if you run your android application project.Your camera preview will display on surfaceView.
Next you will write function for image capturing button.In that function you will use PictureCallback interface to supply image data.And Store in a directory called “AndroidCodec_”.Next you will call takepicture method on button click passing just one parameter mPictureCallBack others should be null.

Before running your application project.Add CAMERA permission in your Android Manifest.xml file.

package com.example.anonymous.studiosurfaceview;

import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    Camera camera; // camera class variable
    SurfaceView camView; // drawing camera preview using this variable
    SurfaceHolder surfaceHolder; // variable to hold surface for surfaceView which means display
    boolean camCondition = false;  // conditional variable for camera preview checking and set to false
    Button cap;    // image capturing button

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // getWindow() to get window and set it's pixel format which is UNKNOWN
        // refering the id of surfaceView
        camView = (SurfaceView) findViewById(;
        // getting access to the surface of surfaceView and return it to surfaceHolder
        surfaceHolder = camView.getHolder();
        // adding call back to this context means MainActivity
        // to set surface type

        // refering button id
        cap = (Button) findViewById(;
        // click event on button
        cap.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                // calling a method of camera class takepicture by passing one picture callback interface parameter
                camera.takePicture(null, null, null, mPictureCallback);
    // camera picture taken image and store in directory
    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera c) {

            FileOutputStream outStream = null;
            try {

                // Directory and name of the photo. We put system time
                // as a postfix, so all photos will have a unique file name.
                outStream = new FileOutputStream("/sdcard/AndroidCodec_" +
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            } finally {


    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // TODO Auto-generated method stub
        // stop the camera
            camera.stopPreview(); // stop preview using stopPreview() method
            camCondition = false; // setting camera condition to false means stop
        // condition to check whether your device have camera or not
        if (camera != null){
            try {
                Camera.Parameters parameters = camera.getParameters();
                parameters.setColorEffect(Camera.Parameters.EFFECT_SEPIA); //applying effect on camera
                camera.setParameters(parameters); // setting camera parameters
                camera.setPreviewDisplay(surfaceHolder); // setting preview of camera
                camera.startPreview();  // starting camera preview

                camCondition = true; // setting camera to true which means having camera
            } catch (IOException e) {
                // TODO Auto-generated catch block

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera =;   // opening camera
        camera.setDisplayOrientation(90);   // setting camera preview orientation

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera.stopPreview();  // stopping camera preview
        camera.release();       // releasing camera
        camera = null;          // setting camera to null when left
        camCondition = false;   // setting camera condition to false also when exit from application


Run your android studio surfaceview example application project.

I have added every line comment for you convenience.Final Preview of your app with color effect.

android studio surfaceview,android studio surfaceview example,surfaceview example,surfaceview,

I hope this android studio surfaceview example is helpful for you.

Don’t forget to leave your feedback.