Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee61f7042b | ||
|
|
ab54b6d96a | ||
|
|
a85fb1f5ca | ||
|
|
e007ad1744 | ||
|
|
292c3457fa | ||
|
|
79c7c16d21 | ||
|
|
0782bbdd04 | ||
|
|
e89a5d11a1 | ||
|
|
b246ad8624 | ||
|
|
192f88ede5 | ||
|
|
3c1366bd10 | ||
|
|
35583a4cdd | ||
|
|
d22952d83b |
No files matched your search
Whitespace-only changes.
+4
-9
@@ -26,6 +26,9 @@ android {
|
|||||||
sourceCompatibility JavaVersion.VERSION_1_8
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
targetCompatibility JavaVersion.VERSION_1_8
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
}
|
}
|
||||||
|
buildFeatures {
|
||||||
|
viewBinding true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -33,16 +36,8 @@ dependencies {
|
|||||||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||||
implementation 'com.google.android.material:material:1.8.0'
|
implementation 'com.google.android.material:material:1.8.0'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||||
|
implementation 'com.google.android.gms:play-services-nearby:18.0.0'
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||||
|
|
||||||
// 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}"
|
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,11 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<uses-feature android:name="android.hardware.camera"/>
|
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||||
<uses-permission android:name="android.permission.CAMERA"/>
|
<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
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
@@ -11,12 +14,14 @@
|
|||||||
android:fullBackupContent="@xml/backup_rules"
|
android:fullBackupContent="@xml/backup_rules"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.Ueberwachungssystem"
|
android:theme="@style/Theme.Ueberwachungssystem"
|
||||||
tools:targetApi="31">
|
tools:targetApi="31">
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true">
|
android:exported="true"
|
||||||
|
android:label="Ueberwachungsapp">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
|
|||||||
@@ -1,126 +1,103 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import android.annotation.SuppressLint;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import android.net.wifi.WifiInfo;
|
||||||
import androidx.camera.core.Camera;
|
import android.net.wifi.WifiManager;
|
||||||
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.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
@SuppressLint("SetTextI18n")
|
||||||
|
|
||||||
@ExperimentalGetImage
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
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;
|
||||||
|
|
||||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
|
boolean communicationRunning = false;
|
||||||
private PreviewView previewView;
|
|
||||||
private TextView textView;
|
|
||||||
private Button previewButton;
|
|
||||||
private Boolean cameraPreviewIsRunning = false;
|
|
||||||
private Boolean isPressed = false;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
tvMessages = findViewById(R.id.tvMessages);
|
||||||
textView = findViewById(R.id.textView);
|
tvConnectionInfos = findViewById(R.id.tvConnectionInfos);
|
||||||
previewView = findViewById(R.id.previewView);
|
try {
|
||||||
previewButton = findViewById(R.id.previewButton);
|
SERVER_IP = getLocalIpAddress();
|
||||||
|
tvConnectionInfos.setText("Connection Infos: \n Own IP-Adress: " + SERVER_IP+ " Port: " + SERVER_PORT);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
VideoDetector vd = new VideoDetector(this);
|
e.printStackTrace();
|
||||||
vd.setPreviewView(previewView);
|
}
|
||||||
vd.startDetection();
|
Button btnSend = findViewById(R.id.btnSend);
|
||||||
|
btnSend.setOnClickListener(new View.OnClickListener() {
|
||||||
previewButton.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
getCameraAccess();
|
i++;
|
||||||
if (isCameraAccessAllowed() && !isPressed)
|
communication.sendTrue(String.valueOf(i));
|
||||||
{
|
|
||||||
vd.stopDetection();
|
|
||||||
isPressed = true;
|
|
||||||
}
|
|
||||||
else if (isCameraAccessAllowed() && isPressed) {
|
|
||||||
vd.startDetection();
|
|
||||||
isPressed = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
private boolean isCameraAccessAllowed() {
|
getMenuInflater().inflate(R.menu.options_menu, menu);
|
||||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
|
return true;
|
||||||
}
|
}
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
private void getCameraAccess() {
|
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
|
||||||
if (!isCameraAccessAllowed())
|
PopUpClass popUpClass;
|
||||||
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
|
switch (item.getItemId()) {
|
||||||
}
|
case R.id.Rechteverwaltung:
|
||||||
|
popUpClass = new PopUpClass(MainActivity.this);
|
||||||
@Override
|
popUpClass.showPopupWindow(tvMessages);
|
||||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
popUpClass.RechtePrüfen();
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
return true;
|
||||||
|
case R.id.Sensoren:
|
||||||
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0) {
|
popUpClass = new PopUpClass(MainActivity.this);
|
||||||
boolean cameraRights = grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
popUpClass.showPopupWindow(tvMessages);
|
||||||
if (cameraRights) {
|
popUpClass.Sensoren();
|
||||||
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
|
return true;
|
||||||
} else {
|
case R.id.Impressum:
|
||||||
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
|
popUpClass = new PopUpClass(MainActivity.this);
|
||||||
}
|
popUpClass.showPopupWindow(tvMessages);
|
||||||
|
popUpClass.Impressum();
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void onResume() {
|
||||||
private void previewCamera() {
|
super.onResume();
|
||||||
// Request Camera Access
|
if (!communicationRunning){
|
||||||
getCameraAccess();
|
communication = new WifiCommunication(MainActivity.this, SERVER_PORT);
|
||||||
// Return when Camera Access not allowed or Camera Preview is running
|
communicationRunning = true;
|
||||||
if (!isCameraAccessAllowed() || cameraPreviewIsRunning)
|
}
|
||||||
return;
|
permission = new PermissionRequest(MainActivity.this);
|
||||||
// Camera Preview is running
|
permission.rechtePruefen();
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
private void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
|
protected void onPause() {
|
||||||
// Create Preview
|
super.onPause();
|
||||||
Preview preview = new Preview.Builder().build();
|
communication.stopCommunication();
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
}
|
private String getLocalIpAddress() throws UnknownHostException {
|
||||||
|
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
|
||||||
|
assert wifiManager != null;
|
||||||
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
||||||
|
int ipInt = wifiInfo.getIpAddress();
|
||||||
|
return InetAddress.getByAddress(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(ipInt).array()).getHostAddress();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.example.ueberwachungssystem;
|
||||||
|
|
||||||
|
import static android.Manifest.permission.INTERNET;
|
||||||
|
import static android.Manifest.permission.CAMERA;
|
||||||
|
import static android.Manifest.permission.RECORD_AUDIO;
|
||||||
|
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
public class PermissionRequest extends AppCompatActivity{
|
||||||
|
private static final int PERMISSION_REQUEST_CODE = 123;
|
||||||
|
private final MainActivity mainActivity;
|
||||||
|
|
||||||
|
Handler handler = new Handler();
|
||||||
|
|
||||||
|
public PermissionRequest(MainActivity mainActivity) {
|
||||||
|
this.mainActivity = mainActivity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringBuilder rechtePruefen() {
|
||||||
|
boolean rechtKamera = ContextCompat.checkSelfPermission(mainActivity, CAMERA) == PackageManager.PERMISSION_GRANTED;
|
||||||
|
boolean rechtMikrofon = ContextCompat.checkSelfPermission(mainActivity, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
|
||||||
|
boolean rechtInternet = ContextCompat.checkSelfPermission(mainActivity, INTERNET) == PackageManager.PERMISSION_GRANTED;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("Rechte prüfen:")
|
||||||
|
.append("\nKamera: ").append(rechtKamera)
|
||||||
|
.append("\nMikrofon: ").append(rechtMikrofon)
|
||||||
|
.append("\nInternet: ").append(rechtInternet);
|
||||||
|
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(sb));
|
||||||
|
if (!(rechtKamera && rechtMikrofon && rechtInternet)){
|
||||||
|
handler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Toast.makeText(mainActivity.getApplicationContext(),"Es werden Rechte benötigt", Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return sb;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void rechteAnfordern() {
|
||||||
|
mainActivity.requestPermissions(new String[]{CAMERA, RECORD_AUDIO, INTERNET}, PERMISSION_REQUEST_CODE);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package com.example.ueberwachungssystem;
|
||||||
|
|
||||||
|
import android.view.Gravity;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.MotionEvent;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.LinearLayout;
|
||||||
|
import android.widget.PopupWindow;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
public class PopUpClass {
|
||||||
|
private final MainActivity mainActivity;
|
||||||
|
|
||||||
|
PermissionRequest permission;
|
||||||
|
TextView PopUpText;
|
||||||
|
|
||||||
|
|
||||||
|
public PopUpClass(MainActivity mainActivity) {
|
||||||
|
this.mainActivity = mainActivity;
|
||||||
|
permission = new PermissionRequest(mainActivity);
|
||||||
|
}
|
||||||
|
|
||||||
|
//PopupWindow display method
|
||||||
|
|
||||||
|
public void showPopupWindow(final View view) {
|
||||||
|
|
||||||
|
|
||||||
|
//Create a View object yourself through inflater
|
||||||
|
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
|
||||||
|
View popupView = inflater.inflate(R.layout.popup_window, null);
|
||||||
|
|
||||||
|
//Specify the length and width through constants
|
||||||
|
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
|
||||||
|
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
|
||||||
|
|
||||||
|
//Make Inactive Items Outside Of PopupWindow
|
||||||
|
boolean focusable = true;
|
||||||
|
|
||||||
|
//Create a window with our parameters
|
||||||
|
final PopupWindow popupWindow = new PopupWindow(popupView,width*1, height*1, focusable);
|
||||||
|
|
||||||
|
//Set the location of the window on the screen
|
||||||
|
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
|
||||||
|
|
||||||
|
//Initialize the elements of our window, install the handler
|
||||||
|
|
||||||
|
PopUpText = popupView.findViewById(R.id.titleText);
|
||||||
|
|
||||||
|
Button buttonEdit = popupView.findViewById(R.id.RechteAnfordern);
|
||||||
|
buttonEdit.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
RechteAnfordern();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//Handler for clicking on the inactive zone of the window
|
||||||
|
|
||||||
|
popupView.setOnTouchListener(new View.OnTouchListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
|
|
||||||
|
//Close the window when clicked
|
||||||
|
popupWindow.dismiss();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
public void RechtePrüfen(){
|
||||||
|
StringBuilder Text = permission.rechtePruefen();
|
||||||
|
PopUpText.setText(Text);
|
||||||
|
}
|
||||||
|
public void RechteAnfordern(){
|
||||||
|
permission.rechteAnfordern();
|
||||||
|
StringBuilder Text = permission.rechtePruefen();
|
||||||
|
PopUpText.setText(Text);
|
||||||
|
}
|
||||||
|
public void Sensoren(){
|
||||||
|
PopUpText.setText("Es können 3 verschiedene Sensoren verwendet werden \n -1. Beschleunigungssensor\n -2. Mikrofon\n -3. Kamera");
|
||||||
|
}
|
||||||
|
public void Impressum(){
|
||||||
|
PopUpText.setText("Die Ueberwachungsapp wurde im Rahmen eines Praktikums der TH-Nürnberg programmiert");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,154 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.ImageFormat;
|
|
||||||
import android.media.Image;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.util.Size;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
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.lifecycle.ProcessCameraProvider;
|
|
||||||
import androidx.camera.view.PreviewView;
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
import androidx.lifecycle.LifecycleOwner;
|
|
||||||
|
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
@ExperimentalGetImage
|
|
||||||
public class VideoDetector {
|
|
||||||
// Calling Activity
|
|
||||||
private final Context context;
|
|
||||||
// Camera Provider
|
|
||||||
private ProcessCameraProvider cameraProvider;
|
|
||||||
private Boolean isDetectionRunning = false;
|
|
||||||
// Preview Camera Image
|
|
||||||
private PreviewView previewView = null;
|
|
||||||
// Check Violation
|
|
||||||
private final float DELTA_LUMINOSITY_THRESHOLD = 10f;
|
|
||||||
private float lastLuminosity = 0f;
|
|
||||||
private Boolean isViolated = false;
|
|
||||||
|
|
||||||
|
|
||||||
/** Constructor */
|
|
||||||
public VideoDetector(Context context) { this.context = context; }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Return State of Video Detector */
|
|
||||||
public Boolean isRunning() { return isDetectionRunning; }
|
|
||||||
|
|
||||||
/** Return the status of Violation */
|
|
||||||
public boolean getViolationStatus()
|
|
||||||
{
|
|
||||||
return isViolated;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Starts Video Detection */
|
|
||||||
public void startDetection() {
|
|
||||||
isDetectionRunning = true;
|
|
||||||
// Request Camera Provider
|
|
||||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
|
||||||
//Check for Camera availability
|
|
||||||
cameraProviderFuture.addListener(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
cameraProvider = cameraProviderFuture.get();
|
|
||||||
bindLuminosityAnalysis(cameraProvider);
|
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
|
||||||
// No errors need to be handled for this Future. This should never be reached.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},ContextCompat.getMainExecutor(context));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Stops Video Detection */
|
|
||||||
public void stopDetection() {
|
|
||||||
cameraProvider.unbindAll();
|
|
||||||
isDetectionRunning = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Set PreviewView to show Image */
|
|
||||||
public void setPreviewView(PreviewView previewView) {
|
|
||||||
this.previewView = previewView;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
|
||||||
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
|
||||||
// Configure and create Image Analysis
|
|
||||||
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
|
||||||
builder.setTargetResolution(new Size(640, 480));
|
|
||||||
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
|
||||||
ImageAnalysis imageAnalysis = builder.build();
|
|
||||||
|
|
||||||
// Set Analyzer
|
|
||||||
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), new ImageAnalysis.Analyzer() {
|
|
||||||
@Override
|
|
||||||
public void analyze(@NonNull ImageProxy imageProxy) {
|
|
||||||
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
|
||||||
Image image = imageProxy.getImage();
|
|
||||||
assert image != null;
|
|
||||||
|
|
||||||
// Analyze frame
|
|
||||||
float luminosity = calculateLuminosity(image);
|
|
||||||
Log.d("Video Detector", String.valueOf(luminosity));
|
|
||||||
if (luminosity != 0)
|
|
||||||
checkForViolation(luminosity, lastLuminosity);
|
|
||||||
lastLuminosity = luminosity;
|
|
||||||
}
|
|
||||||
imageProxy.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// Create Preview
|
|
||||||
Preview preview = new Preview.Builder().build();
|
|
||||||
// Specify which Camera to use
|
|
||||||
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
|
||||||
// Update PreviewView if set
|
|
||||||
if (previewView != null)
|
|
||||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
|
||||||
|
|
||||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Return Luminosity from Image */
|
|
||||||
private float calculateLuminosity (Image image) {
|
|
||||||
int width = image.getWidth();
|
|
||||||
int height = image.getHeight();
|
|
||||||
|
|
||||||
Image.Plane[] planes = image.getPlanes();
|
|
||||||
ByteBuffer luminosityBuffer = planes[0].getBuffer();
|
|
||||||
|
|
||||||
int yRowStride = image.getPlanes()[0].getRowStride();
|
|
||||||
int yPixelStride = image.getPlanes()[0].getPixelStride();
|
|
||||||
|
|
||||||
int luminosity;
|
|
||||||
float sum = 0;
|
|
||||||
|
|
||||||
for (int y = 0; y < height; ++y) {
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
int index = (y * yRowStride) + (x * yPixelStride);
|
|
||||||
luminosity = (luminosityBuffer.get(index) & 0xff);
|
|
||||||
sum += luminosity;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sum / (width * height);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Check if delta Luminosity exceeds threshold */
|
|
||||||
private void checkForViolation(float luminosity, float previousLuminosity) {
|
|
||||||
float deltaLuminosity = Math.abs(luminosity - previousLuminosity);
|
|
||||||
if (deltaLuminosity > DELTA_LUMINOSITY_THRESHOLD) {
|
|
||||||
isViolated = true;
|
|
||||||
Log.d("Violation", "Violation");
|
|
||||||
}
|
|
||||||
Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
package com.example.ueberwachungssystem;
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.Inet4Address;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.NetworkInterface;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
|
||||||
|
public class WifiCommunication {
|
||||||
|
private final MainActivity mainActivity;
|
||||||
|
private final InetAddress address;
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
private String messageToSend;
|
||||||
|
volatile private boolean send;
|
||||||
|
private final DatagramSocket socket;
|
||||||
|
volatile private boolean running;
|
||||||
|
@SuppressLint("SetTextI18n")
|
||||||
|
public WifiCommunication(MainActivity mainActivity, int port) {
|
||||||
|
this.mainActivity = mainActivity;
|
||||||
|
this.port = port;
|
||||||
|
try {
|
||||||
|
socket = new DatagramSocket(this.port);
|
||||||
|
socket.setBroadcast(true);
|
||||||
|
address = InetAddress.getByName("255.255.255.255"); //100.82.255.255
|
||||||
|
running = true;
|
||||||
|
send = false;
|
||||||
|
new ReceiveThread().start();
|
||||||
|
new SendThread().start();
|
||||||
|
} catch (SocketException | UnknownHostException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
Toast.makeText(mainActivity.getApplicationContext(),"Communication running", Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
|
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
|
||||||
|
}
|
||||||
|
private class ReceiveThread extends Thread {
|
||||||
|
private StringBuffer rxStringBuffer = new StringBuffer();
|
||||||
|
private String rxString="";
|
||||||
|
private String previousRxString = "";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
do {
|
||||||
|
byte[] receiveData = new byte[512];
|
||||||
|
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
|
||||||
|
socket.receive(rxPacket);
|
||||||
|
rxString = new String(rxPacket.getData());
|
||||||
|
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));
|
||||||
|
previousRxString = rxString;
|
||||||
|
}
|
||||||
|
} while (running);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private class SendThread extends Thread {
|
||||||
|
private int tmpCnt = 0;
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
do {
|
||||||
|
if(send)
|
||||||
|
{
|
||||||
|
send = false;
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||||
|
Date curDate = new Date(System.currentTimeMillis());
|
||||||
|
String str = formatter.format(curDate);
|
||||||
|
byte[] send_Data = new byte[512];
|
||||||
|
String txString = ("1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend);
|
||||||
|
send_Data = txString.getBytes();
|
||||||
|
|
||||||
|
DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port);
|
||||||
|
|
||||||
|
for(int i = 0; i < 500; i++) {
|
||||||
|
socket.send(txPacket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (running);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static String getLocalIpAddress() {
|
||||||
|
try {
|
||||||
|
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
|
||||||
|
NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
|
||||||
|
for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
|
||||||
|
InetAddress inetAddress = addresses.nextElement();
|
||||||
|
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
|
||||||
|
return inetAddress.getHostAddress();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SocketException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendTrue(String message){
|
||||||
|
send = true;
|
||||||
|
messageToSend = message;
|
||||||
|
}
|
||||||
|
public void stopCommunication() {
|
||||||
|
running = false;
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,35 +1,37 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version = "1.0" encoding = "utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<RelativeLayout 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"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
android:layout_width = "match_parent"
|
||||||
android:layout_width="match_parent"
|
android:layout_height = "match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_margin = "16dp"
|
||||||
android:layout_gravity="center"
|
tools:context = ".MainActivity">
|
||||||
android:gravity="center"
|
|
||||||
android:orientation="vertical"
|
|
||||||
tools:context=".MainActivity">
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/textView"
|
android:id = "@+id/tvConnectionInfos"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width = "match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height = "wrap_content"
|
||||||
android:text="Hello World!"
|
android:inputType = "textMultiLine"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
android:hint="ConnectionInfos"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/previewButton"
|
android:id="@+id/btnSend"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Button"
|
android:layout_below="@id/scrollView"
|
||||||
tools:layout_editor_absoluteX="156dp"
|
android:layout_centerHorizontal="true"
|
||||||
tools:layout_editor_absoluteY="189dp" />
|
android:text="SEND" />
|
||||||
|
|
||||||
<androidx.camera.view.PreviewView
|
<ScrollView
|
||||||
android:id="@+id/previewView"
|
android:id="@+id/scrollView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent" />
|
android:layout_below="@id/tvConnectionInfos"
|
||||||
|
android:layout_height="300dp">
|
||||||
</LinearLayout>
|
<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>
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?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"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingLeft="10dp"
|
||||||
|
android:paddingRight="10dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:background="#A5ACB2">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/titleText"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textSize="20sp"
|
||||||
|
android:textStyle="italic"
|
||||||
|
android:padding="10dp"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/RechteAnfordern"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="Rechte Anfordern" />
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||||
|
<item android:id="@+id/Rechteverwaltung"
|
||||||
|
android:title="Rechteverwaltung" />
|
||||||
|
<item android:id="@+id/Sensoren"
|
||||||
|
android:title="Sensoren" />
|
||||||
|
<item android:id="@+id/Impressum"
|
||||||
|
android:title="Impressum" />
|
||||||
|
</menu>
|
||||||
@@ -14,3 +14,4 @@ dependencyResolutionManagement {
|
|||||||
}
|
}
|
||||||
rootProject.name = "Ueberwachungssystem"
|
rootProject.name = "Ueberwachungssystem"
|
||||||
include ':app'
|
include ':app'
|
||||||
|
include ':app'
|
||||||
Reference in new issue
Block a user