[Tutorial] How to use camera with Android and Android Studio
It’s a really short and simple tutorial, I’m just going to show you how to use camera with Android Studio.
I’m creating this tutorial because it’ll be a requirement for some new posts about image processing with Android.
I - About Android Tutorials
I’ll be creating a big number of Android tutorials, showing you how to use nice libraries, UI, tips, and more.
I’ll be using Android Studio and Gradle in all tutorials.
If you need some help with any Android lib or feature, feel free to comment here and if possible, I can write a new tutorial about it :)
II - Creating a new project
Open your Android Studio and create a new blank activity application.
The first step is to add the android permissions and required features.
Add these lines to your AndroidManifest.xml:
Inside your style.xml, just edit the line to remove the ActionBar, if you prefer(this is removed just to have a full-screen camera):
Create this basic activity_main.xml:
Here we have a simple FrameLayout.
The camera_view will display the camera data, that we’ll use later with OpenGL and some algorithms to process it.
And the ImageButton will be used to close the application.
III - Camera code
The first step is to create a SurfaceView. It’ll receive camera data and display it inside the FrameLayout.
Create a new file, CameraView.java in the same folder that your MainActivity.java is.
First, just extend the SurfaceView and implements the SurfaceHolder.Callback.
You’ll have something like this code:
Add these two private variables:
And then just update your CameraView constructor with:
Now, override these methods, I commented everything to help you:
III - Camera Activity
Now, inside you MainActivity.java, add these variables:
No, override the method bellow, I commented to help you to understand it:
Now, you can run your app and you’ll see something like this:
That’s it. This post is really basic, but I’m going to write more on how to use QR, OpenGL and some image processing algorithm, and I prefer to have the necessary background here.