gpt4 book ai didi

android - 谷歌视觉 API 示例 : Get the CameraSource to Focus

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:41 25 4
gpt4 key购买 nike

我已从此处查看最新的 Google Vision API:

https://github.com/googlesamples/android-vision

我在带有 KitKat 的 LG G2 设备上运行它。我所做的唯一更改是 Gradle 文件中的 minSdkVerion:

...
defaultConfig {
applicationId "com.google.android.gms.samples.vision.face.multitracker"
minSdkVersion 19
...

但是它没有聚焦。如何使其聚焦?

最佳答案

我修改了CameraSourcePreview(....)构造函数如下:

public CameraSourcePreview(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mStartRequested = false;
mSurfaceAvailable = false;

mSurfaceView = new SurfaceView(context);
mSurfaceView.getHolder().addCallback(new SurfaceCallback());
addView(mSurfaceView);
mSurfaceView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cameraFocus(mCameraSource, Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
});
}

private static boolean cameraFocus(@NonNull CameraSource cameraSource, @NonNull String focusMode) {
Field[] declaredFields = CameraSource.class.getDeclaredFields();

for (Field field : declaredFields) {
if (field.getType() == Camera.class) {
field.setAccessible(true);
try {
Camera camera = (Camera) field.get(cameraSource);
if (camera != null) {
Camera.Parameters params = camera.getParameters();
params.setFocusMode(focusMode);
camera.setParameters(params);
return true;
}

return false;
} catch (IllegalAccessException e) {
e.printStackTrace();
}

break;
}
}

return false;
}

这里给出了建议:https://github.com/googlesamples/android-vision/issues/2

代码引用在这里:https://gist.github.com/Gericop/7de0b9fdd7a444e53b5a

我还必须修改 FaceTrackerFactory draw(Canvas ...) 方法:

@Override
public void draw(Canvas canvas) {
Face face = mFace;
if (face == null) {
return;
}

// Draws a circle at the position of the detected face, with the face's track id below.
float cx = translateX(face.getPosition().x + face.getWidth() / 2);
float cy = translateY(face.getPosition().y + face.getHeight() / 2);
canvas.drawCircle(cx, cy, FACE_POSITION_RADIUS, mFacePositionPaint);
canvas.drawText("id: " + getId(), cx + ID_X_OFFSET, cy + ID_Y_OFFSET, mIdPaint);

// Draws an oval around the face.
float xOffset = scaleX(face.getWidth() / 2.0f);
float yOffset = scaleY(face.getHeight() / 2.0f);
float left = cx - xOffset;
float top = cy - yOffset;
float right = cx + xOffset;
float bottom = cy + yOffset;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawOval(left, top, right, bottom, mBoxPaint);
} else {
canvas.drawCircle(cx, cy, Math.max(xOffset, yOffset), mBoxPaint);
}
}

关于android - 谷歌视觉 API 示例 : Get the CameraSource to Focus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32051973/

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