Working with the Camera

There are many ways to incorporate camera hardware into your application. You can build camera support directly into your application, or you can integrate existing camera support functionality into your application by using the Intent mechanism.

For more fine-tuned control over the handset camera hardware, you can use the android.hardware.Camera class to connect to the Camera service on the device, configure settings, and take photos and video. Your application will require the android.permission.CAMERA permission to access the camera hardware on the handset.

Did you Know?

By far, the simplest way to include photo-taking abilities in an application is by launching the ACTION_IMAGE_CAPTURE intent. For example, you could add the following code to the onClick() method of the avatar ImageButton control's OnClickListener:

Intent pictureIntent = new Intent(

android.provider.MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(pictureIntent, TAKE_AVATAR_CAMERA_REQUEST);

There is no camera available on the Android emulator. Instead, a mock camera screen is shown, and a fixed graphic is saved whenever the user chooses to take a picture. This is helpful for testing camera functionality using the Android emulator. When you run the application and click the avatar ImageButton control, the emulator screen should look something like Figure 13.4.

FIGURE 13.4

Taking a photograph using the camera application in the Android emulator.

Did you Know?

When launching a "remote" activity—that is, an activity that is not necessarily part of your application—you are effectively sending out an intent that says, "I want to do this. Who can do it for me?" A number of other applications on the handset may have the ability to handle this operation. The Android operating system attempts to match the most appropriate activity to handle the request. However, if you want the user to be shown a list of applicable activities (or applications) to handle the request, simply wrap your intent within another intent called ACTION_CHOOSER. You often see this mechanism used with common applications such as messaging applications (for example, "Which application do you want to use to send this message?"). You can wrap an intent within a chooser by using the createChooser() method, like this: Intent.createCftooser(innerIntent,

"Choose which application to handle this");

Although most handsets have only one image-capturing application, as a developer, you are better off not making assumptions of this sort.

The ACTION_IMAGE_CAPTURE intent action causes the camera application to launch, allows the user to take a photograph, and returns the photo. By default, a small bitmap is returned, and it is suitable for your avatar. Within a specific case statement of the onActivityResult() method for the request code

TAKE_AVATAR_CAMERA_REQUEST, you can retrieve the bitmap by inspecting the

Intent parameter called data, as follows:

Bitmap cameraPic = (Bitmap) data.getExtras().get("data");

You can then pass the bitmap graphic into your helper method saveAvatar().

0 0

Post a comment