Xamarin. Android camera, xamarin. android

Source: Internet
Author: User

Xamarin. Android camera, xamarin. android

Android provides many hardware functions. Among them, camera functions are a must for the military.
Even a trigger such as a Samsung attack. However, the purpose of this article is to call the Android system-defined API and show it after obtaining the photo.

Now, we will introduce the namespace for processing Camera in several Android regions.
Of course there will be more, but if you develop a fellow in Visual Studio, you will soon be able to use tools to remove it.

using System;using System.Linq;using System.Text;using System.Collections.Generic;using Android.OS;using Android.App;using Android.Views;using Android.Widget;using Android.Content;using Android.Runtime;using Android.Graphics;using Android.Provider;using Android.Content.PM;using Java.IO;using Environment = Android.OS.Environment;using Uri = Android.Net.Uri;


Check whether the hardware configuration has been renewed first?

private bool IsThereAnAppToTakePictures (){  Intent intent = new Intent (MediaStore.ActionImageCapture);   IList<ResolveInfo> availableActivities = PackageManager.QueryIntentActivities (intent, PackageInfoFlags.MatchDefaultOnly);   return availableActivities != null && availableActivities.Count > 0;}

Please note that

Intent intent = new Intent (MediaStore.ActionImageCapture);

Here we use the Intent provided by the system to obtain camera functions.

Then, we saved our contents.

private void CreateDirectoryForPictures (){    App._dir = new File (    Environment.GetExternalStoragePublicDirectory ( Environment.DirectoryPictures), "AndroidTips");    if (!App._dir.Exists ())    {        bool result =App._dir.Mkdirs( );        Debug.WriteLine ("mkdir:" + result);    }}      


Processing Bitmap (change the size to ensure the performance is displayed proportionally)

public static class BitmapHelpers{  public static Bitmap LoadAndResizeBitmap (this string fileName, int width, int height)   {    // First we get the the dimensions of the file on disk       BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };       BitmapFactory.DecodeFile (fileName, options);       int outHeight = options.OutHeight;       int outWidth = options.OutWidth;       int inSampleSize = 1;       if (outHeight > height || outWidth > width)       {         inSampleSize = outWidth > outHeight            ? outHeight / height : outWidth / width;       }       // Now we will load the image and have BitmapFactory resize it for us.       options.InSampleSize = inSampleSize;       options.InJustDecodeBounds = false;       Bitmap resizedBitmap = BitmapFactory.DecodeFile (fileName, options);       return resizedBitmap;  }}

 

How to restore objects

protected override void OnActivityResult (int requestCode, Result resultCode, Intent data){    base.OnActivityResult (requestCode, resultCode, data);    // Make it available in the gallery    Intent mediaScanIntent = new Intent (Intent.ActionMediaScannerScanFile);    Uri contentUri = Uri.FromFile (App._file);    mediaScanIntent.SetData (contentUri);    SendBroadcast (mediaScanIntent);    // Display in ImageView. We will resize the bitmap to fit the display.    // Loading the full sized image will consume to much memory    // and cause the application to crash.    int height = Resources.DisplayMetrics.HeightPixels;    int width = 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();}

 

In this way, you can quickly use the camera function.

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

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.