Android 提供了不少硬體功能。其中照相機功能更是兵家必爭之地。
甚至爆發如「三星門」等事件。不過本篇文章的目的只呼叫Android系統定義的API,取得相片後顯示出來。

現在我們先引入幾個 Android 負責處理 Camera 的命名空間。
當然會多些,不過要是在 Visual Studio 內開發的同學,很快就可以用工具移除。

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;


我們先檢查硬體配備是否已準備好?

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

特別請大家注意

Intent intent = new Intent (MediaStore.ActionImageCapture);

在這邊我們使用系統提供的 Intent 來取得照相機的功能。

然後我們準備存儲目錄

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);    }}      


準備處理 Bitmap 的方法(改變大小確保能按比例顯示)

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;  }}

 

準備回調的方法

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();}

 

如此即可快速調用照相機的功能。

程式碼:https://github.com/FangHuaiAn/Xamarin-AndroidTips
參考資料:https://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/

转载于:https://www.cnblogs.com/Liddle/p/5403616.html

更多相关文章

  1. Android关于socket编程,实现与服务器端通信
  2. Android学习问题:关于AlertDialog中自定义布局带有的EditText无法
  3. AsyncTask类的用法
  4. Android(安卓)View绘制及实践
  5. Android(安卓)中使用WebViewJavaScriptBridge进行H5和原生的交互
  6. Android歌词秀设计思路(1)SafetyTimer
  7. Android(安卓)View系列(一):View的基础知识
  8. Android(安卓)进程间通信——AIDL
  9. Android(安卓)自定义View--ProgressBar篇(三)

随机推荐

  1. Android(安卓)Gradle 编译问题汇总
  2. Android(安卓)SDK大连东软镜像地址及地址
  3. 详解android:scaleType属性
  4. 把TextView中的文字添加阴影效果及Style
  5. 黑马程序员之手机卫士第八天
  6. Android(安卓)EditView 获取焦点 不弹出
  7. Android(安卓)ListView xml配置
  8. android 图片自动切换
  9. Android应用开发相关下载资源
  10. 【30篇突击 android】源码统计 十