简体   繁体   English

我如何使用C#从Android相机中抓取图像或位图

[英]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[] . 拍摄快照后,我想将图像转换为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. 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);
        }
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在c#中从网络摄像头抓取恒定的位图图像流 - How to grab constant stream of bitmap images from webcam in c# 如何在C#中获取位图图像的背景色? - How do I get the background color of Bitmap Image in c#? 如何在Xamarin for Android中使用OpenTK将OpenGL渲染捕获为位图? - How do I grab an OpenGL render as a bitmap using OpenTK in Xamarin for Android? 将原始图像从JAI GigE相机转换为C#中的位图 - Convert raw image from JAI GigE camera to bitmap in C# 如何从现有图像(从相机)创建支持PropertyItems的C#位图 - How to create C# Bitmap supporting PropertyItems from existing Image (from Camera) 如何在C#中“从头开始”创建Windows位图? - How do I create a Windows bitmap “from scratch” in C#? 如何使用RegEx Asp.net C#抓取BODY html标签内的所有内容(来自字符串) - How do i grab everything inside the BODY html tag (From a string) using RegEx Asp.net C# 如何使用C#从笔记本电脑的摄像头捕获视频 - How do I capture video from my laptop's camera using C# 如何使用C#抓取PDF图像元素 - How to Grab PDF image elements using c# ASP.NET C#如何将画布图像作为Bitmap传递给控制器 - ASP.NET C# How do I pass canvas image to controller as Bitmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM