Custom Camera using SurfaceView

Custom Camera using SurfaceView

SurfaceView is a class provided by android.view package. It offers a dedicated drawing surface embedded inside of a view hierarchy. We can manage the format of this surface, however, the SurfaceView takes care of putting the surface at the right location on the screen.

In this post, we will use the SurfaceView to preview the camera (android.hardware.camera) onto the screen and capture images using it.

First let us create a new project and add the following dependencies in your app level build.gradle file which is available at location: app/build.gradle.

In the AndroidManifest.xml file, add the following permission.

We will also be needing the real-time permission check for android version M and above. Add the below code in your MainActivity.java for handling these checks.

Now let’s build our layout file. So open activity_main.xml file and add the below code.

Now in MainActivity.java, let us set up our implementation for SurfaceView.

To initialize SurfaceView, use the following code :

Now to setup SurfaceHolder, we need to first create a FaceDetector object.

Now setup SurfaceHolder and add a callback to it.

Now to click the picture, we need to setup OnClickListener to click the picture and display it.

Below is the complete code for MainActivity.java.

That’s all for this post. Stay tuned for the next post to learn how to click the picture on the blinking of your eyes.

InnovationM is a globally renowned Android app development company in India that caters to a strong & secure Android app development, iOS app development, hybrid app development services. Our commitment & engagement towards our target gives us brighter in the world of technology and has led us to establish success stories consecutively which makes us the best iOS app development company in India.

Thanks for giving your valuable time. Keep reading and keep learning 🙂

Leave a Reply