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.
[Html] <? 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>
<? 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.
[Java] // The image storage address www.2cto.com
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 );
// 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.
[Java] @ Override
Protected void onActivityResult (int requestCode, int resultCode,
Intent intent ){
// TODO Auto-generated method stub
Super. 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 image
BitmapFactory. 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 );
}
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode,
Intent intent ){
// TODO Auto-generated method stub
Super. 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 image
BitmapFactory. 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.
From the column kangkangz4