获得全分辨率的照片从相机与MonoDroid

本文关键字:相机 MonoDroid 照片 分辨率 | 更新日期: 2023-09-27 18:14:11

我试图找到一种方法从MonoDroid访问全分辨率图片,经过很长一段时间试图将Java示例移植到MonoDroid,看看其他人似乎已经得到了,我目前有以下(不工作)

    private const int TAKE_PICTURE = 1;
    protected Uri fileUri;
    private void takePhoto(object sender, EventArgs e)
    {
        Intent intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
        string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                                             "hqxmmc/pictures/ikbeneenplaatje.jpg");
        Java.IO.File xfile = new Java.IO.File(path);
        if (xfile.Exists())
        {
            Android.Util.Log.Warn("FILE", "file exists {0} 'n overwriting!", xfile.Name);
            xfile.Delete();
            xfile.CreateNewFile();
        }
        else
        {
            Android.Util.Log.Warn("FILE", "file does not exist {0}, creating", xfile.Name);
            xfile.Mkdirs();
            xfile.CreateNewFile();
        }

        fileUri = Android.Net.Uri.FromFile(xfile);
        Android.Util.Log.WriteLine(LogPriority.Warn, "FILE", fileUri.ToString());
        intent.PutExtra(Android.Provider.MediaStore.ExtraOutput, fileUri);
        StartActivityForResult(intent, TAKE_PICTURE);

    }
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (requestCode == TAKE_PICTURE)
        {
            Uri imageUri = null;
            // Check if the result includes a thumbnail Bitmap
            if (data != null)
            {
                if (data.HasExtra("data"))
                {
                    var thumbnail = data.GetParcelableArrayExtra("data");
                    Android.Util.Log.WriteLine(LogPriority.Info, "DATA", thumbnail.ToString());
                    // TODO Do something with the thumbnail
                    var outputFileUri = data.GetParcelableArrayExtra("outputFileuri");
                    Android.Util.Log.WriteLine(LogPriority.Info, "DATA", outputFileUri.ToString());
                    // TODO Do something with the full image stored
                    // in outputFileUri
                }
            }
            else
            {
                //todo : reload full image form fileuri
                Android.Util.Log.WriteLine(LogPriority.Info, "PATH", fileUri.ToString());
            }
        }
    }

显示"拍照"屏幕,我可以拍照,但该照片随后被保存到设备上的默认DCIM文件夹,忽略我为其指定的文件名和路径。

当我保存图片时,OnActivityResult被调用,但是它的Intent data参数包含一个空意图。

我如何访问全分辨率图片和/或我刚刚拍摄的图片的缩略图?

获得全分辨率的照片从相机与MonoDroid

我将简单地张贴我在项目中使用的代码。希望对你有所帮助。我也试图通过ContentValues类设置文件名和路径,就像它在Android wiki中描述的那样,但都是无效的,我认为这是一些MonoDroid的错误。

private string _imageUri;
private Boolean isMounted
{
    get
    {
        return Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted);
    }
}
public void BtnCameraClick(object sender, EventArgs eventArgs)
{
      var uri = ContentResolver.Insert(isMounted ? MediaStore.Images.Media.ExternalContentUri
                              : MediaStore.Images.Media.InternalContentUri, new ContentValues());
     _imageUri = uri.ToString();
      var i = new Intent(MediaStore.ActionImageCapture);
      i.PutExtra(MediaStore.ExtraOutput, uri);
      StartActivityForResult(i, CAPTURE_PHOTO);
}
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        if (resultCode == Result.Ok && requestCode == CAPTURE_PHOTO)
        {
          Toast.MakeText(this, string.Format("Image URI is {0}",_imageUri), ToastLength.Short).Show();    
        }
    }