Custom Camera API Tutorial in Android Studio using Camera2 API
Welcome guys. So in this tutorial you will learn how to display the camera preview in your Android Application.
So lets get started.
Introduction
Nowadays a lot of apps offer users to take pictures and record videos for various kind of purposes. In this tutorial you will learn how to do the same in your android application.
You can integrate your camera in multiple ways. One of the most simple way is to use the built-in camera app of Android. This is a simple process where you just call your built-in camera application using intent and you just take picture and videos from it and save them in your android directory. This way of accessing camera wont give you much control over your camera in certain ways as most of the work is done by the built-in camera app. This way is most appropriate when you just want to take a picture and video.
The second step is to use the Custom camera app in your android and then add the camera functionality on your own. So this way will require you more effort because all of the background work and most of the work will be done by the developer. Building custom Camera interface will be useful when you are building an application which mainly deals with images and videos just like Instagram app.
Source Code
First of all you have to specify the permissions which your app will be requiring when it will be installed on your android smartphone.
So open AndroidManifest.xml and add these lines of code in the beginning.
Now create your activity_main.xml and change the Layout to FrameLayout.
Now add the following code in your MainActivity.java.
So the complete switch_on_camera() method will be like this:
So now you have to create another class ShowCamera and extend it with SurfaceView.