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.
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
I am not gonna talk about the Camera2 API in this post, but might do that in another one later on.
- It is recommended to open the camera from a separate thread that is launched from
onCreateas 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
onResumein 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:
- It is important to close the camera onPause.
- It is also important to remember to release the camera in case it failed to start just to make sure it was released.
- When releasing the camera one should notice that its not enough to just execute
camera.release()but the following:
- Every time a change is happening with the camera, the preview must be also restarted in the preview class
- Set the Preview Orientation.
- After taking a picture it is important to restart the preview.
- You must stop the preview before releasing the camera.
- Changing the preview's size might require some extra work, which will be explained in my next post. Basically you might want to read more about getSupportedPreviewSizes.
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().
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