gpt4 book ai didi

android - 如何在 camera api android 中调整触摸对焦区域

转载 作者:太空狗 更新时间:2023-10-29 14:54:56 24 4
gpt4 key购买 nike

我有一个代码可以将相机聚焦到屏幕上的触摸位置。在那个左右触摸位置正确地关注触摸。但上下触摸位置无法正常工作。谁能帮助解决这个问题

 // Surface view on touch
@Override
public boolean onTouchEvent(MotionEvent event) {
((CameraActivity) getContext()).touchFocus(event);
return true;
}

public void touchFocus(MotionEvent event) {

if (event.getAction() != MotionEvent.ACTION_DOWN) {
return;
}

float x = event.getX();
float y = event.getY();


Rect touchRect = new Rect(
(int) (x - 100),
(int) (y - 100),
(int) (x + 100),
(int) (y + 100));

final Rect targetFocusRect = new Rect(
touchRect.left * 2000 /mPreview.getWidth() - 1000,
((touchRect.top * 2000) /mPreview.getHeight()) - 1000,
touchRect.right * 2000 /mPreview.getWidth() - 1000,
((touchRect.bottom * 2000) /mPreview.getHeight()) - 1000);

final List<Camera.Area> focusList = new ArrayList<Camera.Area>();
Camera.Area focusArea = new Camera.Area(targetFocusRect, 1000);
focusList.add(focusArea);

Camera.Parameters para = mCamera.getParameters();
para.setFocusAreas(focusList);
para.setMeteringAreas(focusList);
try {
mCamera.setParameters(para);
mCamera.autoFocus(mAutoFocusTakePictureCallback);
} catch (Exception e) {
AppController.log(TAG, "focusOnTouch : " + e.getLocalizedMessage());
}
}

private Camera.AutoFocusCallback mAutoFocusTakePictureCallback = new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if (success) {
AppController.log(TAG, "Success");
} else {
AppController.log(TAG, "Failed");
}
}
};

最佳答案

我找到了触摸对焦的解决方案,我们也需要采取相机方向。

 private void setFocusing(int focusWidth, int focusHeight, int x, int y, int width, int height) {       
if (mMatrix == null) {
mMatrix = new Matrix();
mPreview.setMatrix(width, height, cameraOrientation);
}

Rect targetFocusRect = new Rect();
calculateTapArea(focusWidth, focusHeight, 1f, x, y, width, height, targetFocusRect);

final List<Camera.Area> focusList = new ArrayList<>();
Camera.Area focusArea = new Camera.Area(targetFocusRect, 1000);
focusList.add(focusArea);

Camera.Parameters parameters = mCamera.getParameters();
if (parameters.getMaxNumFocusAreas() > 0) {
parameters.setFocusAreas(focusList);
}

if (parameters.getMaxNumMeteringAreas() > 0) {
parameters.setMeteringAreas(focusList);
}

try {
mCamera.setParameters(parameters);
if (parameters.getMaxNumFocusAreas() > 0) {
mCamera.autoFocus(autoFocus);
}
} catch (Exception e) {
Log.d(TAG, "focusOnTouch : " + e.getLocalizedMessage());
}
}

Camera.AutoFocusCallback autoFocus = new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
Log.d(TAG, "Focus Status : " + success);
}
};

private void calculateTapArea(int focusWidth, int focusHeight, float areaMultiple, int x, int y, int previewWidth, int previewHeight, Rect rect) {
int areaWidth = (int) (focusWidth * areaMultiple);
int areaHeight = (int) (focusHeight * areaMultiple);
int left = clamp(x - areaWidth / 2, 0, previewWidth - areaWidth);
int top = clamp(y - areaHeight / 2, 0, previewHeight - areaHeight);

RectF rectF = new RectF(left, top, left + areaWidth, top + areaHeight);
mMatrix.mapRect(rectF);
rectFToRect(rectF, rect);
}

public static int clamp(int x, int min, int max) {
if (x > max) return max;
if (x < min) return min;
return x;
}

public static void rectFToRect(RectF rectF, Rect rect) {
rect.left = Math.round(rectF.left);
rect.top = Math.round(rectF.top);
rect.right = Math.round(rectF.right);
rect.bottom = Math.round(rectF.bottom);
}

关于android - 如何在 camera api android 中调整触摸对焦区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32451307/

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