|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.glass.qrcodescannersample; |
| 18 | + |
| 19 | +import android.Manifest.permission; |
| 20 | +import android.app.Activity; |
| 21 | +import android.content.Intent; |
| 22 | +import android.content.pm.PackageManager; |
| 23 | +import android.os.Bundle; |
| 24 | +import android.util.DisplayMetrics; |
| 25 | +import android.util.Size; |
| 26 | +import android.view.TextureView; |
| 27 | +import androidx.annotation.NonNull; |
| 28 | +import androidx.camera.core.CameraX; |
| 29 | +import androidx.core.content.ContextCompat; |
| 30 | +import com.example.glass.qrcodescannersample.GlassGestureDetector.Gesture; |
| 31 | +import com.example.glass.qrcodescannersample.QRCodeImageAnalysis.QrCodeAnalysisCallback; |
| 32 | +import java.util.concurrent.ExecutorService; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | + |
| 35 | +/** |
| 36 | + * Activity responsible for requesting camera permission, starting camera and returning scanned QR |
| 37 | + * code as a result. |
| 38 | + */ |
| 39 | +public class CameraActivity extends BaseActivity implements QrCodeAnalysisCallback { |
| 40 | + |
| 41 | + /** |
| 42 | + * Key for the scan result in an {@link Intent}. |
| 43 | + */ |
| 44 | + public static final String QR_SCAN_RESULT = "SCAN_RESULT"; |
| 45 | + |
| 46 | + /** |
| 47 | + * Request code for the camera permission. This value doesn't have any special meaning. |
| 48 | + */ |
| 49 | + private static final int CAMERA_PERMISSIONS_REQUEST_CODE = 105; |
| 50 | + |
| 51 | + /** |
| 52 | + * Single thread executor service for the image analysis purposes. |
| 53 | + */ |
| 54 | + private ExecutorService executorService; |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void onCreate(Bundle savedInstanceState) { |
| 58 | + super.onCreate(savedInstanceState); |
| 59 | + setContentView(R.layout.activity_camera); |
| 60 | + executorService = Executors.newSingleThreadExecutor(); |
| 61 | + |
| 62 | + if (ContextCompat.checkSelfPermission(this, |
| 63 | + permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { |
| 64 | + startCamera(); |
| 65 | + } else { |
| 66 | + requestPermissions(new String[]{permission.CAMERA}, CAMERA_PERMISSIONS_REQUEST_CODE); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void onDestroy() { |
| 72 | + super.onDestroy(); |
| 73 | + executorService.shutdown(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean onGesture(Gesture gesture) { |
| 78 | + switch (gesture) { |
| 79 | + case SWIPE_DOWN: |
| 80 | + finishNoQR(); |
| 81 | + return true; |
| 82 | + default: |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| 89 | + @NonNull int[] grantResults) { |
| 90 | + if (requestCode == CAMERA_PERMISSIONS_REQUEST_CODE) { |
| 91 | + for (int result : grantResults) { |
| 92 | + if (result != PackageManager.PERMISSION_GRANTED) { |
| 93 | + finishNoQR(); |
| 94 | + } else { |
| 95 | + startCamera(); |
| 96 | + } |
| 97 | + } |
| 98 | + } else { |
| 99 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onQrCodeDetected(String result) { |
| 105 | + final Intent intent = new Intent(); |
| 106 | + intent.putExtra(QR_SCAN_RESULT, result); |
| 107 | + setResult(Activity.RESULT_OK, intent); |
| 108 | + finish(); |
| 109 | + } |
| 110 | + |
| 111 | + private void startCamera() { |
| 112 | + final TextureView textureView = findViewById(R.id.view_finder); |
| 113 | + final QRCodePreview qrCodePreview = new QRCodePreview( |
| 114 | + CameraConfigProvider.getPreviewConfig(getDisplaySize()), |
| 115 | + textureView); |
| 116 | + final QRCodeImageAnalysis qrCodeImageAnalysis = new QRCodeImageAnalysis( |
| 117 | + CameraConfigProvider.getImageAnalysisConfig(), executorService, this); |
| 118 | + |
| 119 | + CameraX.bindToLifecycle(this, qrCodePreview.getUseCase(), qrCodeImageAnalysis.getUseCase()); |
| 120 | + } |
| 121 | + |
| 122 | + private void finishNoQR() { |
| 123 | + setResult(Activity.RESULT_CANCELED); |
| 124 | + finish(); |
| 125 | + } |
| 126 | + |
| 127 | + private Size getDisplaySize() { |
| 128 | + final DisplayMetrics displayMetrics = new DisplayMetrics(); |
| 129 | + getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); |
| 130 | + return new Size(displayMetrics.widthPixels, displayMetrics.heightPixels); |
| 131 | + } |
| 132 | +} |
0 commit comments