Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel

Free Preview: Take Pictures With Your Android App

Introduction

00:54
  • Overview
  • Transcript

The Google Play store has dozens of camera apps—each of them enabling a different way of taking photos or doing something unique with the pictures from the camera. Have you ever wondered what goes into creating such an app? What APIs are involved? This course will answer those questions!

In this course, Envato Tuts+ instructor Ashraff Hathibelagal will show you how to create an Android app that allows users to take photos and apply image filters to them. You'll use the Camera2 API, introduced in Android Lollipop, to create a live camera preview and capture high-resolution still pictures. The Camera2 API expects you to manage a lot of low-level details yourself. Therefore, you'll also learn how to control the state of the auto-focus and auto-exposure algorithms of an Android device's camera hardware. Furthermore, you'll learn how to store the photos on external storage media, such as SD cards. Towards the end of this course, you'll also learn to use the Android GPUImage library to apply several commonly used image filters, such as sepia, grayscale, and toon, to the photos.

1. Introduction

1.1 Introduction

If you go to Google Play and search for the keyboard camera, you're gonna find dozens of camera apps. Each of them allow users to either take photos in a novel manner, or do something unique with the photos. Have you ever wondered what goes into creating this type of apps, how much effort is required, or what APIs are involved? Well, this course should answer all those questions for you. I'm Ashraff Hathibelagal, and today we're gonna be creating a simple Android app that will not only allow users to take photos, but also apply various Instagram-like filters them. We'll be using the new and very powerful camera2 API, because the older API is now deprecated. Over the next 45 minutes or so, you're going to learn how to create a live camera preview, take a photo, apply various filters to it using a library called Android GPUImage, and finally save the modified photo. So when you're ready, just open the next lesson.

1.2 Project Setup

Hello and welcome back. In this lesson we create a new Android Studio project and configure it, so that it has all the required permissions, dependencies, and assets. I'll be using Android Studio2.1.1 throughout this course. All right, let's start by selecting the Start a new Android Studio project option in the welcome menu. I'm going to name this application My Camera. The camera to API was introduced in Android 5.0. Therefore, the Minimum SDK we can support here is API level 21. Choose the empty activity in the screen and press Next and then press Finish. Once the project has been generated, open its manifest file. Here, we must add a uses-permission tag in order to get the permission to use the camera. Set the name attribute of the tag to android.permission.CAMERA. Similarly add another uses-permission tag in order to get the permission to write to the external storage. This is required because we'll be storing the photos we capture on external storage media such as SD cards. To keep this app simple, we won't be handling any device orientation changes. So set the screen orientation attribute of main activity to portrait. This way, the activity will always be in the portrait mode. Next, open build.gradle so that we can specify the libraries we'll be using in this project. The first library we are gonna need is the design support library. So copy this line, paste it and just change appcompat-v7 to design. Next, we'll be using a popular library called Android GPUImage. This library allows us to apply various image filters to the photos. To add it, just copy this line here and paste it in your build.gridle file, press Sync Now to sync the dependencies we added. Let's add a vector asset to our project now. Open Vector Asset Studio and click on the choose button. Open the image category here and select the camera icon, press OK > Next and Finish. At this point, our project is fully configured for devices running API level 22 or lower. Now in order to support devices running API level 23 or higher, we must request the permissions be mentioned in a manifest file again during runtime. So open main-activity.java. Here create a new method called requestPermissions. Inside the method, check if the user has granted the required permissions using the context compact.checkselfpermission method. So first check if the result for the camera permission is equal to PackageManager.PERMISSION_GRANTED. And then check if the result for the right external storage permission is equal to PackageManager.PERMISSION_GRANTED. If you have both permissions, we don't have to do anything for now, but if you don't have those permissions, we must ask for them using the ActivityCompat.requestPermissions method. Pass both the permissions to the method in the form of a string array. We must also pass a request code to it. I'm gonna call it MY_REQUEST_CODE and make it a constant field of the activity. Its value can be any integer, I'll say 1. To handle the result of our request, we must override a method called onRequestPermissionsResult. Here, check if the request code is equal to MY_REQUEST_CODE. And then check if the grantResults array has two results. Next, check if both the results are equal to PackageManager.PEMISSION_GRANTED. If that is true, we don't have to do anything for now. But if it is not true, it means that the user has not granted us the permissions we need. So let us create a toast message using the Toast.makeText method saying Permissions not granted. And then call it show method to display the message. Our app cannot run without the permission. So at this point, call finish to close the app. If you press the run button now and choose a device running API level 23 or higher, You're going to see something like this as soon as the app starts. This brand is of course for the camera permission. Press allow and this prompt is for the right external storage permission. Press Allow again. Our project setup is now complete. In the next lesson, we'll start using the camera to API to create a live preview interface. I'll be waiting for you there. Thanks for watching.