点按即可对焦以实现相机 [英] Tap to focus for camera implementation

查看:117
本文介绍了点按即可对焦以实现相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为相机页面实现手动对焦功能,以便用户可以点击以对焦相机.

I'm trying to implement a manual focus feature for my camera page so that the user can tap to focus the camera.

我正在关注此StackOverflow问题目前是用Java 编写的,适用于本机Android.我已经将其Xamarin.Forms Android应用程序转换为C#.

I'm following this StackOverflow question that's currently written in Java for native Android. I've been converting it to C# for my Xamarin.Forms Android app.

这是我到目前为止的内容:

Here's what I have so far:

public class CameraPage : PageRenderer, TextureView.ISurfaceTextureListener, Android.Views.View.IOnTouchListener, IAutoFocusCallback
{
    global::Android.Hardware.Camera camera;
    TextureView textureView;

    public void OnAutoFocus(bool success, Android.Hardware.Camera camera)
    {
        var parameters = camera.GetParameters();
        if (parameters.FocusMode != Android.Hardware.Camera.Parameters.FocusModeContinuousPicture)
        {
            parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeContinuousPicture;

            if (parameters.MaxNumFocusAreas > 0)
            {
                parameters.FocusAreas = null;
            }
            camera.SetParameters(parameters);
            camera.StartPreview();
        }
    }

    public bool OnTouch(Android.Views.View v, MotionEvent e)
    {
        if (camera != null)
        {
            var parameters = camera.GetParameters();
            camera.CancelAutoFocus();
            Rect focusRect = CalculateTapArea(e.GetX(), e.GetY(), 1f);

            if (parameters.FocusMode != Android.Hardware.Camera.Parameters.FocusModeAuto)
            {
                parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeAuto;
            }
            if (parameters.MaxNumFocusAreas > 0)
            {
                List<Area> mylist = new List<Area>();
                mylist.Add(new Android.Hardware.Camera.Area(focusRect, 1000));
                parameters.FocusAreas = mylist;
            }

            try
            {
                camera.CancelAutoFocus();
                camera.SetParameters(parameters);
                camera.StartPreview();
                camera.AutoFocus(OnAutoFocus); //Here is the issue. How do I use the callback? 
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.Write(ex.StackTrace);
            }
            return true;
        }
        return false; 
    }

    private Rect CalculateTapArea(object x, object y, float coefficient)
    {
        var focusAreaSize = 500;
        int areaSize = Java.Lang.Float.ValueOf(focusAreaSize * coefficient).IntValue();

        int left = clamp((int)x - areaSize / 2, 0, textureView.Width - areaSize);
        int top = clamp((int)y - areaSize / 2, 0, textureView.Height - areaSize);

        RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);
        Matrix.MapRect(rectF);

        return new Rect((int)System.Math.Round(rectF.Left), (int)System.Math.Round(rectF.Top), (int)System.Math.Round(rectF.Right), (int)System.Math.Round(rectF.Bottom));
    }

    private int clamp(int x, int min, int max)
    {
        if (x > max)
        {
            return max;
        }
        if (x < min)
        {
            return min;
        }
        return x;
    }
}

我已经设法转换了大部分,但是我不确定如何在此处正确使用AutoFocusCallback.我应该如何从OnTouch事件中调用OnAutoFocus,就像上面链接的Java答案一样?

I've managed to convert most of it but I'm not sure how to properly use the AutoFocusCallback here. What should I do to call OnAutoFocus from my OnTouch event like in the java answer I linked above?

在附加了回调之后,我要做的就是将OnTouch事件正确地订阅到我的页面上,或者...?

After I attached the callback, then all I need to do is subscribe the OnTouch event to my page correct or...?

例如,我尝试过:

textureView.Click += OnTouch;,但'OnTouch'的无重载匹配委托'EventHandler'.我需要使用特定的事件处理程序吗?

textureView.Click += OnTouch; but 'no overload for 'OnTouch' matches delegate 'EventHandler'. Is there a specific event handler I need to use?

推荐答案

您可以尝试更改

camera.AutoFocus(OnAutoFocus);

camera.AutoFocus(this);

,它将使用OnAutoFocus,因为它是从IAutoFocusCallback实现的.

and it will be using OnAutoFocus because it implementation from IAutoFocusCallback.

对于有关订阅事件的问题,您可以尝试像这样在OnElementChanged中订阅事件

And for your question about subscribe event you can try to subscribe event in OnElementChanged like this

protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Page> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement != null || Element == null)
            {
                return;
            }
            try
            {
                this.SetOnTouchListener(this);
            }
            catch (Exception e)
            {

            }

        }

顺便说一句,我看不到在此代码中使用TextureView.ISurfaceTextureListener.

And btw I don't see to use TextureView.ISurfaceTextureListener in this code.

这篇关于点按即可对焦以实现相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆