Android--Camera (1)

Source: Internet
Author: User

Capturing images using the built-in camera application

All original Android devices with the right hardware (webcam) will come with the camera application. The camera application contains an intent filter (Intent Filter) that enables developers to provide the same image capture capabilities as the camera application without having to build their own custom capture routines.

The camera application specifies the following intent filters in its manifest file. The intent filter shown here is contained within the "Camera" activity tag.

< Intent-filter > <  android:name= ""/><   android:name= "Android.intent.category.DEFAULT"/></  Intent-filter>

In order to use the camera application with an intent, all we have to do is construct an intent that will be captured by the above filter.

New Intent ("");

In practice, we may not want to create an intent directly using an action string. In this case, you can specify the constant action_image_capture in the Mediastore class. The reason that a constant should be used instead of the string itself is that if the string changes (and of course the constants can change constantly), then using constants will make our calls more conducive to future changes than using strings.

New Intent (Android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  StartActivity (i);
returning data from the camera application

Of course, capturing an image using the built-in camera application will not really work if the camera application does not return the picture to the calling activity when capturing a picture. And in order to make it really useful, you can replace the StartActivity method in the activity with the Startactivityforresult method. Using this method will allow us to access the data returned from the camera application, which happens to be an image captured by the user in bitmap (Bitmap) Form.; Importandroid.content.Intent;; ImportAndroid.os.Bundle; ImportAndroid.widget.ImageView;  Public classCameraintentextendsActivity {Final Static intCamera_result = 0;      ImageView IMV; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);          Setcontentview (R.layout.main); Intent I=NewIntent (Android.provider.          Mediastore.action_image_capture);      Startactivityforresult (i, Camera_result); }      protected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) {          Super. Onactivityresult (Requestcode, ResultCode, intent); if(ResultCode = =RESULT_OK) {Get Bundle Extras=Intent.getextras (); Bitmap BMP= (Bitmap) extras.get ("Data")); IMV=(ImageView) Findviewbyid (;          Imv.setimagebitmap (BMP); }      }  }
 xml version= "1.0" encoding= "Utf-8"  <  linearlayout  xmlns:android  = ""   android:orientation  = "Vertical"   Android:layout_width  = "Fill_parent"   Android:layout_height  = "Fill_parent"  <  imageview  
Androidandroid:id = "@+id/returnedimageview"
Android:layout_width =  "Wrap_content"
Android:layout_height = "Wrap_content" >
</ ImageView > </ LinearLayout >

The camera application returns an image in an added value (extra) that is passed through intent, and the intent is passed to the keynote activity in the Onactivityresult method. The added value is named "Data", which contains a bitmap object that needs to be cast from a generic object.

// gain added value  from intent Bundle Extras = Intent.getextras ();    // get the returned image  from the added value Bitmap bmp = (Bitmap) extras.get ("Data");

In our layout XML (layout/main.xml) file, there is a ImageView object. ImageView is an extension of the generic view that supports the display of images. Since we have a ImageView object with the specified Returnedimageview number (ID), we need to get its reference in the activity and set its bitmap object to the returned image through the Setimagebitmap method. This will enable the application user to view the captured image.

To reference ImageView and notify it to display the bitmap object from the camera, use the following code:

IMV = (ImageView) Findviewbyid (;  Imv.setimagebitmap (BMP);

Reprint Please specify source:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.