Many developers want to use the program to call the camera and process the photos they have taken. First, preview the program.
First, design the Home Page. It is very simple, just adding a button and an image.
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/camera" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /></LinearLayout>
The next step is to process the button event. When you press the button, the local camera will be called.
// Image storage address: imagefilepath = environment. getexternalstoragedirectory (). getabsolutepath () + "/mypicture.jpg"; file imagefile = new file (imagefilepath); Uri imagefileuri = Uri. fromfile (imagefile); intent I = new intent (Android. provider. mediastore. action_image_capture); I. putextra (Android. provider. mediastore. extra_output, imagefileuri); startactivityforresult (I, camera_result );
Intent directly calls the camera program of the Local Machine and saves the picture in the memory card. Next, we need to display the picture in our own program.
Here we use the onactivityresult method in the activity directly. This method is used to process the returned results of the activity.
@ Overrideprotected void onactivityresult (INT requestcode, int resultcode, intent) {// todo auto-generated method stubsuper. onactivityresult (requestcode, resultcode, intent); // if the photo is taken successfully if (resultcode = result_ OK) {// bundle extras = intent. getextras (); // bitmap BMP = (Bitmap) extras. get ("data"); IMV = (imageview) findviewbyid (R. id. imageview1); // obtain the display size of the screen display currentdisplay = getwindowmanager (). getdefadisplay display (); int DW = currentdisplay. getwidth (); int DH = currentdisplay. getheight (); // scale the bitmapfactory image. options BMP factoryoptions = new bitmapfactory. options (); BMP factoryoptions. injustdecodebounds = true; bitmap BMP = bitmapfactory. decodefile (imagefilepath, BMP factoryoptions); int heightratio = (INT) math. ceil (BMP factoryoptions. outheight/(float) DH); int widthratio = (INT) math. ceil (BMP factoryoptions. outwidth/(float) dw); If (heightratio> 1 & widthratio> 1) {If (heightratio> widthratio) {BMP factoryoptions. insamplesize = heightratio;} else {BMP factoryoptions. insamplesize = widthratio;} BMP factoryoptions. injustdecodebounds = false; BMP = bitmapfactory. decodefile (imagefilepath, BMP factoryoptions); IMV. setimagebitmap (BMP );}}
Here we have completed the call to the local camera. In fact, here we mainly want to learn the startactivityforresult and onactivityresult methods. One is to call the activity and return the result to the called activity, one is to process the returned data. Well, if anyone wants a program, they can leave the email address directly.