如何使用c#从Android摄像头抓取图像或位图

时间:2012-03-21 20:13:36

标签: c# xamarin.android android-camera

我可以使用下面的代码获取图像。拍摄后,我想将图像转换为byte[]。我坚持的事情是我在拍照后如何抓住图像?

Intent是我用来打开相机的意图,但我不确定是否有我可以覆盖的内容,或者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);
        }
    }
}

1 个答案:

答案 0 :(得分:6)

以下是将图像转换为byte []

的方法

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