簡體   English   中英

如何在10秒后正確創建自己的功能以Xamarin形式拍攝照片?

[英]How can I correctly create my own function to take a photo in Xamarin forms after 10 seconds?

我正在關注這個項目https://github.com/ThatCSharpGuy/Forms-FullCameraPage以xamarin形式實現相機。 它可以正常工作,但是我想創建自己的空白來拍攝照片。 我在該項目中更改為當前項目的唯一一件事是,我已將CameraPage更改為Contentpage XAML,而不是一個類。

這個想法是為了使一個功能使照片在10秒鍾內拍完,如果可能的話,我想在我的共享代碼中對此進行控制。

如果我們看一下我添加的代碼,這就是我到目前為止所得到的。

public CameraPage()
{
        InitializeComponent();
        loadCameraFunction (); //so this is the function that i created myself
}

//i added this and put the same value into them as the ones in `SetPhotoResult`
byte[] image;
int width = -1;
int height = -1;

//the void
async void loadCameraFunction()
{
   await Task.Delay (10000); //wait 10 sec for the photo to be taken
   SetPhotoResult(image, width, height); //i add the byte[] and two ints i created above that has the same value as the ones in SetPhotoResult
}

    public delegate void PhotoResultEventHandler(PhotoResultEventArgs result);

    public event PhotoResultEventHandler OnPhotoResult;


    public void SetPhotoResult(byte[] image, int width = -1, int height = -1)
    {
        OnPhotoResult?.Invoke(new PhotoResultEventArgs(image, width, height));
    }

    public void Cancel()
    {
        OnPhotoResult?.Invoke(new PhotoResultEventArgs());
    }

    public class PhotoResultEventArgs : EventArgs
    {

        public PhotoResultEventArgs()
        {
            Success = false;
        }

        public PhotoResultEventArgs(byte[] image, int width, int height)
        {
            Success = true;
            Image = image;
            Width = width;
            Height = height;
        }

        public byte[] Image { get; private set; }
        public int Width { get; private set; }
        public int Height { get; private set; }
        public bool Success { get; private set; }
    }

}

}

我用這段代碼的問題是我的函數的結果似乎為空。 好像我沒有得到圖像。 所以我的問題是:為了拍照需要調用SetPhotoResult函數嗎? 如果是這樣,我在做什么錯?

拍照后應調用SetPhotoResult 如果您只想在擊打FullCameraqPagePage上的“拍攝照片”按鈕10秒后拍攝照片,則需要在自定義渲染器中修改代碼。 對於iOS,請在CameraPageRenderer類中將以下內容添加到ViewDidLoad方法的末尾:

await Task.Delay(10000);
var data = await CapturePhoto();
UIImage imageInfo = new UIImage(data);

(Element as FullCameraPage.CameraPage).SetPhotoResult(
        data.ToArray(),
        (int)imageInfo.Size.Width,
        (int)imageInfo.Size.Height);

這與您單擊CameraPage上的圓形按鈕時所運行的代碼相同,但添加了延遲。

刪除取消按鈕(左上角的x)和實際拍攝照片的圓形按鈕(第一頁上的“拍攝照片”按鈕僅加載相機頁面,而不拍攝照片)也是一個好主意。 )。 要刪除這些按鈕,以便用戶可以不打斷你的圖片(當然,他們仍然可以,如果他們關閉應用程序),然后從刪除的這兩行代碼SetupUserInterface的方法CameraPageRenderer

View.Add(takePhotoButton);
View.Add(cancelPhotoButton);

當然,您也可以刪除與這兩個按鈕相關的所有代碼。

對於Android,應采用相同的想法。 只需將以下內容添加到Android CameraPageRenderer類的OnElementChanged方法中:

await Task.Delay(5000);
        var bytes = await TakePhoto();
        (Element as CameraPage).SetPhotoResult(bytes, liveView.Bitmap.Width, liveView.Bitmap.Height);

並擺脫手動拍照按鈕,請從SetupUserInterface方法中刪除以下內容:

mainLayout.AddView(capturePhotoButton);

編輯:要在共享代碼中全部完成:

一類屬性添加到CameraPage類型的Action

public Action TakePicture { get; set; }

在CameraPageRenderer中設置Action的代碼:

(安卓):

(Element as CameraPage).TakePicture = async () =>
{
     var bytes = await TakePhoto();
    (Element as CameraPage).SetPhotoResult(bytes, liveView.Bitmap.Width, liveView.Bitmap.Height);
};

(IOS):

(Element as CameraPage).TakePicture = async () =>
{
    var data = await CapturePhoto();
    UIImage imageInfo = new UIImage(data);

    (Element as FullCameraPage.CameraPage).SetPhotoResult(data.ToArray(),
        (int)imageInfo.Size.Width,
        (int)imageInfo.Size.Height);
};

然后再次在CameraPage

protected override async void OnAppearing()
{
    base.OnAppearing();
    if (TakePicture != null)
    {
           await Task.Delay(5000);
           TakePicture();
    }
}

那應該做的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM