Compare commits

..

66 Commits

Author SHA1 Message Date
b667e70fef Kommunikationcopie Version 1.8 UDP Socket 2023-06-20 22:30:30 +02:00
c198292fbf Merge remote-tracking branch 'origin/master' into Kommunikationcopie
# Conflicts:
#	app/src/main/AndroidManifest.xml
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
#	app/src/main/java/com/example/ueberwachungssystem/WifiCommunication.java
#	app/src/main/res/layout/activity_main.xml
2023-06-20 21:13:26 +02:00
f8f1fd23fb Merge branch 'bk_video'
# Conflicts:
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
2023-06-20 15:34:24 +02:00
e01e23eccd Refactor 2023-06-20 14:29:10 +02:00
a493bafb79 Refactor 2023-06-20 11:16:26 +02:00
aed079377f Refactor 2023-06-20 11:07:57 +02:00
bff6abbd29 Removed Permission Handling from Video Detector, Added PermissionHandler.java class 2023-06-20 11:00:26 +02:00
5d31f0bb46 Adapted Parameters 2023-06-19 15:47:40 +02:00
ea0e88ed89 Refactor in DetectorService class 2023-06-19 14:51:04 +02:00
7dee124378 Refactor in DetectorService class 2023-06-19 14:41:41 +02:00
0ee1446cbc Merge branch 'bk_video_service' into bk_video
# Conflicts:
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
2023-06-19 14:40:06 +02:00
bc5227ba4f Refactor of DetectorService 2023-06-19 14:37:49 +02:00
4185cb5200 Proper display of Toasts in Audio Recorder 2023-06-19 14:34:07 +02:00
0a3600d0d6 Refactor 2023-06-19 14:27:04 +02:00
9b2d57ad37 Refactor 2023-06-19 13:35:02 +02:00
847e90f5ac Fixed Bug in VideoDetector 2023-06-19 13:17:17 +02:00
ab41217f0e Added DetectionService 2023-06-19 13:13:23 +02:00
e088b0afa3 Working Recorder and Video Detector 2023-06-18 20:17:19 +02:00
493fa8ca83 Fixed Grey Stripes and Rotation Problems 2023-06-18 18:12:03 +02:00
6954d38143 Moved OpenCV Helper to its own Java File 2023-06-18 13:43:10 +02:00
057c7fc139 Added Audio Recorder 2023-06-18 13:03:38 +02:00
29c193b716 Removed Preview Use Case, Audio still not working 2023-06-18 10:55:21 +02:00
76442888f7 Working Video Detector + Recorder WIP: Use Case logic 2023-06-17 19:44:08 +02:00
d32e0a11f5 Fixed bug in Detector and VideoDetector 2023-06-17 11:20:56 +02:00
df9b28c830 merge Kommunikation Version 1.2 UDP Socket 2023-06-15 16:56:59 +02:00
6ec0b175e4 merge Kommunikation Version 1.2 UDP Socket 2023-06-15 16:56:21 +02:00
fb8bdcd895 Fixed up Video detector 2023-06-15 12:40:54 +02:00
7ef0df48dd Refactored Detector 2023-06-14 20:14:06 +02:00
50b579705e Detector based on OpenCV working 2023-06-14 19:43:18 +02:00
8551abfc5d Changde Detector class 2023-06-14 16:38:33 +02:00
bbd333426d unfinished changes in Detector 2023-06-13 20:55:30 +02:00
e98ae10267 refactor 2023-06-12 19:03:52 +02:00
7d29ed1ca9 Rebuild to use openCV. Alarm not yet implemented 2023-06-09 20:56:14 +02:00
f98079d431 Readded main and main xml 2023-06-09 09:09:50 +02:00
11dbcd0362 Merge branch 'bk_video' of siebenhaarmi76624/MDT5_1_Ueberwachungssystem_G1 into master 2023-06-02 15:51:55 +00:00
047d45f1cb Added comments to VideoDetector class 2023-06-02 17:48:41 +02:00
95919a6602 preparing branch for merge 2023-06-02 14:56:57 +02:00
19d1bfe1e3 Refactor 2023-05-29 10:43:51 +02:00
e9da712882 Merge remote-tracking branch 'origin/bk_video' into bk_video 2023-05-28 22:30:58 +02:00
804c5aba36 Removed old Luminosity detection 2023-05-28 22:30:22 +02:00
c5577b125f Changed VideoDetector.java to detect based on changed pixel count instead of average luminosity 2023-05-28 22:30:22 +02:00
6100d17bd2 Refactored in Main 2023-05-28 22:30:22 +02:00
74a291bdd6 Removed olf Luminosity detection 2023-05-28 22:28:23 +02:00
ba6765b1fb Changed VideoDetector.java to detect based on changed pixel count instead of average luminosity 2023-05-28 22:20:56 +02:00
9fce3be6a8 Refactored some stuff in Main 2023-05-28 22:19:30 +02:00
9892fc35bc Refactor of Detector.java 2023-05-28 22:19:05 +02:00
62a528d33b Implemented VideoDetector as a child class of abstract class Detector 2023-05-28 21:16:17 +02:00
37617dbd54 Fixed a bug in Detector.java 2023-05-25 10:54:08 +02:00
569bd43163 Adjusted some values in xml and video detector for showcase 2023-05-25 10:20:11 +02:00
fdb0036ea6 Added comments to abstract class "Detector" 2023-05-25 10:19:21 +02:00
f9797f2dca Added Abstract class Detector 2023-05-24 23:51:49 +02:00
ba75c58a23 Refactor 2023-05-24 22:16:54 +02:00
3d799ae461 Rebuild Demo in Main Activity 2023-05-18 15:05:09 +02:00
2501662737 Fixed a bug in the video decoder 2023-05-18 12:02:18 +02:00
d04d166e14 Added log function to detection report 2023-05-18 12:01:45 +02:00
1576719d10 Added onDetectionListener to VideoDecoder 2023-05-16 22:35:50 +02:00
362af75b9a Added DetectionReportCalss 2023-05-16 22:34:32 +02:00
bef5d03494 Added functions for checking luminosity violation 2023-05-16 19:15:57 +02:00
80ebf77eb6 Removed empty Text File 2023-05-16 17:48:33 +02:00
c4ed247a05 added Functions to VideoDetector:
startDetection(), stopDetection(), isRunning()
2023-05-16 17:42:10 +02:00
323786859e added Video Decoder Class 2023-05-12 18:45:13 +02:00
354bf937b2 added camerax libraries to build.gradle 2023-05-12 09:37:58 +02:00
39bb37f5c3 removed VideoDetection.java 2023-05-11 21:46:15 +02:00
b198dc2e06 small refactor 2023-05-11 21:33:40 +02:00
BepsiKohler
d83cd3dc32 Added camera access permission 2023-05-11 20:58:01 +02:00
065acefeb9 Merge branch 'ms' of siebenhaarmi76624/MDT5_1_Ueberwachungssystem_G1 into master 2023-05-11 13:29:40 +00:00
13 changed files with 997 additions and 90 deletions

View File

View File

@ -40,4 +40,21 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
// OpenCV (from: https://github.com/QuickBirdEng/opencv-android)
def opencv_version = "4.5.3.0"
implementation "com.quickbirdstudios:opencv:${opencv_version}"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
// Required for CameraX
def camerax_version = "1.2.2"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"
}

View File

@ -2,11 +2,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" /> <!-- §permissionCamera§ -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
@ -14,20 +16,19 @@
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Ueberwachungssystem"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="Ueberwachungsapp">
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Detection.DetectorService"/>
</application>
</manifest>

View File

@ -0,0 +1,74 @@
package com.example.ueberwachungssystem.Detection;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class AudioRecorder {
private final Context context;
private MediaRecorder mediaRecorder = null;
private boolean isRecording = false;
private File outputDir; // Default: in app files directory
public AudioRecorder (Context context) {
this.context = context;
this.outputDir = context.getFilesDir();
}
public void startRecording() {
// Handle logic
if (outputDir == null)
return;
if (isRecording)
return;
isRecording = true;
// Setup Audio Recorder for output Format: 3GP
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(outputDir + "/" + generateFileName() + ".3gp");
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaRecorder.start();
}
public void stopRecording() {
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
isRecording = false;
Toast.makeText(context, "audio recording saved", Toast.LENGTH_SHORT).show();
}
}
public boolean isRecording(){
return isRecording;
}
public void setOutputDir(File outputDir) {
this.outputDir = outputDir;
}
private String generateFileName(){
// Get the current timestamp
LocalDateTime currentTime = LocalDateTime.now();
// Define the format for the timestamp
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");
// Return the timestamp as a string
return currentTime.format(formatter);
}
}

View File

@ -0,0 +1,38 @@
package com.example.ueberwachungssystem.Detection;
import android.util.Log;
import java.util.Calendar;
/** Detection Report Class */
public class DetectionReport {
public String timeStamp;
public String detectionType;
public float detectedValue;
public boolean detectionState;
public DetectionReport(boolean detectionState, String detectionType, float detectedAmplitude) {
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
this.detectionType = detectionType;
this.detectedValue = detectedAmplitude;
this.detectionState = detectionState;
//this.detectorID = detectorID;
}
/** Get Detection Report in String format */
public String toString() {
String state = "State: " + "[" + this.detectionState + "]";
String time = "Time: " + "[" + this.timeStamp + "]";
String type = "Type: " + "[" + this.detectionType + "]";
String value = "Value: " + "[" + this.detectedValue + "]";
return String.join("\t", state, time, type, value);
}
/** Debug Report */
public void log(String tag) {
Log.d(tag, this.toString());
}
}

View File

@ -0,0 +1,76 @@
package com.example.ueberwachungssystem.Detection;
import android.os.CountDownTimer;
import androidx.annotation.NonNull;
import androidx.camera.core.ExperimentalGetImage;
abstract public class Detector {
private OnDetectionListener listener;
private boolean isDetecting = false;
private boolean extendViolation = false;
// Countdown parameters
private final int COUNTDOWN_TIME = 10000; // milliseconds
private final int COUNTDOWN_POLLING_TIME = 100; // milliseconds
/** Constructor - takes context of current activity */
public Detector() {}
/** On Detection Listener - runs when violation is reported */
public interface OnDetectionListener {
void onDetection(@NonNull DetectionReport detectionReport);
}
public void setOnDetectionListener(@NonNull OnDetectionListener listener) {
this.listener = listener;
}
/** Triggers onDetectionListener - call this to trigger violation/alarm */
public void reportViolation(String detectionType, float amplitude) {
if (listener != null) {
if (!isDetecting) {
isDetecting = true;
DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude);
listener.onDetection(detectionReport);
startDetectionTimer(detectionType, amplitude);
} else {
extendViolation = true;
}
} else {
isDetecting = false;
extendViolation = false;
}
}
private void startDetectionTimer(String detectionType, float amplitude) {
isDetecting = true;
new CountDownTimer((long) COUNTDOWN_TIME, COUNTDOWN_POLLING_TIME) {
@Override
public void onTick(long millisUntilFinished) {
if (extendViolation) {
extendViolation = false;
startDetectionTimer(detectionType, amplitude);
this.cancel();
}
}
@Override
public void onFinish() {
isDetecting = false;
DetectionReport detectionReport = new DetectionReport(false, detectionType, amplitude);
listener.onDetection(detectionReport);
}
}.start();
}
public void extendViolation(){
this.extendViolation = true;
}
/** Starts Detection (abstract method: needs to be overridden in child class) */
public abstract void startDetection();
/** Stops Detection (abstract method: needs to be overridden in child class) */
public abstract void stopDetection();
}

View File

@ -0,0 +1,174 @@
package com.example.ueberwachungssystem.Detection;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.ExperimentalGetImage;
import androidx.lifecycle.LifecycleService;
import com.example.ueberwachungssystem.WifiCommunication;
import java.io.File;
@ExperimentalGetImage
public class DetectorService extends LifecycleService {
public ServiceBinder serviceBinder = new ServiceBinder();
private DetectorService.OnDetectionListener listener;
private boolean isServiceRunning = false;
VideoDetector videoDetector = null;
AudioRecorder audioRecorder = null;
/** Communication **/
WifiCommunication wifiCommunication;
StringBuffer dataFromWifi;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (isServiceRunning)
return START_NOT_STICKY;
// Setup Service classes:
videoDetector = new VideoDetector(this);
videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
@Override
public void onDetection(@NonNull DetectionReport detectionReport) {
passToServiceListener(detectionReport);
}
});
audioRecorder = new AudioRecorder(this);
isServiceRunning = true;
wifiCommunication = new WifiCommunication (1234);
wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
@Override
public void onConnection(StringBuffer data) {
dataFromWifi = data;
}
});
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
isServiceRunning = false;
}
/** Service methods */
public class ServiceBinder extends Binder {
public DetectorService getBoundService() {
// Return an instance of the TestService
return DetectorService.this;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
super.onBind(intent);
return serviceBinder;
}
/** Video Detection */
public void startVideoDetection() {
if(videoDetector != null)
videoDetector.startDetection();
}
public void stopVideoDetection() {
if(videoDetector != null)
videoDetector.stopDetection();
}
public boolean isVideoDetectionRunning() {
if(videoDetector != null)
return videoDetector.isDetecting();
return false;
}
public void debugVideoProcessing(ImageView input, ImageView output) {
if(videoDetector != null)
videoDetector.debugProcessing(input, output);
}
/** Audio Detection */
public void startAudioDetection() {
}
public void stopAudioDetection() {
}
/** Motion Detection */
public void startMotionDetection() {
}
public void stopMotionDetection() {
}
/** Video Recording */
public void startVideoRecording() {
if(videoDetector != null)
videoDetector.startRecording();
}
public void stopVideoRecording() {
if(videoDetector != null)
videoDetector.stopRecording();
}
public boolean isVideoRecordingRunning() {
if(videoDetector != null)
return videoDetector.isRecording();
return false;
}
public void setVideoRecordingDir(File outputDir) {
if (videoDetector != null)
videoDetector.setOutputDir(outputDir);
}
/** Audio Recording */
public void startAudioRecording() {
if(audioRecorder != null)
audioRecorder.startRecording();
}
public void stopAudioRecording() {
if(audioRecorder != null)
audioRecorder.stopRecording();
}
public boolean isAudioRecordingRunning() {
if(videoDetector != null)
return audioRecorder.isRecording();
return false;
}
public void setAudioRecordingDir(File outputDir) {
if (audioRecorder != null)
audioRecorder.setOutputDir(outputDir);
}
/** pass Detection Report to Service Detection Listener and trigger it */
public void passToServiceListener(DetectionReport detectionReport) {
if (listener != null) {
listener.onDetection(detectionReport);
}
}
/** On Detection Listener - runs when violation is reported */
public interface OnDetectionListener {
void onDetection(@NonNull DetectionReport detectionReport);
}
public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) {
this.listener = listener;
}
}

View File

@ -0,0 +1,109 @@
package com.example.ueberwachungssystem.Detection;
import android.graphics.Bitmap;
import android.media.Image;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.camera.core.ExperimentalGetImage;
import androidx.camera.core.ImageProxy;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ExperimentalGetImage
public class OpenCVHelper {
/** OpenCV helper methods **/
public static Mat addGaussianBlur(Mat inputMat, Size kernelSize){
Mat outputMat = new Mat();
Imgproc.GaussianBlur(inputMat, outputMat, kernelSize, 0);
return outputMat;
}
public static Mat addBlur(Mat inputMat, Size kernelSize){
Mat outputMat = new Mat();
Imgproc.blur(inputMat, outputMat, kernelSize);
return outputMat;
}
public static Mat extractYChannel(@NonNull ImageProxy imgProxy) {
Image img = imgProxy.getImage();
assert img != null;
ByteBuffer yBuffer = img.getPlanes()[0].getBuffer();
byte[] yData = new byte[yBuffer.remaining()];
yBuffer.get(yData);
Mat yMat = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
yMat.put(0, 0, yData);
return yMat;
}
public static Mat thresholdPixels(Mat inputMat, Mat previousImage, int threshold){
Mat diffImage = new Mat();
Core.absdiff(inputMat, previousImage, diffImage);
Mat binaryMat = new Mat();
Imgproc.threshold(diffImage, binaryMat, threshold, 255, Imgproc.THRESH_BINARY);
return binaryMat;
}
public static Mat thresholdContourArea(Mat inputMat, float areaThreshold){
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(inputMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
Mat outputMat = new Mat(inputMat.size(), inputMat.type(), new Scalar(0));
// Iterate over the contours and draw only the larger contours on the outputMat
for (MatOfPoint contour : contours) {
double contourArea = Imgproc.contourArea(contour);
if (contourArea > areaThreshold) {
Imgproc.drawContours(outputMat, Collections.singletonList(contour), 0, new Scalar(255), -1);
}
}
// Apply the outputMat as a mask to the dilatedImage
Mat maskedImage = new Mat();
inputMat.copyTo(maskedImage, outputMat);
return outputMat;
}
public static Mat dilateBinaryMat(Mat inputMat, Size kernelSize){
Mat dilatedMat = new Mat();
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, kernelSize);
Imgproc.dilate(inputMat, dilatedMat, kernel);
return dilatedMat;
}
public static int countNonZeroPixels(Mat inputImage) {
if (inputImage != null)
return Core.countNonZero(inputImage);
else
return 0;
}
public static void debugMat(Mat mat, ImageView imageView) {
if (imageView == null || mat == null)
return;
Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mat, bitmap);
// Display the bitmap in an ImageView
imageView.setImageBitmap(bitmap);
}
}

View File

@ -0,0 +1,327 @@
package com.example.ueberwachungssystem.Detection;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.media.Image;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ExperimentalGetImage;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageProxy;
import androidx.camera.core.Preview;
import androidx.camera.core.VideoCapture;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;
import com.google.common.util.concurrent.ListenableFuture;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ExecutionException;
/**
* Video Detector inherits some methods from abstract Detector class (more info there)
* USE FROM MAIN ACTIVITY:
* VideoDetector vd = new VideoDetector(this);
* */
@ExperimentalGetImage
public class VideoDetector extends Detector {
// Calling Activity
private final Context context;
// Camera Provider
private ProcessCameraProvider cameraProvider;
private ImageAnalysis imageAnalysis;
private VideoCapture videoCapture;
//private Preview preview;
// Logic
private boolean isDetecting = false;
private boolean isRecording = false;
private boolean allowReportViolation = false;
// Image Processing
private Mat previousImage = null;
// Debugging
private ImageView inputImageView = null;
private ImageView outputImageView = null;
// Recorder
private File outputDir; // Default: in app files directory
// Parameters
private static final float ALARM_THRESHOLD = 0f; // Percent of pixels changed
private static final float AREA_THRESHOLD = 10f;
private static final int DILATE_ITERATIONS = 2;
private static final float START_DELAY = 20000; // milliseconds
private static final android.util.Size IMAGE_RES = new android.util.Size(640, 480);
/** Constructor */
public VideoDetector(Context context) {
super();
this.context = context;
this.imageAnalysis = setupImageAnalysis();
this.videoCapture = setupVideoCapture();
this.outputDir = context.getFilesDir();
//this.preview = new Preview.Builder().build();
}
/** Get States */
public boolean isDetecting() {
return isDetecting;
}
public boolean isRecording(){
return isRecording;
}
/** Starts the Video Detection */
@Override
public void startDetection() {
// Check States
if (isDetecting)
return;
// Configure Image Analysis
imageAnalysis = setupImageAnalysis();
// Open CV startup check
if (!OpenCVLoader.initDebug()) {
Log.e("OpenCV", "Unable to load OpenCV!");
return;
} else
Log.d("OpenCV", "OpenCV loaded Successfully!");
// Get Process Camera Provider and start
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
cameraProviderFuture.addListener(() -> {
try {
cameraProvider = cameraProviderFuture.get();
isDetecting = true;
bindCameraProvider();
} catch (ExecutionException | InterruptedException e) {}
}, ContextCompat.getMainExecutor(context));
// Disable Violation Calling for Setup Time
startViolationTimer(START_DELAY);
}
/** Starts the Recorder */
@SuppressLint("RestrictedApi")
public void startRecording() {
// Check States
if (isRecording){
return;
}
videoCapture = setupVideoCapture();
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
cameraProviderFuture.addListener(() -> {
try {
cameraProvider = cameraProviderFuture.get();
isRecording = true;
bindCameraProvider();
File vidFile = new File(context.getFilesDir() + "/" + generateFileName() + ".mp4");
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
return;
}
videoCapture.startRecording(
new VideoCapture.OutputFileOptions.Builder(vidFile).build(),
context.getMainExecutor(),
new VideoCapture.OnVideoSavedCallback() {
@Override
public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) {
isRecording = false;
Toast.makeText(context, "video recording saved", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
isRecording = false;
Toast.makeText(context, "video recording failed", Toast.LENGTH_SHORT).show();
}
}
);
} catch (ExecutionException | InterruptedException ignored) {}
}, ContextCompat.getMainExecutor(context));
}
/** Stops the Video Detection */
@Override
public void stopDetection() {
if (!isDetecting || imageAnalysis == null)
return;
cameraProvider.unbind(imageAnalysis);
isDetecting = false;
allowReportViolation = false;
}
/** Stops the Recording */
@SuppressLint("RestrictedApi")
public void stopRecording(){
if(!isRecording)
return;
videoCapture.stopRecording();
cameraProvider.unbind(videoCapture);
isRecording = false;
}
/** Bind Camera Provider */
private void bindCameraProvider() {
// Specify which Camera to use
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, videoCapture);
}
/** Setup Use Cases */
private ImageAnalysis setupImageAnalysis() {
// Configure and create Image Analysis
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
builder.setTargetResolution(IMAGE_RES);
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
builder.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888);
builder.setTargetRotation(Surface.ROTATION_90);
ImageAnalysis imageAnalysis = builder.build();
// Set Analyzer
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> {
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
Image image = imageProxy.getImage();
assert image != null;
// Violation Handling
Mat processed = processImage(imageProxy);
int n = OpenCVHelper.countNonZeroPixels(processed);
int pixelCount = image.getWidth() * image.getHeight();
float percentChanged = (float) n / pixelCount;
// Violation Condition
if (percentChanged * 100 > ALARM_THRESHOLD) {
if (allowReportViolation)
reportViolation("Video", percentChanged);
}
}
imageProxy.close();
});
return imageAnalysis;
}
@SuppressLint("RestrictedApi")
private VideoCapture setupVideoCapture() {
int rotation = getDisplayRotation();
return new VideoCapture.Builder()
.setTargetRotation(rotation)
.build();
}
/** Process Image to be used for Motion Detection */
private Mat processImage(ImageProxy imageProxy){
if (imageProxy == null)
return null;
// Image Transformation
Mat imageMat = OpenCVHelper.extractYChannel(imageProxy);
// Show Input Image
if (inputImageView != null)
OpenCVHelper.debugMat(imageMat, inputImageView);
// Preprocess Image
Mat preprocessed = imageMat;
preprocessed = OpenCVHelper.addGaussianBlur(preprocessed, new Size(21, 21));
preprocessed = OpenCVHelper.addBlur(preprocessed, new Size(3, 3));
// Set Previous Image
if (previousImage == null) {
previousImage = preprocessed;
return null;
}
// Process Image
Mat processed = preprocessed.clone();
processed = OpenCVHelper.thresholdPixels(processed, previousImage, 25);
for(int i = 0; i < DILATE_ITERATIONS; i++)
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
processed = OpenCVHelper.thresholdContourArea(processed, AREA_THRESHOLD);
// Output
previousImage = preprocessed.clone();
// Show Output Image
if (outputImageView != null)
OpenCVHelper.debugMat(processed, outputImageView);
return processed;
}
/** Debug input and result of processing */
public void debugProcessing(@NonNull ImageView inputImageView, @NonNull ImageView outputImageView){
this.inputImageView = inputImageView;
this.outputImageView = outputImageView;
}
/**
private void setPreviewView(@NonNull PreviewView previewView) {
// Create Preview
if (this.preview != null)
this.preview.setSurfaceProvider(previewView.getSurfaceProvider());
}
*/
/** Generate File Name */
private String generateFileName(){
// Get the current timestamp
LocalDateTime currentTime = LocalDateTime.now();
// Define the format for the timestamp
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");
// Return the timestamp as a string
return currentTime.format(formatter);
}
/** Get current Display Rotation */
private int getDisplayRotation() {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
return display.getRotation();
}
/** Start delay until Violation Report is allowed */
private void startViolationTimer(float setupTime) {
new CountDownTimer((long) (START_DELAY), 100) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
allowReportViolation = true;
}
}.start();
}
public void setOutputDir(File outputDir) {
this.outputDir = outputDir;
}
}

View File

@ -1,9 +1,16 @@
package com.example.ueberwachungssystem;
import static android.content.ContentValues.TAG;
import android.annotation.SuppressLint;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -19,80 +26,82 @@ import java.nio.ByteOrder;
@SuppressLint("SetTextI18n")
public class MainActivity extends AppCompatActivity {
TextView tvMessages;
TextView tvConnectionInfos;
WifiCommunication communication;
PermissionRequest permission;
public static String SERVER_IP = "";
public static final int SERVER_PORT = 1234;
//private static final int PERMISSION_REQUEST_CODE = 123;
float i=0;
boolean communicationRunning = true;
String dataRecieved;
TextView textview1;
boolean communicationRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview1 = findViewById(R.id.textView1);
Button button1 = findViewById(R.id.buttonSend);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
communication.sendTrue("Testmessage");
}
});
}
@Override
protected void onResume() {
super.onResume();
if (communicationRunning){
communication = new WifiCommunication(1234);
communication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
@Override
public void onConnection(StringBuffer data) {
//Log.d("Test", data.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
textview1.setText(data);
}
});
}
});
communicationRunning = false;
}
permission = new PermissionRequest(MainActivity.this);
permission.rechtePruefen();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvMessages = findViewById(R.id.tvMessages);
tvConnectionInfos = findViewById(R.id.tvConnectionInfos);
try {
SERVER_IP = getLocalIpAddress();
tvConnectionInfos.setText("Connection Infos: \n Own IP-Adress: " + SERVER_IP+ " Port: " + SERVER_PORT);
} catch (UnknownHostException e) {
e.printStackTrace();
}
Button btnSend = findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
communication.sendTrue(String.valueOf(i));
}
});
protected void onPause() {
super.onPause();
//communication.stopCommunication();
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
PopUpClass popUpClass;
switch (item.getItemId()) {
case R.id.Rechteverwaltung:
popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(tvMessages);
//popUpClass.showPopupWindow(tvMessages);
popUpClass.RechtePrüfen();
return true;
case R.id.Sensoren:
popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(tvMessages);
//popUpClass.showPopupWindow(tvMessages);
popUpClass.Sensoren();
return true;
case R.id.Impressum:
popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(tvMessages);
//popUpClass.showPopupWindow(tvMessages);
popUpClass.Impressum();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void onResume() {
super.onResume();
if (!communicationRunning){
communication = new WifiCommunication(MainActivity.this, SERVER_PORT);
communicationRunning = true;
}
permission = new PermissionRequest(MainActivity.this);
permission.rechtePruefen();
}
@Override
protected void onPause() {
super.onPause();
//communication.stopCommunication();
}
private String getLocalIpAddress() throws UnknownHostException {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
assert wifiManager != null;
@ -101,3 +110,4 @@ public class MainActivity extends AppCompatActivity {
return InetAddress.getByAddress(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(ipInt).array()).getHostAddress();
}
}

View File

@ -0,0 +1,43 @@
package com.example.ueberwachungssystem;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class PermissionHandler {
private final Context context;
private static final int PERMISSION_REQUEST_CODE = 23409;
private static final String[] permissions = new String[]{
android.Manifest.permission.CAMERA,
android.Manifest.permission.RECORD_AUDIO
};
public PermissionHandler(Context context) {
this.context = context;
}
public boolean hasPermissions() {
boolean permissionState = true;
for (String permission: permissions) {
permissionState = permissionState && ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
return permissionState;
}
public void getPermissions() {
if (!hasPermissions())
ActivityCompat.requestPermissions((Activity) context, permissions, PERMISSION_REQUEST_CODE);
}
public void showPermissionToast() {
if (hasPermissions())
Toast.makeText(context, "permissions available", Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "permissions missing", Toast.LENGTH_SHORT).show();
}
}

View File

@ -2,6 +2,12 @@ package com.example.ueberwachungssystem;
import android.annotation.SuppressLint;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.example.ueberwachungssystem.Detection.DetectionReport;
import com.example.ueberwachungssystem.Detection.Detector;
import com.example.ueberwachungssystem.Detection.DetectorService;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
@ -15,7 +21,7 @@ import java.util.Date;
import java.util.Enumeration;
public class WifiCommunication {
private final MainActivity mainActivity;
//private final MainActivity mainActivity;
private final InetAddress address;
private final int port;
@ -23,9 +29,11 @@ public class WifiCommunication {
volatile private boolean send;
private final DatagramSocket socket;
volatile private boolean running;
private OnConnectionListener listener;
@SuppressLint("SetTextI18n")
public WifiCommunication(MainActivity mainActivity, int port) {
this.mainActivity = mainActivity;
public WifiCommunication(int port) {
//this.mainActivity = mainActivity;
this.port = port;
try {
socket = new DatagramSocket(this.port);
@ -38,10 +46,23 @@ public class WifiCommunication {
} catch (SocketException | UnknownHostException e) {
throw new RuntimeException(e);
}
Toast.makeText(mainActivity.getApplicationContext(),"Communication running", Toast.LENGTH_SHORT).show();
//Toast.makeText(mainActivity.getApplicationContext(),"Communication running", Toast.LENGTH_SHORT).show();
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
}
public interface OnConnectionListener {
void onConnection(StringBuffer data);
}
public void setOnConnectionListener(@NonNull OnConnectionListener listener) {
this.listener = listener;
}
public void sendWifiData(StringBuffer wifiMessage) {
if (listener != null) {
listener.onConnection(wifiMessage);
}
}
private class ReceiveThread extends Thread {
private StringBuffer rxStringBuffer = new StringBuffer();
private String rxString="";
@ -54,11 +75,12 @@ public class WifiCommunication {
byte[] receiveData = new byte[512];
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(rxPacket);
rxString = new String(rxPacket.getData());
rxString = new String(receiveData, 0, rxPacket.getLength());
String[] splitrxString = rxString.split(",");
if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) {
rxStringBuffer.append(rxString).append("\n");
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
sendWifiData(rxStringBuffer);
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
previousRxString = rxString;
}
} while (running);
@ -86,7 +108,7 @@ public class WifiCommunication {
DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port);
for(int i = 0; i < 500; i++) {
for(int i = 0; i < 300; i++) {
socket.send(txPacket);
}
}

View File

@ -1,37 +1,53 @@
<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_margin = "16dp"
tools:context = ".MainActivity">
<TextView
android:id = "@+id/tvConnectionInfos"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:inputType = "textMultiLine"
android:hint="ConnectionInfos"
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="top"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnSend"
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/black"/>
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<ImageView
android:id="@+id/inputImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/scrollView"
android:layout_centerHorizontal="true"
android:text="SEND" />
tools:srcCompat="@tools:sample/avatars" />
<ScrollView
android:id="@+id/scrollView"
<ImageView
android:id="@+id/outputImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars" />
<androidx.camera.view.PreviewView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_below="@id/tvConnectionInfos"
android:layout_height="300dp">
<TextView
android:id = "@+id/tvMessages"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint="Messages"
android:inputType = "textMultiLine"
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
</ScrollView>
</RelativeLayout>
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/buttonSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>