Google Vision API 示例:让 CameraSource 获得焦点 [英] Google Vision API Samples: Get the CameraSource to Focus

查看:21
本文介绍了Google Vision API 示例:让 CameraSource 获得焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里查看了最新的 Google Vision API:

I have checkout out the latest Google Vision APIs from here:

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

我正在使用 KitKat 的 LG G2 设备上运行它.我所做的唯一更改是对 Gradle 文件中的 minSdkVerion:

And I am running it on a LG G2 device with KitKat. The only change I have made is to the minSdkVerion in the Gradle file:

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

但是它不聚焦.如何让它集中注意力?

However it does not focus. How do I make it focus?

推荐答案

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

I modified the CameraSourcePreview (....) constructor to be as follows:

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-愿景/问题/2

代码参考在这里:https://gist.github.com/Gericop/7de0b9fdd7a444e53b5a

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

I also had to modify the FaceTrackerFactory draw(Canvas ...) method:

@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);
    }
}

这篇关于Google Vision API 示例:让 CameraSource 获得焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆