Android uses built-in camera app to capture images

Source: Internet
Author: User

The implementation of this demo is to call the camera installed on the phone to achieve the function of taking pictures, the photos taken in imageview form display.

Objective: To learn how to use mobile phone to install camera cameras, to understand the large picture processing, here mainly used in Bitmapfactory and bitmapfactory.options two classes.

Loading and displaying a pair of images has a significant impact on memory usage, and Android provides a useful program class called Bitmapfactory, which provides a series of static methods that agree to load bitmap images from various sources.

For our needs, the images will be loaded from the file. and show it in the initial activity. Fortunately, the methods available in Bitmapfactory will call the Bitmapfactory.options class. This allows us to define how to read bitmap into memory. In detail, when the image is loaded. You can set the sample size that bitmapfactory should use. Specifies the insamplesize parameter in the bitmapfactory.options.

Like what. Will insamplesize = 8 o'clock. The size of the resulting picture is 1/8 of the original size. Note that the Bitmapfactoryoptions.injustdecodebounds variable should first be set to true, which notifies the Bitmapfactory class to simply return the range of the image. Without trying to decode the image itself.

Finally, set the BitmapFactory.Options.inJustDecodeBounds to False. Finally, it is really decoded.

Realize:

Source:

Activity_main Layout file:

<linearlayout 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 "    android:orientation= "vertical" >    <imageview        android:id= "@+id/imageview"        android:layout_ Width= "Wrap_content"        android:layout_height= "Wrap_content"/></linearlayout>

Mainactivity Source:


Package Com.multimediademo1;import Java.io.file;import Android.app.activity;import android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.net.uri;import Android.os.Bundle; Import Android.os.environment;import Android.view.display;import Android.widget.imageview;public class MainActivity Extends Activity {private final static int camera_result = 0;private ImageView imageview;private String imagefilepath; @Ove rrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Imagefilepath = Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/ Myfavoritepicture.jpg "; File ImageFile = new file (Imagefilepath); Uri Imagefileuri = Uri.fromfile (imagefile); Intent Intent = new Intent (android.provider.MediaStore.ACTION_IMAGE_ CAPTURE); Intent.putextra (Android.provider.MediaStore.EXTRA_OUTPUT, Imagefileuri); Startactivityforresult (Intent, Camera_result);} @Overrideprotected void ONactivityresult (int requestcode, int resultcode,intent Intent) {super.onactivityresult (Requestcode, ResultCode, Intent); if (ResultCode = = RESULT_OK) {ImageView = (ImageView) Findviewbyid (r.id.imageview);D isplay currentdisplay = Getwindowmanager (). Getdefaultdisplay (); int dw = Currentdisplay.getwidth (); int dh = currentdisplay.getheight ();// Load the dimensions of the image, not the image itself bitmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = true; Bitmap Bitmap = Bitmapfactory.decodefile (Imagefilepath, options); int heightratio = (int) Math.ceil (Options.outheight/( float), int widthRatio = (int) Math.ceil (Options.outwidth/(float) DW);//Assuming two ratios are greater than 1. Then an edge of the image will be larger than the on screen if (HeightRatio > 1 && widthRatio > 1) {if (HeightRatio > WidthRatio) {//If the height ratio is greater, then it is scaled Opti Ons.insamplesize = HeightRatio;} else {options.insamplesize = WidthRatio;}} Options.injustdecodebounds = False;bitmap = Bitmapfactory.decodefile (Imagefilepath, Options); Imageview.setimagebitmap (bitmap);}}}

SOURCE Download:

Click to download source code


Android uses built-in camera app to capture images

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: info-contact@alibabacloud.com 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.