The Android Framework provides us with support for taking images and videos using the built-in camera app. But what if you want to develop your own without writing to much boilerplate code?
Today we are going to do exactly that using the Fotoapparat library which provides an easy way to interact with the Android camera while still maintaining a powerful collection of parameter customization options.
The project we will develop
We will create a simple camera app which lets the user take photos, switch from the back to the front camera and vice versa and control the flash. It will also provide the user with a preview of the camera using the CameraView which is included in the Fotoapparat library.
So, without wasting any further time, let’s get started.
Importing the dependencies
The first thing we need to do is importing the needed dependencies for our app. We can do so by adding a few lines of code to our build.gradle (Module:app) file.
Creating the UI
Now, we need to build the user interface of our camera app. It consists of a simple layout with 3 FloatingActionButtons which will be used to take an image, switch cameras and control the flash. We will also include a CameraView which will display a preview of the camera.
Requesting the required permissions
After creating the UI we can almost start using the Fotoapparat to build our app. But first, we need to request the required permissions to use the camera and access the local storage. We can do that with some simple lines of code in our AndroidManifest.xml file.
We also need to check if the user has really enabled the permissions and request them at runtime if he hasn’t. We can do so in our MainActivity.kt file.
Taking an image
First, we need to add OnClickListeners to the buttons to make sure they react to user events.
Next, we need to configure our instance of the Fotoapparat class.
Here we initialize the Fotoapparat instance and define basic parameters like which camera and scale-type to use. We also define the view in which the camera preview will be displayed. After that we start the preview in the onStart() method and stop it in the OnStop().
Taking an image
Next, we need to define a path and filename for our output and start implementing the image taking functionality.
Here we check if we have all the required permission. If not we request the remaining ones. After we have all the right permission we take an image and save it in the local storage path we defined above.
Now we can start managing the states of our Fotoapparat to make sure we have all the information needed for performing camera events like switching from the back to the front camera.
Note: The state of the Fotoapparat is only used to make sure that the activity is reloaded when we start the app and request the permissions for the first time so the user sees the preview.
Here we use enum classes to manage our states to make sure we know exactly what we should do when the switchCamera or the changeFlashState methods are called.
After checking the state we can continue working on our camera features.
Here we implemented the switch camera functionality using the state we defined above and the switchTo function provided by the Fotoapparat library. To switch the camera, we need to pass the camera we want and our camera configuration (We pass an empty instance for the standard configuration).
Turning the flash on
The changeFlashState method is very similar to the switchCamera one. We are working with the flashState we defined above and just invert it when the changeFlashState method is called.
After that, we change the flashState to match the current flash condition.
Complete Source Code for the MainActivity.kt
Here you can get the complete source code for our basic camera app:
Looking up the image
After taking an image you need to go into the local storage of your Android device to look at it. I’m just including it for the people who aren’t confident navigating in the local Android storage.
- Open the files app on your Android device
- Go to the local register
- Search for test.png
Create an Android Sound Recorder using Kotlin
The Android multimedia framework includes support for recording and playing audio. In this blog post, we’ll develop a…
So, by now you should have a pretty good idea how you can develop your own camera app using the Fotoapparat library. You have also got a good idea of how to request permission in the runtime.
The full source code of this project can be found on GitHub.
If you have found this useful, please consider recommending and sharing it with other fellow developers.
Originally published at gabrieltanner.org.