Google has provided a supremely useful View for managing your CameraSource
for preview and detection. You don't have to worry about managing the texture itself or determining the layout of the texture/surface in your view.
It's called CameraSourcePreview
, and it's used to manage the operation of your CameraSource
along with a SurfaceView
.
You can use the CameraSourcePreview
like any other view in your layout, for example in this full screen LinearLayout
:
<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.google.android.gms.samples.vision.barcodereader.ui.camera.CameraSourcePreview
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.gms.samples.vision.barcodereader.ui.camera.CameraSourcePreview>
</LinearLayout>
And in your activity you set up your BarcodeDetector
and CameraSource
like normal (probably in your onCreate()
method) and then (likely onResume()
) start your CameraSourcePreview
. It would look something like this:
if (mCameraSource != null) {
try {
mPreview.start(mCameraSource, mGraphicOverlay);
} catch (IOException e) {
Log.e(TAG, "Unable to start camera source.", e);
mCameraSource.release();
mCameraSource = null;
}
}
So really, you'll just be managing your CameraSource
through your the CameraSourcePreview
, but it handles a lot of the heavy lifting of the layout/preview/etc. so you don't have to. Check out the BarcodeCaptureActivity
to see more about how the activity manages these components.
Check out the barcode-reader sample in the android vision sample projects for the complete project sample.
Hope that helps, cheers.