本文介绍了坐落在安卓相机对焦区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下几个教程和例子,我想出了下一个算法来设置相机专注于一个特定的点,问题是,相机完全忽略当场进行正常的整体重心,而不是我所指定的矩形区域。还有什么,我的算法很想念?这已经过测试的几款手机都采用Android 4.0及以上,所以对焦区域的API都支持这些设备。请注意,该应用程序我写这封信只有横向模式的作品。

  @覆盖
公共布尔的onTouchEvent(最终MotionEvent事件)
{
    如果(event.getAction()== MotionEvent.ACTION_UP)
    {
        浮X = event.getX();
        浮动Y = event.getY();
        浮touchMajor = event.getTouchMajor();
        浮touchMinor = event.getTouchMinor();

        矩形touchRect =新的Rect((int)的(X  -  touchMajor / 2),(INT)(Y  -  touchMinor / 2),(int)的(X + touchMajor / 2),(int)的(Y + touchMinor / 2)) ;

        this.submitFocusAreaRect(touchRect);
    }
}

私人无效submitFocusAreaRect(最终矩形touchRect)
{
    Camera.Parameters cameraParameters = camera.getParameters();

    如果(cameraParameters.getMaxNumFocusAreas()== 0)
    {
        返回;
    }

    //从视图的宽度和高度转换为+/- 1000

    矩形focusArea =新的矩形();

    focusArea.set(touchRect.left * 2000 / cameraSurfaceView.getWidth() -  1000,
                      touchRect.top * 2000 / cameraSurfaceView.getHeight() -  1000,
                      touchRect.right * 2000 / cameraSurfaceView.getWidth() -  1000,
                      touchRect.bottom * 2000 / cameraSurfaceView.getHeight() -  1000);

    //提交对焦区域相机

    ArrayList的< Camera.Area> focusAreas =新的ArrayList< Camera.Area>();
    focusAreas.add(新Camera.Area(focusArea,1000));

    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    cameraParameters.setFocusAreas(focusAreas);
    camera.setParameters(cameraParameters);

    //开始的自动对焦操作

    camera.autoFocus(本);
}
 

解决方案

我已经找到了答案在这里。 https://gist.github.com/mjurkus/21f7b9aa9b27a7661184 你可以试试。

following several tutorials and examples I came up with the next algorithm to set the camera focus on a specific spot, the problem is that the camera completely ignores the spot and performs a normal overall focus instead of the rect area which I have specified. Is there anything else that I am missing in the algorithm? This has been tested on several phones all with Android 4.0 and above, so the focus area API is supported on these devices. Note, the app I am writing works in landscape mode only.

@Override
public boolean onTouchEvent(final MotionEvent event)
{
    if (event.getAction() == MotionEvent.ACTION_UP)
    {
        float x = event.getX();
        float y = event.getY();
        float touchMajor = event.getTouchMajor();
        float touchMinor = event.getTouchMinor();

        Rect touchRect = new Rect((int)(x - touchMajor / 2), (int)(y - touchMinor / 2), (int)(x + touchMajor / 2), (int)(y + touchMinor / 2));

        this.submitFocusAreaRect(touchRect);
    }
}

private void submitFocusAreaRect(final Rect touchRect)
{
    Camera.Parameters cameraParameters = camera.getParameters();

    if (cameraParameters.getMaxNumFocusAreas() == 0)
    {
        return;
    }

    // Convert from View's width and height to +/- 1000

    Rect focusArea = new Rect();

    focusArea.set(touchRect.left * 2000 / cameraSurfaceView.getWidth() - 1000,
                      touchRect.top * 2000 / cameraSurfaceView.getHeight() - 1000,
                      touchRect.right * 2000 / cameraSurfaceView.getWidth() - 1000,
                      touchRect.bottom * 2000 / cameraSurfaceView.getHeight() - 1000);

    // Submit focus area to camera

    ArrayList<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
    focusAreas.add(new Camera.Area(focusArea, 1000));

    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    cameraParameters.setFocusAreas(focusAreas);
    camera.setParameters(cameraParameters);

    // Start the autofocus operation

    camera.autoFocus(this);
}
解决方案

I've found the answer here. https://gist.github.com/mjurkus/21f7b9aa9b27a7661184 You could try.

这篇关于坐落在安卓相机对焦区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:38