gpt4 book ai didi

Android setFocusArea 和自动对焦

转载 作者:行者123 更新时间:2023-12-02 06:10:38 29 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",
专注,
相机.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)。在 HTC One 上进行测试,其中 Camera.getParameters().getMaxNumFocusAreas()1

初始对焦模式(首次点击之前)设置为 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/55909179/

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