【问题标题】:How to adjust camera (auto?) focus ?如何调整相机(自动?)焦距?
【发布时间】:2015-09-14 01:28:13
【问题描述】:

我正在使用新的 Google API (https://developers.google.com/android/reference/com/google/android/gms/vision/barcode/BarcodeDetector) 构建条形码扫描仪。我对相机一无所知,这是我第一次做这些东西。我已经可以打开应用程序并扫描大代码,但是较小的,我不能,因为相机没有调整它的焦点。永远保持不变。

TLDL:如何启用 CameraSource 的 (com.google.android.gms.vision) 实例自动对焦?

我的布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">

<com.example.victormilazzo.barcodedetector.camera.CameraSourcePreview
    android:id="@+id/preview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.victormilazzo.barcodedetector.camera.GraphicOverlay
        android:id="@+id/faceOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</com.example.victormilazzo.barcodedetector.camera.CameraSourcePreview>

</LinearLayout>

GraphicOverlay 和 CameraSourcePreview 是自定义类。

主要活动:

...
private CameraSource mCameraSource = null;
private CameraSourcePreview mPreview;
private GraphicOverlay mGraphicOverlay;

/**
 * Initializes the UI and creates the detector pipeline.
 */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_camera);

    mPreview = (CameraSourcePreview) findViewById(R.id.preview);
    mGraphicOverlay = (GraphicOverlay) findViewById(R.id.faceOverlay);

    // Check for the camera permission before accessing the camera.  If the
    // permission is not granted yet, request permission.
    int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
    if (rc == PackageManager.PERMISSION_GRANTED) {
        createCameraSource();
    } else {
        requestCameraPermission();
    }
}

...

/**
 * Creates and starts the camera.  Note that this uses a higher resolution in comparison
 * to other detection examples to enable the barcode detector to detect small barcodes
 * at long distances.
 */
private void createCameraSource() {


    Context context = getApplicationContext();

    // A barcode detector is created to track barcodes.  An associated multi-processor instance
    // is set to receive the barcode detection results, track the barcodes, and maintain
    // graphics for each barcode on screen.  The factory is used by the multi-processor to
    // create a separate tracker instance for each barcode.
    BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
    BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay);
    barcodeDetector.setProcessor(
            new MultiProcessor.Builder<>(barcodeFactory).build());

    if (!barcodeDetector.isOperational()) {
        // Note: The first time that an app using the barcode or face API is installed on a
        // device, GMS will download a native libraries to the device in order to do detection.
        // Usually this completes before the app is run for the first time.  But if that
        // download has not yet completed, then the above call will not detect any barcodes
        // and/or faces.
        //
        // isOperational() can be used to check if the required native libraries are currently
        // available.  The detectors will automatically become operational once the library
        // downloads complete on device.
        Log.w(TAG, "Detector dependencies are not yet available.");
    }

    // Creates and starts the camera.  Note that this uses a higher resolution in comparison
    // to other detection examples to enable the barcode detector to detect small barcodes
    // at long distances.
    mCameraSource = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
            .setFacing(CameraSource.CAMERA_FACING_BACK)
            .setRequestedPreviewSize(1600, 1024)
            .setRequestedFps(15.0f)
            .build();
}

...

/**
 * Starts or restarts the camera source, if it exists.  If the camera source doesn't exist yet
 * (e.g., because onResume was called before the camera source was created), this will be called
 * again when the camera source is created.
 */
private void startCameraSource() {

    // check that the device has play services available.
    int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
            getApplicationContext());
    if (code != ConnectionResult.SUCCESS) {
        Dialog dlg =
                GoogleApiAvailability.getInstance().getErrorDialog(this, code, RC_HANDLE_GMS);
        dlg.show();
    }

    if (mCameraSource != null) {
        try {
            mPreview.start(mCameraSource, mGraphicOverlay);
        } catch (IOException e) {
            Log.e(TAG, "Unable to start camera source.", e);
            mCameraSource.release();
            mCameraSource = null;
        }
    }
}

我只发布相关方法。还有其他的,比如 pauseCamera 等。

谢谢!

【问题讨论】:

    标签: android android-studio camera android-camera autofocus


    【解决方案1】:

    您可能想要使用以下相机参数之一:

    参数 params = camera.getParameters();

    params.setFocusMode(INSERT_MODE_HERE);

    camera.setParameters(params);

    您将INSERT_MODE_HERE 设置为:

    Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE Camera.Parameters.FOCUS_MODE_AUTO

    注意:并非所有手机/平板电脑都具备自动对焦功能。

    【讨论】:

    • 但 getParameters() 不是 CameraSource 方法:/
    • 你能看看这里吗? github.com/googlesamples/android-vision/issues/2。他们展示了如何将其作为一种解决方法。
    • 似乎相机类已被弃用。有一个 android.hardware.camera2 包可以替换它。我会尝试了解它现在是如何工作的,然后我再次回复。谢谢!
    • 另一方面:" Gericop 5 天前评论说:这是 Google 处理相机的官方方式(他们没有在代码中使用新的 Camera 2 API,所以我不能也不要这样做)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    相关资源
    最近更新 更多