简体   繁体   English

如何在10秒后正确创建自己的功能以Xamarin形式拍摄照片?

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

i am following this project https://github.com/ThatCSharpGuy/Forms-FullCameraPage to implement a camera in xamarin forms. 我正在关注这个项目https://github.com/ThatCSharpGuy/Forms-FullCameraPage以xamarin形式实现相机。 it works without any problem but i would like to create my own void to take the photo. 它可以正常工作,但是我想创建自己的空白来拍摄照片。 The only thing i have changed in that project to my current one is that I have made the CameraPage to a Contentpage XAML instead of a class. 我在该项目中更改为当前项目的唯一一件事是,我已将CameraPage更改为Contentpage XAML,而不是一个类。

The idea is to make a function so the photo gets taken in 10 seconds and i want to control this in my shared code if it is possible. 这个想法是为了使一个功能使照片在10秒钟内拍完,如果可能的话,我想在我的共享代码中对此进行控制。

if we look at the code that i have added this is what i got so far. 如果我们看一下我添加的代码,这就是我到目前为止所得到的。

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; }
    }

}

} }

Problem i have with this code is that the result from my function seems to be null. 我用这段代码的问题是我的函数的结果似乎为空。 It seems like i dont get the image. 好像我没有得到图像。 So my question is: Is the SetPhotoResult function what i need to call in order to take the photo? 所以我的问题是:为了拍照需要调用SetPhotoResult函数吗? And if so, what am I doing wrong? 如果是这样,我在做什么错?

SetPhotoResult should be called once the photo is taken. 拍照后应调用SetPhotoResult If all you want to happen is that a photo is taken 10 seconds after you hit the "Take Photo" button on the FullCameraqPagePage then you will need to modify the code in the custom renderer. 如果您只想在击打FullCameraqPagePage上的“拍摄照片”按钮10秒后拍摄照片,则需要在自定义渲染器中修改代码。 For iOS in the CameraPageRenderer class add the following to the end of the ViewDidLoad method: 对于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);

This is the same code that is run when you click the circular button on the CameraPage but with the delay added. 这与您单击CameraPage上的圆形按钮时所运行的代码相同,但添加了延迟。

It may also be a good idea to remove the cancel (x in the upper left) and the circular button that actually takes the photo (the "Take Photo" button on the first page only loads the camera page, it does not take the photo). 删除取消按钮(左上角的x)和实际拍摄照片的圆形按钮(第一页上的“拍摄照片”按钮仅加载相机页面,而不拍摄照片)也是一个好主意。 )。 To remove those buttons so the user can't interrupt your picture (well, they still can if they close the app) then remove these two lines of code from the SetupUserInterface method of CameraPageRenderer : 要删除这些按钮,以便用户可以不打断你的图片(当然,他们仍然可以,如果他们关闭应用程序),然后从删除的这两行代码SetupUserInterface的方法CameraPageRenderer

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

Of course you can remove all code related to those two buttons as well. 当然,您也可以删除与这两个按钮相关的所有代码。

For Android the same idea should apply. 对于Android,应采用相同的想法。 Just add the following to the OnElementChanged method in the Android CameraPageRenderer class: 只需将以下内容添加到Android CameraPageRenderer类的OnElementChanged方法中:

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

and to get rid of the the manual take photo button, remove the following from the SetupUserInterface method: 并摆脱手动拍照按钮,请从SetupUserInterface方法中删除以下内容:

mainLayout.AddView(capturePhotoButton);

EDIT: To do it all in the shared code: 编辑:要在共享代码中全部完成:

Add a class property to CameraPage of type Action : 一类属性添加到CameraPage类型的Action

public Action TakePicture { get; set; }

Set the code for the Action in CameraPageRenderer: 在CameraPageRenderer中设置Action的代码:

(Android): (安卓):

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

(iOS): (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);
};

Then in CameraPage again: 然后再次在CameraPage

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

That oughta do it. 那应该做的。

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

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