简体   繁体   English

如何使用媒体插件在 Xamarin.Forms 中实现相机叠加的依赖服务?

[英]How to Implement Dependency Service for Camera Overlay in Xamarin.Forms Using the Media Plugin?

I am trying to pass a camera overlay function as a dependency service into my shared code using the Media Plugin for Xamarin https://github.com/jamesmontemagno/MediaPlugin .我正在尝试使用 Xamarin 的媒体插件https://github.com/jamesmontemagno/MediaPlugin将相机叠加功能作为依赖服务传递到我的共享代码中。

I can not figure out how to implement the dependency service correctly.我无法弄清楚如何正确实现依赖服务。 The app runs but when I open the camera, it doesn't display the overlay.该应用程序运行,但当我打开相机时,它不显示叠加层。 If someone could help me with my code, or direct me to an example of using the overlay option, I would really appreciate it.如果有人可以帮助我编写代码,或指导我使用覆盖选项的示例,我将不胜感激。

My interface code:我的界面代码:

public interface IPhotoOverlay 
{
   object GetImageOverlayAsync();
}

My iOS code:我的iOS代码:

public object GetImageOverlayAsync()
    {
        Func<object> func = CreateOverlay;

        return func;
    }

    public object CreateOverlay()
    {
        var imageView = new UIImageView(UIImage.FromBundle("face-template.png"));
        imageView.ContentMode = UIViewContentMode.ScaleAspectFit;

        var screen = UIScreen.MainScreen.Bounds;
        imageView.Frame = screen;

        return imageView;
    }

My shared code:我的共享代码:

var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() {
            OverlayViewProvider = DependencyService.Get<IPhotoOverlay>().GetImageOverlayAsync,
            DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front});

In your Xamarin.iOS Service, you need to register the Dependency, here is an example.在您的 Xamarin.iOS 服务中,您需要注册依赖项,这是一个示例。

[assembly: Dependency (typeof (PhotoOverlayiOS))]
namespace UsingDependencyService.iOS
{
    public class PhotoOverlayiOS : IPhotoOverlay
    {

        public object GetImageOverlayAsync()
        {
            Func<object> func = CreateOverlay;

            return func;
        }

        public object CreateOverlay()
        {
            var imageView = new UIImageView(UIImage.FromBundle("face-template.png"));
            imageView.ContentMode = UIViewContentMode.ScaleAspectFit;

            var screen = UIScreen.MainScreen.Bounds;
            imageView.Frame = screen;

            return imageView;
        }  
    }
}

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

相关问题 如何使用Xamarin.Forms的媒体插件在共享代码中实现iOS覆盖? - How to Implement an iOS Overlay in Shared Code Using the Media Plugin for Xamarin.Forms? 如何使用相机“圆圈”覆盖捕获图像并将其保存在 Xamarin.forms (Android) 中的路径上 - How Capture image using camera 'Circle' overlay and Save it on path in Xamarin.forms (Android) 如果调用方未使用 Xamarin.Forms,Xamarin 依赖项服务是否可以工作? - Can the Xamarin Dependency service work if the caller is not using Xamarin.Forms? 如何实现Xamarin.Forms中的经典媒体选择器? - How to implement the classic media picker in Xamarin.Forms? 使用依赖关系服务从Xamarin.Forms应用发送电子邮件 - Send email from Xamarin.Forms app using Dependency Service Xamarin.Forms依赖服务UWP:methodNotFoundException - Xamarin.Forms Dependency Service UWP: methodNotFoundException Xamarin.Forms:Android<entry> 和依赖服务?</entry> - Xamarin.Forms: Android <Entry> and dependency service? 如何覆盖XAMARIN.FORMS的加载视图 - How to Overlay Loading View for XAMARIN.FORMS 如何使用Prism / DryIoC在Xamarin.Forms中解决IValueConverter中的依赖关系 - How to resolve a dependency in IValueConverter in Xamarin.Forms using Prism/DryIoC Xamarin.Forms Plugin.Media GetStream / Dispose不存在 - Xamarin.Forms Plugin.Media GetStream/Dispose does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM