Android Custom Camera Tutorial Example Using Surfaceview Programmatically

Android Custom Camera tutorial with example will guide you to build the custom camera preview. You will learn how to open front camera Programmatically.

Generally, everybody implement the camera feature using camera intent which will use the default camera app of the android device.

In camera intent, you can not customize the preview as per your requirements.

You also can not set the image quality, it’s pixels, it’s resolutions which will be captured by the users.

Other customize features also required sometimes, for example, your client wants a pink color and triangle capture button. Now in this case you must have to create your own camera preview.

Android provides surfaceview for creating custom camera preview.

SurfaceView enables developer to add all the UI widgets like TextView, EditText, Button, ListView etc. on the camera preview.

In this example, you will be able to take pictures using surfaceview.

You will learn how to take selfie pictures with front/secondary camera in surfaceview.

1. Camera and storage permissions

Following three permissions are required to develop this example.

You need to request for runtime permissions when you are targeting sdk version greater than 22.

You can learn about this topic here.

2. Creating Preview

Create a new class named CameraPreview.java and add following code in it.

This class helps us to create a preview using the surfaceview.

Let us understand above class.

First of all, the objects of camera and surfaceholder are created.

Below three lines will initiate the surfaceview.

When the surfaceview is initiated surfaceCreated() method is called.

Code for surfaceCreated()

As you can see that preview is starting in this method.

3. Open Main Activity

When you create new project in android studio, you will have two default files: activity_main.xml and MainActivity.java

Add following source code into the activity_main.xml file

I have taken RelativeLayout as a parent layout. First child of Relativelayout is a Linearlayout with full height and width.

This linearlayout will work as a camera preview. Second child contains two buttons.

Because parent is a relativelayout, these two buttons will overlap the camera preview.

Copy and paste following code into the MainActivity.java

Describing Main Activity Class

Look at the below source code

Compiler will first open the camera resource of the android device.

Then it will initiate the custom camera preview. LinearLayout with full height and width is set as the preview.

ChooseCamera() method

This method will allow user to switch between front (selfie) and back camera.

When the user clicks on the SWITCH CAMERA button, this method is called.

There are other methods are present in this method

  • findFrontFacingCamera()
  • findBackFacingCamera()

findFrontFacingCamera() method will set the boolean variable cameraFront as true.

findFrontFacingCamera() Code

findBackFacingCamera() Code

findBackFacingCamera() method will set the boolean variable cameraFront as false.

PictureCallback()

Another interesting method is PictureCallback(). Below is the code for this method.

When the user clicks on the TAKE PHOTO button, this method will be executed.

Here, we will get out picture in byte array format. We will convert it into the bitmap.

In the picture activity, we will preview our picture using this bitmap.

releaseCamera()

When the user pause our app by switching between various apps, we need to release the camera resource of android device. For example, he switches to whatsapp without clicking the back button.

If we do not release camera resource then other apps will not be able to use them.

When the user comes back to our app we will get back camera in onResume() method.

4. Previewing the Picture

Make one activity named PictureActivity

Write below code in activity_picture.xml file

Print below code in PictureActivity.java class

The captured image is set in the imageview.

Compiler will also save this image in the internal storage using the saveImage() method.

Output of this example

Following pictures shows the output of this tutorial.

 

android custom camera

Custom Preview with buttons

 

android custom camera

Front or Selfie Camera

 

android custom camera

Captured Picture

 

Download Source Code for Android Custom Camera Tutorial

Use following link to download the source code

[sociallocker]Download Custom Camera Source Code[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

1 Response

  1. Jay says:

    AWESOME … very helpful

Leave a Reply

Your email address will not be published. Required fields are marked *