From cfebc7c83dc28f33bb6304344e1c3cc9694f7166 Mon Sep 17 00:00:00 2001 From: Hiago Lopes Date: Thu, 16 Dec 2021 08:13:29 -0300 Subject: [PATCH] feat: add timeout to force manual capture on error --- app/build.gradle | 4 +- .../camerabiomanager/SelfieActivity.java | 41 ++++++++++++++++--- .../camerabiomanager/SelfieActivity.java | 41 ++++++++++++++++--- 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 82e5a43..e706431 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "com.example.camerabioexample_android" minSdkVersion 19 targetSdkVersion 28 - versionCode 6 - versionName "2.3.2" + versionCode 7 + versionName "2.4.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } diff --git a/app/src/main/java/com/example/camerabioexample_android/camerabiomanager/SelfieActivity.java b/app/src/main/java/com/example/camerabioexample_android/camerabiomanager/SelfieActivity.java index dca1640..e108337 100644 --- a/app/src/main/java/com/example/camerabioexample_android/camerabiomanager/SelfieActivity.java +++ b/app/src/main/java/com/example/camerabioexample_android/camerabiomanager/SelfieActivity.java @@ -59,6 +59,7 @@ public class SelfieActivity extends Camera2Base implements ImageProcessor, Captu "Gire um pouco a direita", "Rosto não identificado", "Rosto inclinado"} ; + private final String manualCaptureMessage = "Pressione o botão para tirar a foto"; private int erroIndex = -1; private boolean faceOK = true; @@ -137,6 +138,9 @@ public class SelfieActivity extends Camera2Base implements ImageProcessor, Captu private Boolean autoCapture; private Boolean countRegressive; + // contador de erro + private CountDownTimer errorCountDownTimer; + private Boolean forcedManualCapture = Boolean.FALSE; private static CameraBioManager cameraBioManager; @@ -354,7 +358,6 @@ else if (diffNose < noseRange || diffNose > maxDiffNose) { } else { markRed(); - takePictureImageButton.setEnabled(false); } // exibe as grides em tela (caso ativo) if (showLines) { @@ -368,20 +371,20 @@ else if (diffNose < noseRange || diffNose > maxDiffNose) { else { erroIndex = 7; markRed(); - takePictureImageButton.setEnabled(false); } } else { erroIndex = 7; markRed(); - takePictureImageButton.setEnabled(false); } runOnUiThread(new Runnable() { @Override public void run() { - if (erroIndex != -1) { + if (erroIndex != -1 && forcedManualCapture) { + showFastToast(manualCaptureMessage); + } else if (erroIndex != -1) { showFastToast(mensagens[erroIndex]); } else if (toast != null) { toast.cancel(); @@ -439,6 +442,32 @@ public void onFinish() { } } + private void destroyErrorTimer () { + if (errorCountDownTimer != null) { + errorCountDownTimer.cancel(); + errorCountDownTimer = null; + } + } + + private void createErrorTimer () { + if (errorCountDownTimer == null && !forcedManualCapture) { + takePictureImageButton.setEnabled(false); + errorCountDownTimer = new CountDownTimer(5000, 1000) { + + public void onTick(long millisUntilFinished) {} + + public void onFinish() { + autoCapture = Boolean.FALSE; + forcedManualCapture = Boolean.TRUE; + takePictureImageButton.setEnabled(true); + markBlue(); + } + + }; + errorCountDownTimer.start(); + } + } + private void autoCapture () { if(countDownTimer == null && isRequestImage == false) { @@ -469,6 +498,7 @@ public static float convertDpToPixel(float dp, Context context){ } private void markBlue() { + destroyErrorTimer(); erroIndex =-1; @@ -492,8 +522,9 @@ else if (autoCapture) { private void markRed() { destroyTimer(); + createErrorTimer(); - if (!countDownCancelled[0]) { + if (!countDownCancelled[0] && !forcedManualCapture) { int size = 18; if (screenWidth > 1600) { size = 34; diff --git a/camerabioandroid/src/main/java/com/example/camerabioandroid/camerabiomanager/SelfieActivity.java b/camerabioandroid/src/main/java/com/example/camerabioandroid/camerabiomanager/SelfieActivity.java index bbdbd86..d08f512 100644 --- a/camerabioandroid/src/main/java/com/example/camerabioandroid/camerabiomanager/SelfieActivity.java +++ b/camerabioandroid/src/main/java/com/example/camerabioandroid/camerabiomanager/SelfieActivity.java @@ -56,6 +56,7 @@ public class SelfieActivity extends Camera2Base implements ImageProcessor, Captu "Gire um pouco a direita", "Rosto não identificado", "Rosto inclinado"} ; + private final String manualCaptureMessage = "Pressione o botão para tirar a foto"; private int erroIndex = -1; private boolean faceOK = true; @@ -134,6 +135,9 @@ public class SelfieActivity extends Camera2Base implements ImageProcessor, Captu private Boolean autoCapture; private Boolean countRegressive; + // contador de erro + private CountDownTimer errorCountDownTimer; + private Boolean forcedManualCapture = Boolean.FALSE; private static CameraBioManager cameraBioManager; @@ -349,7 +353,6 @@ else if (diffNose < noseRange || diffNose > maxDiffNose) { } else { markRed(); - takePictureImageButton.setEnabled(false); } // exibe as grides em tela (caso ativo) if (showLines) { @@ -363,20 +366,20 @@ else if (diffNose < noseRange || diffNose > maxDiffNose) { else { erroIndex = 7; markRed(); - takePictureImageButton.setEnabled(false); } } else { erroIndex = 7; markRed(); - takePictureImageButton.setEnabled(false); } runOnUiThread(new Runnable() { @Override public void run() { - if (erroIndex != -1) { + if (erroIndex != -1 && forcedManualCapture) { + showFastToast(manualCaptureMessage); + } else if (erroIndex != -1) { showFastToast(mensagens[erroIndex]); } else if (toast != null) { toast.cancel(); @@ -434,6 +437,32 @@ public void onFinish() { } } + private void destroyErrorTimer () { + if (errorCountDownTimer != null) { + errorCountDownTimer.cancel(); + errorCountDownTimer = null; + } + } + + private void createErrorTimer () { + if (errorCountDownTimer == null && !forcedManualCapture) { + takePictureImageButton.setEnabled(false); + errorCountDownTimer = new CountDownTimer(5000, 1000) { + + public void onTick(long millisUntilFinished) {} + + public void onFinish() { + autoCapture = Boolean.FALSE; + forcedManualCapture = Boolean.TRUE; + takePictureImageButton.setEnabled(true); + markBlue(); + } + + }; + errorCountDownTimer.start(); + } + } + private void autoCapture () { if(countDownTimer == null && isRequestImage == false) { @@ -464,6 +493,7 @@ public static float convertDpToPixel(float dp, Context context){ } private void markBlue() { + destroyErrorTimer(); erroIndex =-1; @@ -487,8 +517,9 @@ else if (autoCapture) { private void markRed() { destroyTimer(); + createErrorTimer(); - if (!countDownCancelled[0]) { + if (!countDownCancelled[0] && !forcedManualCapture) { int size = 18; if (screenWidth > 1600) { size = 34;