From 250166273769c361c0a01f31e8aba0cf1dd34c15 Mon Sep 17 00:00:00 2001 From: Bastian Kohler Date: Thu, 18 May 2023 12:02:18 +0200 Subject: [PATCH] Fixed a bug in the video decoder --- .../ueberwachungssystem/MainActivity.java | 37 +------------------ .../VideoDetection/VideoDetector.java | 18 ++++++--- 2 files changed, 14 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index a32cf6e..4e1d659 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -54,7 +54,7 @@ public class MainActivity extends AppCompatActivity { vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() { @Override public void onDetection(DetectionReport detectionReport) { - Log.d("Listener", detectionReport.detectionType); + detectionReport.log("OnDetection"); } }); vd.startDetection(); @@ -99,39 +99,4 @@ public class MainActivity extends AppCompatActivity { } } } - - - private void previewCamera() { - // Request Camera Access - getCameraAccess(); - // Return when Camera Access not allowed or Camera Preview is running - if (!isCameraAccessAllowed() || cameraPreviewIsRunning) - return; - // Camera Preview is running - cameraPreviewIsRunning = true; - // Request Camera Provider - final ListenableFuture cameraProviderFuture = ProcessCameraProvider.getInstance(this); - //Check for Camera availability - cameraProviderFuture.addListener(new Runnable() { - @Override - public void run() { - try { - ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); - bindPreview(cameraProvider); - } catch (ExecutionException | InterruptedException e) { - // No errors need to be handled for this Future. This should never be reached. - } - } - },ContextCompat.getMainExecutor(this)); - } - - private void bindPreview(@NonNull ProcessCameraProvider cameraProvider) { - // Create Preview - Preview preview = new Preview.Builder().build(); - // Specify which Camera to use - CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build(); - // Connect Preview to PreviewView - preview.setSurfaceProvider(previewView.getSurfaceProvider()); - Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview); - } } \ No newline at end of file diff --git a/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java b/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java index f7d4b95..c09c849 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java @@ -25,6 +25,7 @@ import java.util.concurrent.ExecutionException; @ExperimentalGetImage public class VideoDetector { + // Calling Activity private final Context context; // Camera Provider @@ -41,20 +42,23 @@ public class VideoDetector { /** Constructor */ - public VideoDetector(Context context) { this.context = context; } + public VideoDetector(Context context) { + this.context = context; + } /** On Detection Listener - runs when a violation is reported */ public interface OnDetectionListener { - public void onDetection(DetectionReport detectionReport); + void onDetection(DetectionReport detectionReport); } public void setOnDetectionListener(OnDetectionListener listener) { this.listener = listener; } public void reportViolation(float amplitude) { - DetectionReport detectionReport = new DetectionReport("123", "Video", amplitude); - if (listener != null) + if (listener != null) { + DetectionReport detectionReport = new DetectionReport("123", "Video", amplitude); listener.onDetection(detectionReport); + } } @@ -66,7 +70,8 @@ public class VideoDetector { /** Starts Video Detection */ public void startDetection() { - isDetectionRunning = true; + if (isDetectionRunning) + return; // Request Camera Provider final ListenableFuture cameraProviderFuture = ProcessCameraProvider.getInstance(context); //Check for Camera availability @@ -76,6 +81,7 @@ public class VideoDetector { try { cameraProvider = cameraProviderFuture.get(); bindLuminosityAnalysis(cameraProvider); + isDetectionRunning = true; } catch (ExecutionException | InterruptedException e) { // No errors need to be handled for this Future. This should never be reached. } @@ -86,6 +92,8 @@ public class VideoDetector { /** Stops Video Detection */ public void stopDetection() { + if (!isDetectionRunning) + return; cameraProvider.unbindAll(); isDetectionRunning = false; }