Google Vision API示例:让CameraSource成为焦点

时间:2015-08-17 13:41:50

标签: android camera google-play-services google-vision android-vision

我从这里结帐了最新的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
...

然而它没有集中注意力。如何让它集中注意力?

3 个答案:

答案 0 :(得分: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绘图(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);
    }
}

答案 1 :(得分:12)

官方API现在提供自动对焦选项。请在此处查看setAutoFocusEnabled方法:

https://developers.google.com/android/reference/com/google/android/gms/vision/CameraSource.Builder.html#setAutoFocusEnabled(boolean)

另外,我们开源了CameraSource类,它也有自动聚焦方法。这个允许您设置特定的焦点模式,而不是官方API默认的“连续视频”模式:

https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/ui/camera/CameraSource.java

答案 2 :(得分:7)

这适用于我使用Google Play服务8.4: 'com.google.android.gms:播放服务:8.4.0'

cameraSource = new CameraSource.Builder(this, detector).setRequestedPreviewSize(640, 480).setAutoFocusEnabled(true).build();