如何在Android中手动设置相机对焦值 [英] How to set camera focus value manually in Android

查看:389
本文介绍了如何在Android中手动设置相机对焦值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算创建一个可以通过以下方式拍照的应用程序:

I intend to create an application that can take photos in the following way:

  • 当用户触摸屏幕时,它开始拍摄照片
  • 它在几微秒内拍了几张照片,每张照片都具有不同的焦点
  • When the user touches the screen, it starts to take photos
  • It takes several photos within a few microseconds, each with different focus

使用伪代码:

Camera camera=getAndroidCamera();
for(i<10)
{
  camera.setFocus(i*0.1);
  camera.takePhoto(path,pictureName+i);
}

所以基本上我打算用不同的焦点值拍摄同一物体的照片.

So basically I intend to take photos of the same object with different values of focus.

根据 ,这是不可能的,只有辅助自动对焦可行.

According to this, it is not possible, only assisted autofocus is viable.

您可以确认吗?

如果可能的话,我该怎么办?我是否应该将自动对焦设置为不同区域?

If possible, how should I do it? Should I set autofocus to different areas?

推荐答案

答案- Android setFocusArea和自动对焦

我要做的就是取消以前称为自动对焦的功能.基本上,正确的操作顺序是这样的:

All I had to do is cancel previously called autofocus. Basically the correct order of actions is this:

protected void focusOnTouch(MotionEvent event) {
    if (camera != null) {

        camera.cancelAutoFocus();
        Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f);
        Rect meteringRect = calculateTapArea(event.getX(), event.getY(), 1.5f);

        Parameters parameters = camera.getParameters();
        parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
        parameters.setFocusAreas(Lists.newArrayList(new Camera.Area(focusRect, 1000)));

        if (meteringAreaSupported) {
            parameters.setMeteringAreas(Lists.newArrayList(new Camera.Area(meteringRect, 1000)));
        }

        camera.setParameters(parameters);
        camera.autoFocus(this);
    }}

...更新

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    ...
    Parameters p = camera.getParameters();
    if (p.getMaxNumMeteringAreas() > 0) {
       this.meteringAreaSupported = true;
    }
    ...
}

/**
 * Convert touch position x:y to {@link Camera.Area} position -1000:-1000 to 1000:1000.
 */
private Rect calculateTapArea(float x, float y, float coefficient) {
    int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue();

    int left = clamp((int) x - areaSize / 2, 0, getSurfaceView().getWidth() - areaSize);
    int top = clamp((int) y - areaSize / 2, 0, getSurfaceView().getHeight() - areaSize);

    RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);
    matrix.mapRect(rectF);

    return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), 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;
}

这篇关于如何在Android中手动设置相机对焦值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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