如何从Textureview获取Bitmap并将其传递给另一个活动

时间:2017-03-19 22:59:57

标签: c# android visual-studio camera textureview

我的纹理视图上有一个相机流,我还有一个拍照按钮,我试图投射纹理视图中的任何内容并将其发送到另一个活动。

这是我的代码:

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        _textureView = FindViewById<TextureView>(Resource.Id.textureView);
        _textureView.SurfaceTextureListener = this;
        TakePictureBtn = FindViewById<Button>(Resource.Id.takePhotoButton);
        TakePictureBtn.Click += TakePicture_Click;

    }

    private void TakePicture_Click(object sender, EventArgs e)
    {
        Bitmap Photo;
        Photo = _textureView.GetBitmap(640,480);
        Intent PictureActivity = new Intent(this, typeof(PictureActivity));
        PictureActivity.PutExtra("Photo", Photo);
        this.StartActivity(PictureActivity);
    } 

这是其他活动代码:

 public class PictureActivity : Activity
    {
        ImageView Photo;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.PictureActivity);
            Photo = FindViewById<ImageView>(Resource.Id.imageView1);
            Photo.SetImageBitmap((Bitmap)Intent.GetParcelableExtra("Photo"));
            // Create your application here
        }
    }

但是当我按下按钮时没有任何反应,这是正确的方法吗? 感谢。

1 个答案:

答案 0 :(得分:0)

点击你的点击活动

 private void TakePicture_Click(object sender, EventArgs e)
        {
            _camera.TakePicture(null, null, this);         
        }

然后添加:

public void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
        {
            _camera.StopPreview();
            Bitmap picture = BitmapFactory.DecodeByteArray(data, 0, data.Length);
            Intent PictureActivity = new Intent(this, typeof(PictureActivity));
            PictureActivity.PutExtra("Photo", picture);
            this.StartActivity(PictureActivity);
            _camera.StartPreview();
        }

不要忘记:

 public class Activity1 : Activity, TextureView.ISurfaceTextureListener, IPictureCallback