import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.appcompat.app.AppCompatActivity; | 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.camera.view.PreviewView; | ||||
import androidx.core.app.ActivityCompat; | import androidx.core.app.ActivityCompat; | ||||
import androidx.core.content.ContextCompat; | import androidx.core.content.ContextCompat; | ||||
import androidx.camera.core.ExperimentalGetImage; | import androidx.camera.core.ExperimentalGetImage; | ||||
import androidx.lifecycle.LifecycleOwner; | |||||
import android.Manifest; | import android.Manifest; | ||||
import android.content.pm.PackageManager; | import android.content.pm.PackageManager; | ||||
import android.graphics.Color; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.util.Log; | |||||
import android.view.View; | import android.view.View; | ||||
import android.widget.Button; | |||||
import android.widget.TextView; | import android.widget.TextView; | ||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import android.widget.ToggleButton; | |||||
import com.example.ueberwachungssystem.VideoDetection.DetectionReport; | import com.example.ueberwachungssystem.VideoDetection.DetectionReport; | ||||
import com.example.ueberwachungssystem.VideoDetection.VideoDetector; | import com.example.ueberwachungssystem.VideoDetection.VideoDetector; | ||||
import com.google.common.util.concurrent.ListenableFuture; | |||||
import java.util.Calendar; | |||||
import java.util.concurrent.ExecutionException; | |||||
@ExperimentalGetImage | @ExperimentalGetImage | ||||
public class MainActivity extends AppCompatActivity { | public class MainActivity extends AppCompatActivity { | ||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101; | private static final int CAMERA_PERMISSION_REQUEST_CODE = 101; | ||||
private PreviewView previewView; | private PreviewView previewView; | ||||
private TextView textView; | private TextView textView; | ||||
private Button previewButton; | |||||
private ToggleButton toggleButton; | |||||
private Boolean cameraPreviewIsRunning = false; | private Boolean cameraPreviewIsRunning = false; | ||||
private Boolean isPressed = false; | private Boolean isPressed = false; | ||||
textView = findViewById(R.id.textView); | textView = findViewById(R.id.textView); | ||||
previewView = findViewById(R.id.previewView); | previewView = findViewById(R.id.previewView); | ||||
previewButton = findViewById(R.id.previewButton); | |||||
toggleButton = findViewById(R.id.previewButton); | |||||
VideoDetector vd = new VideoDetector(this); | VideoDetector vd = new VideoDetector(this); | ||||
@Override | @Override | ||||
public void onDetection(DetectionReport detectionReport) { | public void onDetection(DetectionReport detectionReport) { | ||||
detectionReport.log("OnDetection"); | detectionReport.log("OnDetection"); | ||||
textView.setText(detectionReport.toString()); | |||||
} | } | ||||
}); | }); | ||||
vd.startDetection(); | |||||
//vd.startDetection(); | |||||
previewButton.setOnClickListener(new View.OnClickListener() { | |||||
toggleButton.setOnClickListener(new View.OnClickListener() { | |||||
@Override | @Override | ||||
public void onClick(View v) { | public void onClick(View v) { | ||||
getCameraAccess(); | getCameraAccess(); | ||||
if (isCameraAccessAllowed() && !isPressed) | |||||
if (isCameraAccessAllowed() && toggleButton.isChecked()) | |||||
{ | { | ||||
vd.stopDetection(); | |||||
isPressed = true; | |||||
} | |||||
else if (isCameraAccessAllowed() && isPressed) { | |||||
vd.startDetection(); | vd.startDetection(); | ||||
isPressed = false; | |||||
} | |||||
else { | |||||
vd.stopDetection(); | |||||
} | } | ||||
} | } | ||||
}); | }); |
this.detectorID = detectorID; | 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 */ | /** Debug Report */ | ||||
public void log(String tag) { | 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()); | |||||
} | } | ||||
} | } |
// Preview Camera Image | // Preview Camera Image | ||||
private PreviewView previewView = null; | private PreviewView previewView = null; | ||||
// Check Violation | // 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 | // On Detection Listener | ||||
private OnDetectionListener listener; | private OnDetectionListener listener; | ||||
// Analyze frame | // Analyze frame | ||||
float luminosity = calculateLuminosity(image); | float luminosity = calculateLuminosity(image); | ||||
Log.d("Video Detector", String.valueOf(luminosity)); | Log.d("Video Detector", String.valueOf(luminosity)); | ||||
checkForViolation(luminosity, previousLuminosity); | |||||
if (previousLuminosity != null) | |||||
checkForViolation(luminosity, previousLuminosity); | |||||
previousLuminosity = luminosity; | previousLuminosity = luminosity; | ||||
} | } | ||||
imageProxy.close(); | imageProxy.close(); | ||||
preview.setSurfaceProvider(previewView.getSurfaceProvider()); | preview.setSurfaceProvider(previewView.getSurfaceProvider()); | ||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview); | cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview); | ||||
stopDetection(); | |||||
} | } | ||||
android:id="@+id/textView" | android:id="@+id/textView" | ||||
android:layout_width="wrap_content" | android:layout_width="wrap_content" | ||||
android:layout_height="wrap_content" | android:layout_height="wrap_content" | ||||
android:text="Hello World!" | |||||
android:text="" | |||||
app:layout_constraintBottom_toBottomOf="parent" | app:layout_constraintBottom_toBottomOf="parent" | ||||
app:layout_constraintEnd_toEndOf="parent" | app:layout_constraintEnd_toEndOf="parent" | ||||
app:layout_constraintStart_toStartOf="parent" | app:layout_constraintStart_toStartOf="parent" | ||||
app:layout_constraintTop_toTopOf="parent" /> | app:layout_constraintTop_toTopOf="parent" /> | ||||
<Button | |||||
<ToggleButton | |||||
android:id="@+id/previewButton" | android:id="@+id/previewButton" | ||||
android:layout_width="wrap_content" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="wrap_content" | 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 | <androidx.camera.view.PreviewView | ||||
android:id="@+id/previewView" | android:id="@+id/previewView" | ||||
android:layout_width="match_parent" | android:layout_width="match_parent" | ||||
android:layout_height="match_parent" /> | |||||
android:layout_height="match_parent" | |||||
android:backgroundTint="@android:color/black"/> | |||||
</LinearLayout> | </LinearLayout> |