Compare commits
3 Commits
1576719d10
...
3d799ae461
Author | SHA1 | Date | |
---|---|---|---|
3d799ae461 | |||
2501662737 | |||
d04d166e14 |
@ -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);
|
||||
@ -54,24 +45,23 @@ public class MainActivity extends AppCompatActivity {
|
||||
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() {
|
||||
@Override
|
||||
public void onDetection(DetectionReport detectionReport) {
|
||||
Log.d("Listener", detectionReport.detectionType);
|
||||
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();
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -99,39 +89,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<ProcessCameraProvider> 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);
|
||||
}
|
||||
}
|
@ -1,19 +1,36 @@
|
||||
package com.example.ueberwachungssystem.VideoDetection;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
/** Detection Report Class */
|
||||
|
||||
public class DetectionReport {
|
||||
public String timeStamp;
|
||||
public String detectionType;
|
||||
public float detectedAmplitude;
|
||||
public float detectedValue;
|
||||
public String detectorID;
|
||||
|
||||
public DetectionReport(String detectorID, String detectionType, float detectedAmplitude) {
|
||||
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
|
||||
this.detectionType = detectionType;
|
||||
this.detectedAmplitude = detectedAmplitude;
|
||||
this.detectedValue = detectedAmplitude;
|
||||
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) {
|
||||
Log.d(tag, this.toString());
|
||||
}
|
||||
}
|
@ -25,6 +25,7 @@ import java.util.concurrent.ExecutionException;
|
||||
|
||||
@ExperimentalGetImage
|
||||
public class VideoDetector {
|
||||
|
||||
// Calling Activity
|
||||
private final Context context;
|
||||
// Camera Provider
|
||||
@ -33,28 +34,31 @@ 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;
|
||||
|
||||
|
||||
|
||||
/** 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<ProcessCameraProvider> 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;
|
||||
}
|
||||
@ -116,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();
|
||||
@ -131,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>
|
Loading…
x
Reference in New Issue
Block a user