@shakedko
IF AN EXPERT SAYS IT CAN'T BE DONE GET ANOTHER EXPERT.
- DAVID BEN-GURION

Android Camera Development - Basics

Description

As part of my upcoming post about writing an Android portrait camera app I have decided to post few pre posts that might be important to understand.

This post, and related ones such as Android Development and Threads might be relevant for some readers and I strongly suggest going over them in case you are not sure about these topics.

Permissions

From Android documentation for the Camera API:

To access the device camera, you must declare the CAMERA permission in your Android Manifest. Also be sure to include the manifest element to declare camera features used by your application. For example, if you use the camera and auto-focus feature, your Manifest should include the following

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

This class was deprecated in API level 21. Android's recommendation is to use android.hardware.camera2.

I am not gonna talk about the Camera2 API in this post, but might do that in another one later on.

Open Camera

  • It is recommended to open the camera from a separate thread that is launched from onCreate as it might slow down the UI thread - this raises an interesting question when the UI is dependent on the camera preview size, I will cover this topic in my future post writing an Android portrait camera app.
  • In some cases it might be enough to use the onResume in order to open the camera.
  • The camera should be opened only once, otherwise an exception will be thrown. This means that you should make sure you catch the exception and handle it.

Your application should only have one Camera object active at a time for a particular hardware camera.

  • Since API 9 the Camera API supports an argument to choose which camera should be opened.
  • In case you, for some reason, use API 9, you might want to make sure that you are not using Android's constants:

Close Camera

Camera Preview

Taking a Picture

From Android's documentation:

Use the Camera.takePicture() method to take a picture once the preview is started. You can create Camera.PictureCallback and Camera.ShutterCallback objects and pass them into Camera.takePicture().

If you want to grab images continously, you can create a Camera.PreviewCallback that implements onPreviewFrame(). For something in between, you can capture only selected preview frames, or set up a delayed action to call takePicture().

Summary

As mentioned above, this is a very basic post about how to work with Android's camera. Everything above is mentioned in Android's documentation, most of it can be found in Android's page Controlling the Camera and in the Camera Documentation