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

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








  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值