在几个教程和示例之后,我提出了下一个算法来将相机焦点设置在特定点上,问题是相机完全忽略了点并执行了正常的整体焦点而不是我指定的矩形区域 . 算法中还有什么我遗漏的东西吗?这已经在Android 4.0及更高版本的几部手机上进行了测试,因此这些设备支持焦点区域API . 请注意,我正在编写的应用程序仅在横向模式下工作 .
@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);
}
2 回答
在cameraParameters.setFocusAreas(focusAreas);之前,你应该添加:
这不是我的代码,但你可以尝试一下 . 所有的积分都去了那里的例子Touch to select focus and metering area
此外,我还为代码提供了一条小线,因为当我第二次点击Surface View时,应用程序正在破碎 . 所以请尽快使用下面的类(“AndroidCamera”):
我所做的只是在
drawingView.invalidate();
之后添加camera.startFaceDetection();
附: On that site is a link for downloading the entire project!