2015-09-14 133 views
1

我正在用新的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等。

谢谢!

回答

0

你可能想使用这些cameraParameters之一:

参数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

注:并非所有手机/平板电脑有自动对焦的能力。

+2

但getParameters()不是CameraSource方法:/ –

+1

你能看这里吗? https://github.com/googlesamples/android-vision/issues/2。他们展示了如何将其作为解决方法。 –

+0

似乎不喜欢Camera类已被弃用。有一个android.hardware.camera2包来取代它。我会试着了解它现在如何工作,然后我再次回复。谢谢 ! “ –