簡體   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