【问题标题】:Xamarin Android Display a stream from the cameraXamarin Android 显示来自相机的流
【发布时间】:2016-05-08 08:25:51
【问题描述】:

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

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)
        {

        }

    }
}

例如我的布局必须是:

<?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”中添加相机预览?

提前致谢!

【问题讨论】:

    标签: android xaml xamarin android-camera textureview


    【解决方案1】:

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

    SetContentView(Resource.Layout.CameraLayout);
    

    注意:这假定布局文件夹中的 axml 名称为 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;
                }
            };
        }
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 2014-09-13
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多