@@ -2,31 +2,22 @@ package com.example.ueberwachungssystem; | |||
import androidx.annotation.NonNull; | |||
import androidx.appcompat.app.AppCompatActivity; | |||
import androidx.camera.core.Camera; | |||
import androidx.camera.core.CameraSelector; | |||
import androidx.camera.core.Preview; | |||
import androidx.camera.lifecycle.ProcessCameraProvider; | |||
import androidx.camera.view.PreviewView; | |||
import androidx.core.app.ActivityCompat; | |||
import androidx.core.content.ContextCompat; | |||
import androidx.camera.core.ExperimentalGetImage; | |||
import androidx.lifecycle.LifecycleOwner; | |||
import android.Manifest; | |||
import android.content.pm.PackageManager; | |||
import android.graphics.Color; | |||
import android.os.Bundle; | |||
import android.util.Log; | |||
import android.view.View; | |||
import android.widget.Button; | |||
import android.widget.TextView; | |||
import android.widget.Toast; | |||
import android.widget.ToggleButton; | |||
import com.example.ueberwachungssystem.VideoDetection.DetectionReport; | |||
import com.example.ueberwachungssystem.VideoDetection.VideoDetector; | |||
import com.google.common.util.concurrent.ListenableFuture; | |||
import java.util.Calendar; | |||
import java.util.concurrent.ExecutionException; | |||
@ExperimentalGetImage | |||
public class MainActivity extends AppCompatActivity { | |||
@@ -34,7 +25,7 @@ public class MainActivity extends AppCompatActivity { | |||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101; | |||
private PreviewView previewView; | |||
private TextView textView; | |||
private Button previewButton; | |||
private ToggleButton toggleButton; | |||
private Boolean cameraPreviewIsRunning = false; | |||
private Boolean isPressed = false; | |||
@@ -46,7 +37,7 @@ public class MainActivity extends AppCompatActivity { | |||
textView = findViewById(R.id.textView); | |||
previewView = findViewById(R.id.previewView); | |||
previewButton = findViewById(R.id.previewButton); | |||
toggleButton = findViewById(R.id.previewButton); | |||
VideoDetector vd = new VideoDetector(this); | |||
@@ -55,23 +46,22 @@ public class MainActivity extends AppCompatActivity { | |||
@Override | |||
public void onDetection(DetectionReport detectionReport) { | |||
detectionReport.log("OnDetection"); | |||
textView.setText(detectionReport.toString()); | |||
} | |||
}); | |||
vd.startDetection(); | |||
//vd.startDetection(); | |||
previewButton.setOnClickListener(new View.OnClickListener() { | |||
toggleButton.setOnClickListener(new View.OnClickListener() { | |||
@Override | |||
public void onClick(View v) { | |||
getCameraAccess(); | |||
if (isCameraAccessAllowed() && !isPressed) | |||
if (isCameraAccessAllowed() && toggleButton.isChecked()) | |||
{ | |||
vd.stopDetection(); | |||
isPressed = true; | |||
} | |||
else if (isCameraAccessAllowed() && isPressed) { | |||
vd.startDetection(); | |||
isPressed = false; | |||
} | |||
else { | |||
vd.stopDetection(); | |||
} | |||
} | |||
}); |
@@ -18,14 +18,19 @@ public class DetectionReport { | |||
this.detectorID = detectorID; | |||
} | |||
/** Get Detection Report in String format */ | |||
public String toString() { | |||
String time = "Time: " + "[" + this.timeStamp + "]"; | |||
String type = "Type: " + "[" + this.detectionType + "]"; | |||
String value = "Value: " + "[" + this.detectedValue + "]"; | |||
String id = "ID: " + "[" + this.detectorID + "]"; | |||
return String.join("\t", time, type, value, id); | |||
} | |||
/** Debug Report */ | |||
public void log(String tag) { | |||
String time = "Time: [" + this.timeStamp + "]"; | |||
String type = "Type: [" + this.detectionType + "]"; | |||
String value = "Value: [" + this.detectedValue + "]"; | |||
String id = "ID: [" + this.detectorID + "]"; | |||
String message = String.join("\t", time, type, value, id); | |||
Log.d(tag, message); | |||
Log.d(tag, this.toString()); | |||
} | |||
} |
@@ -34,8 +34,8 @@ public class VideoDetector { | |||
// Preview Camera Image | |||
private PreviewView previewView = null; | |||
// Check Violation | |||
private final float DELTA_LUMINOSITY_THRESHOLD = 0.5f; | |||
private float previousLuminosity = 0f; | |||
private final float DELTA_LUMINOSITY_THRESHOLD = 0.3f; | |||
private Float previousLuminosity = null; | |||
// On Detection Listener | |||
private OnDetectionListener listener; | |||
@@ -124,7 +124,8 @@ public class VideoDetector { | |||
// Analyze frame | |||
float luminosity = calculateLuminosity(image); | |||
Log.d("Video Detector", String.valueOf(luminosity)); | |||
checkForViolation(luminosity, previousLuminosity); | |||
if (previousLuminosity != null) | |||
checkForViolation(luminosity, previousLuminosity); | |||
previousLuminosity = luminosity; | |||
} | |||
imageProxy.close(); | |||
@@ -139,6 +140,7 @@ public class VideoDetector { | |||
preview.setSurfaceProvider(previewView.getSurfaceProvider()); | |||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview); | |||
stopDetection(); | |||
} | |||
@@ -13,23 +13,24 @@ | |||
android:id="@+id/textView" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:text="Hello World!" | |||
android:text="" | |||
app:layout_constraintBottom_toBottomOf="parent" | |||
app:layout_constraintEnd_toEndOf="parent" | |||
app:layout_constraintStart_toStartOf="parent" | |||
app:layout_constraintTop_toTopOf="parent" /> | |||
<Button | |||
<ToggleButton | |||
android:id="@+id/previewButton" | |||
android:layout_width="wrap_content" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:text="Button" | |||
tools:layout_editor_absoluteX="156dp" | |||
tools:layout_editor_absoluteY="189dp" /> | |||
android:text="ToggleButton" /> | |||
<androidx.camera.view.PreviewView | |||
android:id="@+id/previewView" | |||
android:layout_width="match_parent" | |||
android:layout_height="match_parent" /> | |||
android:layout_height="match_parent" | |||
android:backgroundTint="@android:color/black"/> | |||
</LinearLayout> |