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

好了,整个程序就到这里了。








阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_26562875/article/details/49998881
个人分类: Xamarin Android
想对作者说点什么? 我来说一句

Xamarin技术讲座——初级篇

-

学院

1970年01月01日 08:00

没有更多推荐了,返回首页

不良信息举报

Xamarin.Android 照相机的使用

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭