繁体   English   中英

Xamarin Android相机流

[英]Xamarin Android Camera Stream

我正在做一个项目,我需要创建一个类似于SnapChat的应用程序,我正在Visual Studio上使用Xamarin,我想在屏幕底部创建一个带有按钮的Camera Stream。 用户触摸到该圆圈后,应用程序将拍照!

Snapchat的例子

我是xamarin的新手,所以我有很多问题。

1.)我在xamarin页面上关注以下示例: https : //developer.xamarin.com/recipes/android/other_ux/textureview/display_a_stream_from_the_camera/

但是我不明白为什么摄像头流看起来很奇怪,如果手机处于垂直位置,则显示的图像是水平的,反之亦然,同样,当我移动手机时,显示的图像也会移动得非常慢,就像它将进行任何类型的操作一样调整大小或其他内容。

我怎样才能解决这个问题 ?

2.)我需要向应用程序添加按钮,但是代码中包含以下行:

SetContentView (_textureView);  

所以我找到了这段代码:

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显示来自相机的流

但是在该代码中,他们使用按钮来启用和禁用流,如何使用按钮将当前图像另存为位图? 如何保持流始终运行? (用户拍照时的示例)

3.)最后,如何将按钮放在textureView上? 如您在我的即时聊天图像中所见。 有可能实现吗? 有可能在xamarin做吗?

谢谢你的时间。

但是我不明白为什么摄像头流看起来如此奇怪,如果手机处于垂直位置,则显示的图像是水平的,反之亦然。

要解决此问题,可以设置摄像机的显示方向,例如:

_camera = Android.Hardware.Camera.Open();

try
{
    _camera.SetPreviewTexture(surface);
    _camera.SetDisplayOrientation(90);
    _camera.StartPreview();
}
catch (Java.IO.IOException ex)
{
    Console.WriteLine(ex.Message);
}

但是在该代码中,他们使用按钮来启用和禁用流,如何使用按钮将当前图像另存为位图?

您可以为相机设置一个PreviewCallback ,然后在OnPreviewFrame中获取当前预览的YuvImage 通常,此处不需要Bitmap ,但是如果要将其转换为Bitmap ,则可以参考这种情况下的答案: 将预览帧转换为位图

例如:

public class mPreviewCallback : Java.Lang.Object, IPreviewCallback
{
    public void OnPreviewFrame(byte[] data, Camera camera)
    {
        var paras = camera.GetParameters();
        var imageformat = paras.PreviewFormat;
        if (imageformat == Android.Graphics.ImageFormatType.Nv21)
        {
            Android.Graphics.YuvImage img = new Android.Graphics.YuvImage(data,
                imageformat, paras.PreviewSize.Width, paras.PreviewSize.Height, null);
        }
    }
}

并将此回调设置为您的相机,如下所示:

_camera.SetPreviewCallback(new mPreviewCallback());

最后,如何将按钮放在textureView上?

您可以像下面这样简单地创建视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextureView
        android:id="@+id/textureView"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

    <Button
        android:id="@+id/saveimg"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="img" />
</RelativeLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM