From cd3db850b5e153bb7540238b25381df0a182413e Mon Sep 17 00:00:00 2001 From: Xin Yang <105740670+xyang16@users.noreply.github.com> Date: Mon, 19 Sep 2022 10:08:33 -0700 Subject: [PATCH] [android] Handle request permissions result (#262) --- .../SemanticActivity.java | 90 ++++++++++--------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/android/pytorch_android/semantic_segmentation/src/main/java/ai/djl/examples/semanticsegmentation/SemanticActivity.java b/android/pytorch_android/semantic_segmentation/src/main/java/ai/djl/examples/semanticsegmentation/SemanticActivity.java index bfebc36f..fcbc0b0a 100644 --- a/android/pytorch_android/semantic_segmentation/src/main/java/ai/djl/examples/semanticsegmentation/SemanticActivity.java +++ b/android/pytorch_android/semantic_segmentation/src/main/java/ai/djl/examples/semanticsegmentation/SemanticActivity.java @@ -93,43 +93,44 @@ protected void onCreate(Bundle savedInstanceState) { mCaptureButton = binding.captureButton; mCaptureButton.setOnClickListener(view -> { - view.setEnabled(false); + mCaptureButton.setEnabled(false); mImagePredicted.setVisibility(View.GONE); - imageCapture.takePicture(executor, new ImageCapture.OnImageCapturedCallback() { - - @Override - public void onCaptureSuccess(@NonNull ImageProxy image) { - ByteBuffer buffer = image.getPlanes()[0].getBuffer(); - byte[] bytes = new byte[buffer.remaining()]; - buffer.get(bytes); - bitmapBuffer = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); - - // Rotate image if needed - if (image.getImageInfo().getRotationDegrees() != 0) { - Matrix matrix = new Matrix(); - matrix.postRotate(image.getImageInfo().getRotationDegrees()); - bitmapBuffer = Bitmap.createBitmap(bitmapBuffer, 0, 0, bitmapBuffer.getWidth(), bitmapBuffer.getHeight(), matrix, true); + if (imageCapture != null) { + imageCapture.takePicture(executor, new ImageCapture.OnImageCapturedCallback() { + + @Override + public void onCaptureSuccess(@NonNull ImageProxy image) { + ByteBuffer buffer = image.getPlanes()[0].getBuffer(); + byte[] bytes = new byte[buffer.remaining()]; + buffer.get(bytes); + bitmapBuffer = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); + image.close(); + + // Rotate image if needed + if (image.getImageInfo().getRotationDegrees() != 0) { + Matrix matrix = new Matrix(); + matrix.postRotate(image.getImageInfo().getRotationDegrees()); + bitmapBuffer = Bitmap.createBitmap(bitmapBuffer, 0, 0, bitmapBuffer.getWidth(), bitmapBuffer.getHeight(), matrix, true); + } + + runOnUiThread(() -> { + mViewFinder.setVisibility(View.INVISIBLE); + mImagePreview.setImageBitmap(bitmapBuffer); + mImagePreview.setVisibility(View.VISIBLE); + mProgressBar.setVisibility(View.VISIBLE); + }); + + executor.execute(new InferenceTask()); } - runOnUiThread(() -> { - mViewFinder.setVisibility(View.INVISIBLE); - mImagePreview.setImageBitmap(bitmapBuffer); - mImagePreview.setVisibility(View.VISIBLE); - mProgressBar.setVisibility(View.VISIBLE); - }); - - executor.execute(new InferenceTask()); - - image.close(); - } - - @Override - public void onError(@NonNull ImageCaptureException exception) { - Log.e(TAG, "Photo capture failed: " + exception.getMessage()); + @Override + public void onError(@NonNull ImageCaptureException exception) { + Log.e(TAG, "Photo capture failed: " + exception.getMessage()); + } } - } - ); + ); + } }); mCloseImageButton.setOnClickListener(view -> { @@ -141,23 +142,17 @@ public void onError(@NonNull ImageCaptureException exception) { mCaptureButton.setEnabled(true); }); - if (checkCameraPermission()) { - setUpCamera(); - mCaptureButton.setEnabled(true); - } else { - requestCameraPermission(); - } - - Snackbar.make(findViewById(android.R.id.content), R.string.message_download_model, Snackbar.LENGTH_LONG).show(); + Snackbar.make(findViewById(android.R.id.content), R.string.message_download_model, + Snackbar.LENGTH_LONG).show(); executor.execute(new LoadModelTask()); } @Override protected void onResume() { super.onResume(); + // Request permissions each time the app resumes, since they can be revoked at any time if (checkCameraPermission()) { setUpCamera(); - mCaptureButton.setEnabled(true); } else { requestCameraPermission(); } @@ -174,6 +169,18 @@ protected void onDestroy() { super.onDestroy(); } + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (requestCode == CAMERA_REQUEST_CODE) { + if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + setUpCamera(); + } else { + mCaptureButton.setEnabled(false); + } + } + } + @NonNull @Override public CameraXConfig getCameraXConfig() { @@ -236,6 +243,7 @@ public void run() { model = SemanticModel.loadModel(); predictor = model.newPredictor(); runOnUiThread(() -> { + mCaptureButton.setEnabled(true); Snackbar.make(findViewById(android.R.id.content), R.string.message_download_model_complete, Snackbar.LENGTH_LONG).show(); }); } catch (IOException | ModelException e) {