Xamarin.Android 照相機的使用

這個板塊呢,我想展示如何調用照相機,如何保存其路徑,然後在我們imageView裏面展示

出來

先上最終效果圖(用的是Genymotion模擬器)


新鍵一個Layout,命名爲CameraLayout.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/CameraButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/openCamera" />
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_height="300.0dp"
        android:id="@+id/imageView1"
        android:adjustViewBounds="true" />
</LinearLayout>

新建一個類,命名爲CameraHelper,方法CreateDirectoryForPictures  是判斷文件夾是否存在,如果不存在則新建文件夾;

方法LoadandResize則是爲了整理圖片的大小

  class CameraHelper
    {
        public static File _file;
        public static File _dir;
        public static Bitmap bitmap;


        public static void CreateDirectoryForPictures()
        {
           _dir = new File(
                Android.OS.Environment.GetExternalStoragePublicDirectory(
                    Android.OS.Environment.DirectoryPictures), "CameraAppDemo");
            if (!_dir.Exists())
            {
                _dir.Mkdirs();
            }
        }

        public static Bitmap LoadAndResizeBitmap( 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);

            // Next we calculate the ratio that we need to resize the image by
            // in order to fit the requested dimensions.
            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;
        }


在我們的Activity中,代碼如下

爲方便找到我們的界面元素,先定義元素

  private ImageView _imageView;
        private Button button;


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.CameraLayout);

            CameraHelper.CreateDirectoryForPictures();
            button = FindViewById<Button>(Resource.Id.CameraButton);
            _imageView = FindViewById<ImageView>(Resource.Id.imageView1);
            button.Click += TakeAPicture;

        }

其實呢,應該先判斷我們的手機是否存在能照相的應用程序,但是我這裏有點問題,到後面我再整理一下

叫起照相機,進行拍照

  private void TakeAPicture(object sender, EventArgs eventArgs)
        {
            var intent = new Intent(MediaStore.ActionImageCapture);
            CameraHelper._file = new File(CameraHelper._dir, string.Format("myPhoto_{0}.jpg", Guid.NewGuid()));
            intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(CameraHelper._file));
            StartActivityForResult(intent, 0);
        }

當程序有返回值時

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            //判斷可用
            var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
            var contentUri = Uri.FromFile(CameraHelper._file);
            mediaScanIntent.SetData(contentUri);
            SendBroadcast(mediaScanIntent);


            var height = Resources.DisplayMetrics.HeightPixels;
            var width = _imageView.Height;
            CameraHelper.bitmap = CameraHelper.LoadAndResizeBitmap(CameraHelper._file.Path, width, height);
            if (CameraHelper.bitmap != null)
            {
                _imageView.SetImageBitmap(CameraHelper.bitmap);
                CameraHelper.bitmap = null;
            }
            //釋放資源
            GC.Collect();
        }

好了,整個程序就到這裏了。








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章