Java tutorial
/* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package quickbeer.android.features.barcodescanner; import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.hardware.Camera; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewTreeObserver; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import com.google.android.gms.common.api.CommonStatusCodes; import com.google.android.gms.vision.MultiProcessor; import com.google.android.gms.vision.barcode.Barcode; import com.google.android.gms.vision.barcode.BarcodeDetector; import java.io.IOException; import quickbeer.android.R; import quickbeer.android.analytics.Analytics; import quickbeer.android.analytics.Events; import quickbeer.android.features.barcodescanner.ui.camera.CameraSource; import quickbeer.android.features.barcodescanner.ui.camera.CameraSourcePreview; import quickbeer.android.features.barcodescanner.ui.camera.GraphicOverlay; import timber.log.Timber; import static io.reark.reark.utils.Preconditions.get; /** * Activity for the multi-tracker app. This app detects barcodes and displays the value with the * rear facing camera. During detection overlay graphics are drawn to indicate the position, * size, and ID of each barcode. */ public final class BarcodeScanActivity extends AppCompatActivity implements BarcodeResultListener { public static final String BARCODE_RESULT = "BarcodeResult"; // intent request code to handle updating play services if needed. private static final int RC_HANDLE_GMS = 9001; // permission request codes need to be < 256 private static final int RC_HANDLE_CAMERA_PERM = 2; private static final int PREVIEW_HEIGHT = 1800; private static final float REQUESTED_FPS = 30.0f; private CameraSource mCameraSource; private CameraSourcePreview mPreview; private GraphicOverlay<BarcodeGraphic> mGraphicOverlay; private int viewWidth; private int viewHeight; @Nullable private Analytics analytics; /** * Initializes the UI and creates the detector pipeline. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); analytics = new Analytics(this); analytics.createEvent(Events.Screen.BARCODE_SCANNER); setContentView(R.layout.barcode_scan_activity); mPreview = (CameraSourcePreview) findViewById(R.id.preview); mGraphicOverlay = (GraphicOverlay<BarcodeGraphic>) findViewById(R.id.graphicOverlay); ViewTreeObserver viewTreeObserver = mPreview.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { mPreview.getViewTreeObserver().removeOnGlobalLayoutListener(this); viewWidth = mPreview.getWidth(); viewHeight = mPreview.getHeight(); // Check for the camera permission before accessing the camera. If the // permission is not granted yet, request permission. int rc = ContextCompat.checkSelfPermission(BarcodeScanActivity.this, Manifest.permission.CAMERA); if (rc == PackageManager.PERMISSION_GRANTED) { createCameraSource(); startCameraSource(); } else { requestCameraPermission(); } } }); } } @NonNull private Analytics analytics() { return get(analytics); } /** * Handles the requesting of the camera permission. This includes * showing a "Snackbar" message of why the permission is needed then * sending the request. */ private void requestCameraPermission() { Timber.e("Camera permission is not granted. Requesting permission"); final String[] permissions = { Manifest.permission.CAMERA }; if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM); return; } final Activity thisActivity = this; View.OnClickListener listener = view -> ActivityCompat.requestPermissions(thisActivity, permissions, RC_HANDLE_CAMERA_PERM); findViewById(R.id.topLayout).setOnClickListener(listener); Snackbar.make(mGraphicOverlay, R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE) .setAction(R.string.ok, listener).show(); } /** * 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. * * Suppressing InlinedApi since there is a check that the minimum version is met before using * the constant. */ @SuppressLint("InlinedApi") 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(this, 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. Timber.w("Detector dependencies are not yet available."); // Check for low storage. If there is low storage, the native library will not be // downloaded, so detection will not become operational. IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null; if (hasLowStorage) { Toast.makeText(this, R.string.low_storage_error, Toast.LENGTH_LONG).show(); Timber.w(getString(R.string.low_storage_error)); } } float ratio = viewHeight / (float) viewWidth; // 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(PREVIEW_HEIGHT, (int) (PREVIEW_HEIGHT / ratio)) .setRequestedFps(REQUESTED_FPS).setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) .build(); } /** * Restarts the camera. */ @Override protected void onResume() { super.onResume(); startCameraSource(); } /** * Stops the camera. */ @Override protected void onPause() { super.onPause(); if (mPreview != null) { mPreview.stop(); } } /** * Releases the resources associated with the camera source, the associated detectors, and the * rest of the processing pipeline. */ @Override protected void onDestroy() { super.onDestroy(); if (mPreview != null) { mPreview.release(); } } /** * Callback for the result from requesting permissions. This method * is invoked for every call on {@link #requestPermissions(String[], int)}. * <p> * <strong>Note:</strong> It is possible that the permissions request interaction * with the user is interrupted. In this case you will receive empty permissions * and results arrays which should be treated as a cancellation. * </p> * * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}. * @param permissions The requested permissions. Never null. * @param grantResults The grant results for the corresponding permissions * which is either {@link PackageManager#PERMISSION_GRANTED} * or {@link PackageManager#PERMISSION_DENIED}. Never null. * @see #requestPermissions(String[], int) */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode != RC_HANDLE_CAMERA_PERM) { Timber.d("Got unexpected permission result: " + requestCode); super.onRequestPermissionsResult(requestCode, permissions, grantResults); return; } if (grantResults.length != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Timber.d("Camera permission granted - initialize the camera source"); // we have permission, so create the camerasource createCameraSource(); return; } Timber.e("Permission not granted: results len = " + grantResults.length + " Result code = " + (grantResults.length > 0 ? grantResults[0] : "(empty)")); DialogInterface.OnClickListener listener = (dialog, id) -> finish(); new AlertDialog.Builder(this).setTitle(R.string.app_name).setMessage(R.string.no_camera_permission) .setPositiveButton(R.string.ok, listener).show(); } /** * 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() throws SecurityException { // 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) { Timber.e("Unable to start camera source.", e); mCameraSource.release(); mCameraSource = null; } } } @Override public void onBarcodeDetected(@NonNull Barcode barcode) { Intent data = new Intent(); data.putExtra(BARCODE_RESULT, barcode); setResult(CommonStatusCodes.SUCCESS, data); finish(); } }