如何使用c#从android相机中获取图像或位图

本文关键字:获取 图像 位图 相机 何使用 android | 更新日期: 2023-09-27 18:28:39

我可以使用下面的代码获得图像。拍完照片后,我想把图像变成byte[]。我一直在思考的问题是,拍完照片后,我应该如何获取图像?

Intent是我用来打开相机的意图,但我不确定是否有什么可以覆盖的东西,或者意图是否仍然有我的image/bitmap,所以我可以将其分解。

[Activity(Label = "CameraPage")]
public class PhotoTaker : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
       SetContentView(Resource.Layout.CameraView);
        ImageButton button = FindViewById<ImageButton>(Resource.Id.imagebutton);
        button.Click += BtnCameraClick;
    }
    private string _imageUri;
    private Intent intent;

    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();
        intent = new Intent(MediaStore.ActionImageCapture);
        //bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, uri);
        intent.PutExtra(MediaStore.ExtraOutput, uri);
        StartActivityForResult(intent, 1001);
    }
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        if (resultCode == Result.Ok && requestCode == 1001)
        {
            Toast.MakeText(this, string.Format("Image URI is {0}", _imageUri), ToastLength.Short).Show();
            Android.Content.Intent temp = new Intent(this, typeof(PayeeInformationViewModel));
            StartActivity(temp);
            //this.byteArr = this.bitmap.GetNinePatchChunk();
          //  byteArr = intent.GetByteArrayExtra(_imageUri);
          //  GetFileStreamPath(this._imageUri);
            //var firstArr = string.Empty;
            //if (byteArr.Length > 5)
            //{
            //    foreach (byte b in byteArr)
            //    {
            //        firstArr += b.ToString();
            //        if (firstArr.Length > 5)
            //        {
            //            break;
            //        }
            //    }
            //}
            //else
            //{
            //    firstArr = "Small";
            //}
          //  Toast.MakeText(this, firstArr, ToastLength.Short).Show();
          //  MemoryStream stream = new MemoryStream(byteArr);
        }
    }
}

如何使用c#从android相机中获取图像或位图

以下是如何将图像转换为字节[]


protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    if (resultCode == Result.Ok && requestCode == 1001)
    {
        Android.Net.Uri _currentImageUri = Android.Net.Uri.Parse(_imageUri);
        Bitmap bitmap = BitmapFactory.DecodeStream(ContentResolver.OpenInputStream(_currentImageUri));
        byte[] bitmapData = null;
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
            bitmapData = stream.ToArray();
        }
        bitmap.Dispose();
    }
}