Preview entfernt ausgabe des Alarmtext aktuell über activity_main.xml

This commit is contained in:
Michael Pilhoefer 2023-06-11 15:37:16 +02:00
parent 94b763f676
commit de8d4c06aa
3 changed files with 46 additions and 20 deletions

View File

@ -21,21 +21,22 @@ import java.util.concurrent.ExecutionException;
public class KameraAktivitaet extends AppCompatActivity { public class KameraAktivitaet extends AppCompatActivity {
private PreviewView previewView; private PreviewView previewView;
private TextView alarm;
private TextView alarm2;
private boolean isMotionDetected; private boolean isMotionDetected;
private TextView alarm;
private ByteBuffer previousBuffer; private ByteBuffer previousBuffer;
private int previousWidth; private int previousWidth;
private int previousHeight; private int previousHeight;
private boolean camera_alarm = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_kamera2_nebenaktivitaet); setContentView(R.layout.activity_main);
alarm = findViewById(R.id.luminanz); alarm = findViewById(R.id.textView);
alarm2 = findViewById(R.id.orientierung);
previewView = findViewById(R.id.previewView); // PREVIEW //
//previewView = findViewById(R.id.previewView);
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this); final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> { cameraProviderFuture.addListener(() -> {
try { try {
@ -78,12 +79,28 @@ public class KameraAktivitaet extends AppCompatActivity {
if (isMotionDetected) { if (isMotionDetected) {
// Code um den Alarm zu setzen aktuell noch über setText
alarm.setText("ALARM"); alarm.setText("ALARM");
camera_alarm = true;
runOnUiThread(() -> { runOnUiThread(() -> {
// Hier kannst du den Code für die Videoaufzeichnung oder andere Aktionen einfügen
// Beispiel: starteVideoAufzeichnung(); // Platz um die Videoaufzeichnung zu starten
}); });
} else { } else {
// Optional evtl nicht nötig
if (camera_alarm) {
/*
try {
Thread.sleep(2000); // Wartezeit wie lange der Alarm gesetzt sein soll bzw. die Aufzeichnung laufen soll
} catch (InterruptedException e) {
e.printStackTrace();
} */
camera_alarm = false;
}
alarm.setText("OK"); alarm.setText("OK");
} }
} }
@ -92,12 +109,15 @@ public class KameraAktivitaet extends AppCompatActivity {
Preview preview = new Preview.Builder().build(); Preview preview = new Preview.Builder().build();
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build(); CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis, preview);
// PREVIEW //
//preview.setSurfaceProvider(previewView.getSurfaceProvider());
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis, preview);
} }
// Bildverarbeitung zur Bewegungserkennung
private boolean compareFrames(Image currentImage) { private boolean compareFrames(Image currentImage) {
ByteBuffer currentBuffer = currentImage.getPlanes()[0].getBuffer(); ByteBuffer currentBuffer = currentImage.getPlanes()[0].getBuffer();
@ -105,34 +125,28 @@ public class KameraAktivitaet extends AppCompatActivity {
int currentWidth = currentImage.getWidth(); int currentWidth = currentImage.getWidth();
int currentHeight = currentImage.getHeight(); int currentHeight = currentImage.getHeight();
// Überprüfe, ob die Größe der beiden Bilder übereinstimmt
if (previousWidth != currentWidth || previousHeight != currentHeight) { if (previousWidth != currentWidth || previousHeight != currentHeight) {
return false; return false;
} }
// Überprüfe die Pixelwerte für jede Zeile und Spalte
for (int row = 0; row < previousHeight; row++) { for (int row = 0; row < previousHeight; row++) {
for (int col = 0; col < previousWidth; col++) { for (int col = 0; col < previousWidth; col++) {
// Berechne den Index des aktuellen Pixels im ByteBuffer
int previousIndex = row * previousWidth + col; int previousIndex = row * previousWidth + col;
int currentIndex = row * currentWidth + col; int currentIndex = row * currentWidth + col;
// Lese die Pixelwerte für den aktuellen Pixel
int previousPixel = previousBuffer.get(previousIndex) & 0xFF; int previousPixel = previousBuffer.get(previousIndex) & 0xFF;
int currentPixel = currentBuffer.get(currentIndex) & 0xFF; int currentPixel = currentBuffer.get(currentIndex) & 0xFF;
// Vergleiche die Pixelwerte und prüfe auf einen signifikanten Unterschied
int pixelDifference = Math.abs(previousPixel - currentPixel); int pixelDifference = Math.abs(previousPixel - currentPixel);
int threshold = 120; int threshold = 120;
if (pixelDifference > threshold) { if (pixelDifference > threshold) {
String text = String.valueOf(pixelDifference); String text = String.valueOf(pixelDifference);
alarm2.setText(text);
return true; return true;
} }
} }
} }
return false; return false;
} }
} }

View File

@ -21,7 +21,7 @@
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
android:id="@+id/luminanz" android:id="@+id/alarm_test"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1" android:layout_weight="1"
@ -31,7 +31,7 @@
android:textSize="100sp" android:textSize="100sp"
android:textColor="#9999ff"/> android:textColor="#9999ff"/>
<TextView <TextView
android:id="@+id/orientierung" android:id="@+id/threshold_test"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1" android:layout_weight="1"

View File

@ -18,5 +18,17 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.931" /> app:layout_constraintVertical_bias="0.931" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.247" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>