Xamarin.android's camera use

Source: Internet
Author: User

Android offers a lot of hard features. One of the camera functions is a war-fighting place.
Even the explosion of events such as "Samsung Door". The purpose of this article is only to call the Android System definition API, to get the photo displayed.

Now let's introduce a couple of Android-responsible Camera naming spaces.
It's more than that, but if you're in the same school in Visual Studio, you'll soon be able to remove it with tools.

usingSystem;usingSystem.Linq;usingSystem.Text;usingSystem.Collections.Generic;usingAndroid.os;usingAndroid.app;usingandroid.views;usingAndroid.widget;usingandroid.content;usingAndroid.runtime;usingAndroid.graphics;usingAndroid.provider;usingAndroid.Content.PM;usingjava.io;usingEnvironment =Android.OS.Environment;usingUri = Android.Net.Uri;


Should we check that the hardware is ready?

Private BOOL  New  Intent (mediastore.actionimagecapture);   IListpackagemanager.queryintentactivities (Intent, packageinfoflags.matchdefaultonly);    return NULL 0 ;}

Special attention, please.

New Intent (mediastore.actionimagecapture);

Here we use the Intent provided by the system to get the camera function.

And then we're ready to save our records.

 private  void   Createdirectoryforpictures () {app._dir  = new
     File (Environment.getexternalstoragepublicdirectory ( Environment.directorypictures),       Androidtips   );  if  (! bool  result =app._dir.        Mkdirs (); Debug.WriteLine (  mkdir:   " + result); }} 


The method of preparing the Bitmap (change the size to ensure proportional display)

 Public Static classbitmaphelpers{ Public StaticBitmap Loadandresizebitmap ( This stringFileName,intWidthintheight) {//First we get the dimensions of the file on diskBitmapfactory.options Options =Newbitmapfactory.options {injustdecodebounds =true };       Bitmapfactory.decodefile (fileName, Options); intOutheight =options.       Outheight; intOutwidth =options.       Outwidth; intInsamplesize =1; if(Outheight > Height | | outwidth >width) {Insamplesize= outwidth >Outheight? Outheight/Height:outwidth/width; }       //Now we'll load the image and has bitmapfactory resize it for us.Options. Insamplesize =insamplesize; Options. Injustdecodebounds=false; Bitmap Resizedbitmap=bitmapfactory.decodefile (fileName, Options); returnResizedbitmap; }}

The way to prepare for the return

protected Override voidOnactivityresult (intRequestcode, Result ResultCode, Intent data) {    Base.    Onactivityresult (Requestcode, ResultCode, data); //Make it available in the galleryIntent mediascanintent=NewIntent (Intent.actionmediascannerscanfile); Uri Contenturi=Uri.fromfile (App._file);    Mediascanintent.setdata (Contenturi);    Sendbroadcast (mediascanintent); //Display in ImageView.    We'll resize the bitmap to fit the display. //Loading The full sized image would consume to much memory//and cause the application to crash.    intHeight =Resources.DisplayMetrics.HeightPixels; intwidth =Resultimageview.height; App.bitmap=App._file.    Path.loadandresizebitmap (width, height); if(App.bitmap! =NULL) {resultimageview.setimagebitmap (APP.BITMAP); Debug.WriteLine ("Contenturi:"+Contenturi); App.bitmap=NULL; }    //Dispose of the Java side bitmap.GC. Collect ();}

So you can quickly adjust the function of the camera.

Reference information: https://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/

Xamarin.android's camera use

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.