We know that all devices with cameras come with a camera application, and the camera application contains an intent filter for the camera
Gets the application image.
In order to use the camera application with an intent, we need to construct an intent filter, which can be defined as:
Intent intent=new Intent ("Android.media.action.IMAGE_CAPTURE");
However, this is not recommended in real-world development, when we can specifyMediastoreConstants in a classaction_image_capture, use such
The benefit is to facilitate future changes. Therefore, we define this:
Intent intent=new Intent (Android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
In order to get the image from the camera application, you should change the startactivity toStartactivityforresultMethod:
Startactivityforresult (Intent, 0);
In this case we get the returned data from the Onactivityresult method:
@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data); if (RESULTCODE==RESULT_OK) {Bundle extras=data.getextras (); Bitmap bitmap= (Bitmap) extras.get ("Data");}}
The camera application returns an image through an intent-passed value added (extra), which is passed to the keynote in the Onactivityrsult method
Move. The added value is named "Data", which contains a bitmap object that needs to be cast from a generic object.
The above is only a small image, OK, this actually gets a thumbnail.
The above just get a small thumbnail, how to get a big picture, at the beginning of Android1.5, on most devices can be a value added to the
The intent of triggering the camera application, the name of this added value is specified in the Mediastore class, which is a constant, called extra_output.
This added value is used to instruct the camera application where to store the captured images. For example, the following code:
</pre><pre name= "code" class= "Java" >stringmfilepath=environment.getexternalstoragedirectory (). GetAbsolutePath () + "/bill.jap"; File File=new file (Mfilepath); mimageuri=uri.fromfile (file); Intent intent=new Intent ( Android.provider.MediaStore.ACTION_IMAGE_CAPTURE); Intent.putextra (Android.provider.MediaStore.EXTRA_OUTPUT, Mimageuri); Startactivityforresult (intent, 0);
Loading and displaying an image has an impact on memory usage, but fortunately, Android provides a utility class called bitmapfactory , the program class
Provides a series of static methods that allow bitmap images to be loaded from various sources. The methods available in Bitmapfactory will be called
Bitmapfactory.options class, which allows us to define how to read bitmap into memory. When the image is loaded, you can set the Bitmapfactory
Using the sample size, specify the insamplesize parameter in Bitmapfactory.options. Like what:
Bitmapfactory.options option=new bitmapfactory.options (); option.insamplesize=5; Bitmap bitmap=bitmapfactory.decodefile (mfilepath, option);
The above is a pair of images that will have a size of 1/5 of the original image size.
This is a way to accelerate the loading of large images, but does not really consider the original size of the image, and does not consider the size of the screen, the following is the ruler to get the screen
Inch:
Displaymetrics metrics=new displaymetrics (); This.getwindowmanager (). Getdefaultdisplay (). GetMetrics (metrics); Mwindowheight=metrics.heightpixels;mwindowwidth=metrics.widthpixels;
To determine all the dimensions of the image, we used the bitmapfactory and bitmapfactory.options, and
The BitmapFactory.Options.inJustDecodeBounds variable is set to True. This informs the Bitmapfactory class to return only the range of the image, without
Attempts to decode the image itself. When using this method, the BitmapFactory.Options.outHeight and BitmapFactory.Options.outWidth variables will be
are assigned values, for example:
Bitmapfactory.options mbitmapfactory=new bitmapfactory.options (); mbitmapfactory.injustdecodebounds=true; Bitmap bitmap=bitmapfactory.decodefile (Mfilepath, mbitmapfactory); int heightratio= (int) Math.ceil ( mbitmapfactory.outheight/(float) mwindowheight), int widthratio= (int) Math.ceil (mbitmapfactory.outwidth/(float) Mwindowwidth);
The image size can then be displayed by the height ratio or width ratio.
The following is a complete sample code:
Mainactivity:
public class Mainactivity extends Activity {private ImageView iv_show;private Button btn_open;private Uri Mimageuri;priva Te String mfilepath;private int mwindowwidth;private int mwindowheight; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitView (); Initevent (); InitData ();} private void InitData () {mfilepath=environment.getexternalstoragedirectory (). GetAbsolutePath () + "/bill.jap"; File File=new file (Mfilepath), mimageuri=uri.fromfile (file);D isplaymetrics metrics=new displaymetrics (); This.getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics); mwindowheight=metrics.heightpixels; Mwindowwidth=metrics.widthpixels;} private void Initevent () {Btn_open.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) { Intent intent=new Intent (Android.provider.MediaStore.ACTION_IMAGE_CAPTURE); Intent.putextra ( Android.provider.MediaStore.EXTRA_OUTPUT, Mimageuri); Startactivityforresult (intent, 0);});}private void Initview () {iv_show= (ImageView) Findviewbyid (r.id.iv_show); btn_open= (Button) Findviewbyid (r.id.btn_ Open);} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data); if (RESULTCODE==RESULT_OK) {/* * Load image size */bitmapfactory.options mbitmapfactory=new Bitmapfactory.options (); mbitmapfactory.injustdecodebounds=true; Bitmap bitmap=bitmapfactory.decodefile (Mfilepath, mbitmapfactory); int heightratio= (int) Math.ceil ( mbitmapfactory.outheight/(float) mwindowheight), int widthratio= (int) Math.ceil (mbitmapfactory.outwidth/(float) Mwindowwidth)/* * Determine if the image exceeds the screen */if (heightratio>1&&widthratio>1) {if (heightratio>1) { Mbitmapfactory.insamplesize=heightratio;} Else{mbitmapfactory.insamplesize=widthratio;}} /* * Decode */mbitmapfactory.injustdecodebounds=false;bitmap=bitmapfactory.decodefile (Mfilepath, mBitmapFactory); iv_ Show.setimagebitmap (bitmap);}}}
Layout file (Activity_main):
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "> <Scro Llview android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <linearlayout Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:orientation = "vertical" > <button android:id= "@+id/btn_open" android:layout_width= "fill _parent "android:layout_height=" 50DP "android:text=" Open "/> <imageview Android:id= "@+id/iv_show" android:layout_width= "Wrap_content" Android:layout_hei ght= "Wrap_content"/> </LinearLayout> </ScrollView></RelativeLayout>
Finally, don't forget to add the appropriate permissions to Androidmanifest:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/45696873 Emotional Control _
Learn about Android from Camera capture Image (*)