Xamarin Android从相机显示流

Xamarin Android从相机显示流,第1张

概述我是Xamarin的新手,因此尝试将相机流实现为xaml布局.Xamarin的这个示例将把整个textureview设置为布局,因此我无法添加一些额外的功能,例如按钮等.https://developer.xamarin.comecipes/android/other_uxextureview/display_a_stream_from_the_camera/usingSystem;using

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

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

提前致谢!

解决方法:

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

SetContentVIEw(Resource.Layout.CameraLayout);

注意:这假定您在Layout文件夹中的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;            }        };    }

输出:

总结

以上是内存溢出为你收集整理的Xamarin Android从相机显示流全部内容,希望文章能够帮你解决Xamarin Android从相机显示流所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1093430.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存