Xamarin android-从相机拍照,然后将其传递给其他活动 [英] Xamarin android - take picture from camera then pass it to other activity

查看:79
本文介绍了Xamarin android-从相机拍照,然后将其传递给其他活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是xamarin的新手,我想在相机上单击我的主活动上的按钮,然后在拍照后将其显示在其他活动中的imageView中.

I am new to xamarin and I want to take a picture from the camera when I click on a button on my mainactivity and then, once the picture taken, display it in an imageView in an other activity.

你能帮我吗?

这就是我现在拥有的:

MainActivity:

MainActivity :

costsButton.Click += delegate
{
    Intent intent = new Intent(MediaStore.ActionImageCapture);
    StartActivityForResult(intent, 0);
};

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    var extra = data.GetByteArrayExtra("data");
    Intent intent = new Intent(this, typeof(AddFrais));

    intent.PutExtra("picture", extra);
    StartActivity(intent);
}

AddFrais.cs:

AddFrais.cs :

namespace Projet_stage_2017
{
    [Activity(Label = "AddFrais")]
    public class AddFrais : Activity
    {
        ImageView picturefrais;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.AddFrais);
            picturefrais = FindViewById<ImageView>(Resource.Id.ImageFrais);
            var image = Intent.GetByteArrayExtra("picture") ?? null;
            Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length);

            picturefrais.SetImageBitmap(bitmap);
        }
    }
}

我不知道要在mainActivity中的"PutExtra"上放什么,以便能够在AddFrais.cs上创建位图.

I don't know what to put on the "PutExtra" in the mainActivity to be able to create a bitmap on AddFrais.cs...

感谢您的帮助!

推荐答案

尝试一下:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    // It's a good idea that you check this before accessing the data
    if (requestCode == 0 && resultCode == Result.Ok)
    { 
        //get the image bitmap from the intent extras
        var image = (Bitmap)data.Extras.Get("data");

        // you might also like to check whether image is null or not
        // if (image == null) do something

        //convert bitmap into byte array
        byte[] bitmapData;
        using (var stream = new MemoryStream())
        {
            image.Compress(Bitmap.CompressFormat.Png, 0, stream);
            bitmapData = stream.ToArray();
        }

        Intent intent = new Intent(this, typeof(AddFrais));

        intent.PutExtra("picture", bitmapData);

        StartActivity(intent);
    }
    // if you got here something bad happened...
}

然后在第二个活动中:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.AddFrais);

    picturefrais = FindViewById<ImageView>(Resource.Id.ImageFrais);

    //Get image from intent as ByteArray
    var image = Intent.GetByteArrayExtra("picture");

    if (image != null)
    { 
        //Convert byte array back into bitmap
        Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length);
        picturefrais.SetImageBitmap(bitmap);
    }
}     

如您所见,您的第二个活动代码很可能是相同的,我只是添加了一个验证,以防止从意图中很好地提取图像时防止NullReferenceException.

As you can see your second activity code is most likely the same, I just added a validation to prevent NullReferenceException if the image is not well extracted from the intent.

希望这会有所帮助!

这篇关于Xamarin android-从相机拍照,然后将其传递给其他活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆