Xamarin Android显示来自相机的流 [英] Xamarin Android Display a stream from the camera

查看:207
本文介绍了Xamarin Android显示来自相机的流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Xamarin的新手,我尝试将我的相机流实现为xaml布局. Xamarin的这个示例将把整个textureview设置为布局,因此我无法添加一些额外的功能,例如按钮等.

I'm new in Xamarin and I try to implement my camera stream into a xaml layout. This example of Xamarin will set the complete textureview as layout so I'm not able to add some extra features like buttons etc.

https://developer.xamarin.com/recipes/android/other_ux/textureview/display_a_stream_from_the_camera/

using System;

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Hardware;
using static Android.App.ActionBar;

namespace TextureViewCameraStream
{
    [Activity (Label = "TextureViewCameraStream", MainLauncher = true)]
    public class Activity1 : Activity, TextureView.ISurfaceTextureListener
    {
        Camera _camera;
        TextureView _textureView;

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

            _textureView = new TextureView(this);
            _textureView.SurfaceTextureListener = this;            

            SetContentView(_textureView);
        }

        public void OnSurfaceTextureAvailable (Android.Graphics.SurfaceTexture surface, int w, int h)
        {
            _camera = Camera.Open ();

            _textureView.LayoutParameters = new FrameLayout.LayoutParams (w, h);

            try {
                _camera.SetPreviewTexture (surface);
                _camera.StartPreview ();

            } catch (Java.IO.IOException ex) {
                Console.WriteLine (ex.Message);
            }
        }

        public bool OnSurfaceTextureDestroyed (Android.Graphics.SurfaceTexture surface)
        {
            _camera.StopPreview ();
            _camera.Release ();

            return true;
        }

        public void OnSurfaceTextureSizeChanged (Android.Graphics.SurfaceTexture surface, int width, int height)
        {
            // camera takes care of this
        }

        public void OnSurfaceTextureUpdated (Android.Graphics.SurfaceTexture surface)
        {

        }

    }
}

例如,我的布局必须为:

For example my layout must be:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1" />
    <TextureView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textureView1"
        android:layout_marginTop="0.0dp" />
</LinearLayout>

有人可以帮我在xaml布局的"textureView1"中添加相机预览吗?

Can anyone help me to add the camera preview in the "textureView1" of the layout xaml?

提前谢谢!

推荐答案

您可以通过调用SetContentView来加载axml,即

You can load your axml via a call to SetContentView, i.e.

SetContentView(Resource.Layout.CameraLayout);

注意:这假设您在Layout文件夹中的axml名称为CameraLayout.xml(.axml).

Note: this assumes that your axml within the Layout folder is name CameraLayout.xml (.axml).

public class Activity1 : Activity 
{
    bool _previewing;
    Camera _camera;
    TextureView _textureView;
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView(Resource.Layout.CameraLayout);
        Button button = FindViewById<Button>(Resource.Id.button1);
        _textureView = FindViewById<TextureView>(Resource.Id.textureView1);
        button.Click += delegate {
            try
            {
                if (!_previewing)
                {
                    _camera = Camera.Open();
                    _camera.SetPreviewTexture(_textureView.SurfaceTexture);
                    _camera.StartPreview();
                }
                else
                {
                    _camera.StopPreview();
                    _camera.Release();
                }
            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                _previewing = !_previewing;
            }
        };
    }

输出:

这篇关于Xamarin Android显示来自相机的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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