package com.example.ueberwachungssystem; | |||||
package com.example.ueberwachungssystem.Detection; | |||||
import android.util.Log; | import android.util.Log; | ||||
package com.example.ueberwachungssystem; | |||||
package com.example.ueberwachungssystem.Detection; | |||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
package com.example.ueberwachungssystem.VideoDetection; | |||||
package com.example.ueberwachungssystem.Detection; | |||||
import android.content.Context; | import android.content.Context; | ||||
import android.graphics.ImageFormat; | import android.graphics.ImageFormat; | ||||
import androidx.core.content.ContextCompat; | import androidx.core.content.ContextCompat; | ||||
import androidx.lifecycle.LifecycleOwner; | import androidx.lifecycle.LifecycleOwner; | ||||
import com.example.ueberwachungssystem.Detector; | |||||
import com.google.common.util.concurrent.ListenableFuture; | import com.google.common.util.concurrent.ListenableFuture; | ||||
import java.nio.ByteBuffer; | import java.nio.ByteBuffer; | ||||
private PreviewView previewView = null; | private PreviewView previewView = null; | ||||
// Detect Violation | // Detect Violation | ||||
private static final float PIXEL_THRESHOLD = 30f; // Luminosity (brightness channel of YUV_420_888) | private static final float PIXEL_THRESHOLD = 30f; // Luminosity (brightness channel of YUV_420_888) | ||||
private static final float ALARM_THRESHOLD = 1f; // Percent of pixels changed | |||||
private static final float ALARM_THRESHOLD = 0.2f; // Percent of pixels changed | |||||
private ByteBuffer previousBuffer = null; | private ByteBuffer previousBuffer = null; | ||||
package com.example.ueberwachungssystem; | package com.example.ueberwachungssystem; | ||||
import androidx.annotation.NonNull; | |||||
import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||
import androidx.camera.view.PreviewView; | |||||
import androidx.core.app.ActivityCompat; | |||||
import androidx.core.content.ContextCompat; | |||||
import androidx.camera.core.ExperimentalGetImage; | |||||
import android.Manifest; | |||||
import android.content.pm.PackageManager; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.view.View; | |||||
import android.widget.TextView; | |||||
import android.widget.Toast; | |||||
import android.widget.ToggleButton; | |||||
import com.example.ueberwachungssystem.VideoDetection.VideoDetector; | |||||
@ExperimentalGetImage | |||||
public class MainActivity extends AppCompatActivity { | public class MainActivity extends AppCompatActivity { | ||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101; | |||||
@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); | ||||
TextView textView = findViewById(R.id.textView); | |||||
PreviewView previewView = findViewById(R.id.previewView); | |||||
VideoDetector vd = new VideoDetector(this); | |||||
vd.setPreviewView(previewView); | |||||
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() { | |||||
@Override | |||||
public void onDetection(@NonNull DetectionReport detectionReport) { | |||||
detectionReport.log("OnDetection"); | |||||
textView.setText(detectionReport.toString()); | |||||
} | |||||
}); | |||||
//vd.startDetection(); | |||||
ToggleButton toggleButton = findViewById(R.id.previewButton); | |||||
toggleButton.setOnClickListener(new View.OnClickListener() { | |||||
@Override | |||||
public void onClick(View v) { | |||||
getCameraAccess(); | |||||
if (isCameraAccessAllowed() && toggleButton.isChecked()) | |||||
{ | |||||
vd.startDetection(); | |||||
} | |||||
else { | |||||
vd.stopDetection(); | |||||
textView.setText(""); | |||||
} | |||||
} | |||||
}); | |||||
} | |||||
private boolean isCameraAccessAllowed() { | |||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED; | |||||
} | |||||
private void getCameraAccess() { | |||||
if (!isCameraAccessAllowed()) | |||||
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE); | |||||
} | |||||
@Override | |||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |||||
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0) { | |||||
boolean cameraRights = grantResults[0] == PackageManager.PERMISSION_GRANTED; | |||||
if (cameraRights) { | |||||
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show(); | |||||
} else { | |||||
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show(); | |||||
} | |||||
} | |||||
} | } | ||||
} | } |
<?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||||
xmlns:app="http://schemas.android.com/apk/res-auto" | 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_gravity="center" | |||||
android:gravity="center" | |||||
android:orientation="vertical" | |||||
tools:context=".MainActivity"> | tools:context=".MainActivity"> | ||||
<TextView | <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="" | |||||
android:textSize="20dp" | |||||
android:text="Hello World!" | |||||
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" /> | ||||
<ToggleButton | |||||
android:id="@+id/previewButton" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="wrap_content" | |||||
android:text="ToggleButton" /> | |||||
<androidx.camera.view.PreviewView | |||||
android:id="@+id/previewView" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="match_parent" | |||||
android:backgroundTint="@android:color/black"/> | |||||
</LinearLayout> | |||||
</androidx.constraintlayout.widget.ConstraintLayout> |