简体   繁体   中英

How do I grab an Image or bitmap from the android camera using c#

I am able to get the image using the code below. After snapping the shot I want to turn the image into a byte[] . The thing I am stuck on is how am I supposed to grab the image after taking the picture?

Intent is the intent I use to open the camera but I am not sure if there is something I can override or if the Intent still has my image/bitmap so I can break it down.

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

Here is how to convert the image to a 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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM