MonoDroid的,要考虑与相机拍摄照片 [英] Monodroid Take a picture with Camera

查看:321
本文介绍了MonoDroid的,要考虑与相机拍摄照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有成功尝试过自己,我能做到这一点在Java中,但它是在C#一个很好的有点不同。任何帮助将是great.All我想要的是:


  1. 启动相机。

  2. 拍摄照片。

  3. 查看图像视图照片。


解决方案

我知道这是一个相当古老的问题,但我会用我的code,我已经使用的回答。根据我的经验瞎搞这一点,不是所有的设备都能够使用标准的意图,并在的onActivityResult 将数据返回。

私人无效TakePicture()
    {
        如果(PackageManager.HasSystemFeature(PackageManager.FeatureCamera))
        {
            VAR意图=新意图(Android.Provider.MediaStore.ActionImageCapture);
            var文件=新File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), PictureTHIS);
            如果(!file.Exists())
            {
                如果(!file.Mkdirs())
                {
                    Log.Debug(Constants.LOG_TAG,无法创建目录来保存照片。);
                    返回;
                }
            }
            _file =新的文件(file.Path +文件分割符+IMG_+ DateTime.Now.ToString(YYYYMMDD_HHMMSS)+.JPG);
            intent.PutExtra(MediaStore.ExtraOutput,Uri.FromFile(_file));
            StartActivityForResult(意向,Constants.CAMERA_ACTIVITY);
        }
        其他
        {
            Toast.MakeText(这一点,此设备不有一个摄像头,请从其他选项中选择一个图像。ToastLength.Short).Show();
        }
    }

我用这个关于如何处理一些,不会返回回的意图的设备的参考。 <一href=\"http://kevinpotgieter.word$p$pss.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/\" rel=\"nofollow\">http://kevinpotgieter.word$p$pss.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/

保护覆盖无效的onActivityResult(INT申请code,结果导致code,意图数据)
    {
        base.OnActivityResult(要求code,结果code,数据);
        如果(结果code == Result.Ok)
        {
    //检查相机acitivy,我的应用程序同时允许画廊和相机检索图片。
            如果(要求code == Constants.CAMERA_ACTIVITY)
            {
        //有些设备不传回的意图使您的数据可能为空
                如果(数据= NULL&放大器;!&安培;!string.IsNullOrEmpty(data.DataString))
                {
        //完整的URI到图像设备。
        Android.Net.Uri.Parse(data.DataString);
                }
                其他
                {
                    //问题,即有些设备不从的意图传回正确的数据。
                    VAR的URI = GetImagePathFromCamera();
                    如果(URI的== NULL)
                    {
            //有与无SD卡部分设备的问题,所以我创建文件并将其存储在设备上
                        VAR方向= 0;
                        VAR日期=的String.Empty;
            // _文件是在得到额外指定的意图通过了java.io.File中。
                        尝试
                        {
                            VAR EXIF​​ =新ExifInterface(_file.Path);
                            方向= exif.GetAttributeInt(ExifInterface.TagOrientation,-1);
                            日期= exif.GetAttribute(ExifInterface.TagDatetime);
                        }
                        赶上{}                        开关(方向)
                        {
                            情况6:
                                Helpers.Orientation = 90;
                                打破;
                            案例3:
                                Helpers.Orientation = 180;
                                打破;
                            案例8:
                                Helpers.Orientation = 270;
                                打破;
                            默认:
                                Helpers.Orientation = 0;
                                打破;
                        }                        ContentValues​​值=新ContentValues​​();
                        values​​.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.DisplayName,_file.Name);
                        values​​.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.DateTaken,日期);
                        values​​.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.MimeType,图像/ JPEG);
                        values​​.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Orientation,Helpers.Orientation);
                        values​​.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data,_file.Path);                        VAR URI = ContentResolver.Insert(Android.Provider.MediaStore.Images.Media.ExternalContentUri,价值观);
                        //返回URI的路径到装置上的图像
                    }
                    其他
                    {
                        //的URI是URI的指定列表拍摄的图像和其缩略图
                    }
                };
            }
        }
        其他
    {
    //通知用户如果希望相机被取消
    }
}

I have tried it myself with no success, I can do it in java but it is a good bit different in C#. Any help would be great.All I want is to:

  1. Launch the camera.
  2. Take a photo.
  3. View the photo in an image view.

解决方案

I know this is a fairly old question, but I'll answer it with my code that I've used. In my experience messing around with this, not all devices are able to use the standard intent and return data back in the OnActivityResult.

private void TakePicture()
    {          
        if (PackageManager.HasSystemFeature(PackageManager.FeatureCamera))
        {
            var intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
            var file = new File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "PictureTHIS");
            if (!file.Exists())
            {
                if (!file.Mkdirs())
                {
                    Log.Debug(Constants.LOG_TAG, "Unable to create directory to save photos.");
                    return;
                }
            }
            _file = new File(file.Path + File.Separator + "IMG_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".jpg");
            intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));
            StartActivityForResult(intent, Constants.CAMERA_ACTIVITY);
        }
        else
        {
            Toast.MakeText(this, "This device does not have a camera, please select an image from another option.", ToastLength.Short).Show();
        }
    }

I used this as a reference on how to deal with some of the devices that would not return back an intent. http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/

      protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (resultCode == Result.Ok)
        {
    //checking for camera acitivy, my app allows both gallery and camera for retrieving pictures.
            if (requestCode == Constants.CAMERA_ACTIVITY)
            {                  
        //some devices do not pass back an intent so your data could be null
                if (data != null && !string.IsNullOrEmpty(data.DataString))
                {
        //full uri to where the image is on the device.
        Android.Net.Uri.Parse(data.DataString);
                }
                else
                {
                    //issue where some devices don't pass back correct data from the intent.
                    var uris = GetImagePathFromCamera();
                    if (uris == null)
                    {
            //had an issue with some devices with no sd card, so i create the file and store it on the device
                        var orientation = 0;
                        var date = string.Empty;
            //_file is a java.io.file that is passed in the intent with get extra specified.
                        try
                        {
                            var exif = new ExifInterface(_file.Path);
                            orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, -1);
                            date = exif.GetAttribute(ExifInterface.TagDatetime);
                        }
                        catch { }

                        switch (orientation)
                        {
                            case 6:
                                Helpers.Orientation = 90;
                                break;
                            case 3:
                                Helpers.Orientation = 180;
                                break;
                            case 8:
                                Helpers.Orientation = 270;
                                break;
                            default:
                                Helpers.Orientation = 0;
                                break;
                        }

                        ContentValues values = new ContentValues();
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.DisplayName, _file.Name);
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.DateTaken, date);
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.MimeType, "image/jpeg");
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Orientation, Helpers.Orientation);
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data, _file.Path);

                        var uri = ContentResolver.Insert(Android.Provider.MediaStore.Images.Media.ExternalContentUri, values);
                        //uri returned is the path to the image on the device
                    }
                    else
                    {
                        //uris is a list of uri's specifying the image taken and its thumbnail
                    }
                };
            }
        }
        else
    {
    //notify user the camera was cancelled if you want
    }
}

这篇关于MonoDroid的,要考虑与相机拍摄照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆