In android, Camera is useful to capture the photos and videos in our applications. By using camera API we can control the functionalities of camera based on our requirements.
The android framework provides a two ways such as android.hardware.camera2 API and camera intent to capture the images and videos in our application.
It’s a primary API for controlling the device cameras. By using this we can take the pictures or videos from our application using camera.
By using intent action types either MediaStore.ACTION_IMAGE_CAPTURE or MediaStore.ACTION_VIDEO_CAPTURE, we can capture the photos or videos without directly using the Camera object.
The best way is to use an Intent to invoke an existing Android camera application to take pictures or videos in our application without writing a lot of extra code.
In android, By using startActivityForResult() method with intent action parameter MediaStore.ACTION_IMAGE_CAPTURE, we can take the pictures from our android applications.
Following is the code snippet to capture the pictures using intent object with action parameter MediaStore.ACTION_IMAGE_CAPTURE in android applications.
If you observe above code snippet, we used startActivityForResult() method with MediaStore.ACTION_IMAGE_CAPTURE intent action parameter to capture the photos. The second parameter Image_Capture_Code is a locally defined integer that must be greater than 0.
Following is the example of using an existing camera app in our android applications to capture the photos on button click.
Create a new android application using android studio and give names as CameraExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
Once we create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="@+id/btnTakePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take a Photo"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/capturedImage"
android:layout_above="@+id/btnTakePicture"/>
</RelativeLayout>
Now open your main activity file MainActivity.java from \java\com.tutlane.cameraexample path and write the code like as shown below
If you observe above code snippet, we used startActivityForResult() method with MediaStore.ACTION_IMAGE_CAPTURE intent action parameter to capture the photos.
When we run the above program in the android studio we will get the result as shown below.
When we click on Take a Photo button, the camera will start and we can take the picture of whatever we want, the captured image will be shown in defined imageview.
This is how we can use the camera in android applications to capture the photos or videos based on our requirements.