gpt4 book ai didi

Android setFocusArea 和自动对焦

转载 作者:IT王子 更新时间:2023-10-29 00:06:12 24 4
gpt4 key购买 nike

这几天我一直在和这个功能作斗争......

看来,相机忽略了(?)我定义的焦点区域。以下是代码 fragment :

聚焦:

protected void focusOnTouch(MotionEvent event) {
if (camera != null) {
Rect rect = calculateFocusArea(event.getX(), event.getY());

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

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

焦点区域计算:

private Rect calculateFocusArea(float x, float y) {
int left = clamp(Float.valueOf((x / getSurfaceView().getWidth()) * 2000 - 1000).intValue(), focusAreaSize);
int top = clamp(Float.valueOf((y / getSurfaceView().getHeight()) * 2000 - 1000).intValue(), focusAreaSize);

return new Rect(left, top, left + focusAreaSize, top + focusAreaSize);
}

来自 Camera.AutoFocusCallback#onAutoFocus

的几个日志事件

Log.d(TAG, String.format("自动对焦成功=%s。对焦模式:'%s'。对焦:%s",
专注,
camera.getParameters().getFocusMode(),
camera.getParameters().getFocusAreas().get(0).rect.toString()));

08-27 11:19:42.240: DEBUG/MyCameraActivity(26268): Auto focus success=true. Focus mode: 'auto'. Focused on: Rect(-109, 643 - -13, 739)
08-27 11:19:55.514: DEBUG/MyCameraActivity(26268): Auto focus success=true. Focus mode: 'auto'. Focused on: Rect(20, 457 - 116, 553)
08-27 11:19:58.037: DEBUG/MyCameraActivity(26268): Auto focus success=true. Focus mode: 'auto'. Focused on: Rect(-159, 536 - -63, 632)
08-27 11:20:00.129: DEBUG/MyCameraActivity(26268): Auto focus success=true. Focus mode: 'auto'. Focused on: Rect(-28, 577 - 68, 673)

在视觉上看起来焦点在记录区域上成功,但突然失去焦点并集中在中心 (0, 0) 或占 SurfaceView 更大部分的地方获得。

计算中使用的

focusAreaSize 约为 210px (96dp)。在 Camera.getParameters().getMaxNumFocusAreas()1 的 HTC One 上进行测试。

初始对焦模式(第一次点击之前)设置为 FOCUS_MODE_CONTINUOUS_PICTURE

我在这里做错了吗?修改 Camera.Area 矩形大小或重量不会显示任何明显效果。

最佳答案

我的问题要简单得多:)

我所要做的就是取消之前称为自动对焦的操作。基本上正确的操作顺序是这样的:

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 setFocusArea 和自动对焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18460647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com