Browse Source

Preview entfernt ausgabe des Alarmtext aktuell über activity_main.xml

Kamera
Michael Pilhoefer 1 year ago
parent
commit
de8d4c06aa

+ 32
- 18
app/src/main/java/com/example/greenwatch/KameraAktivitaet.java View File



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);
alarm = findViewById(R.id.luminanz);
alarm2 = findViewById(R.id.orientierung);
previewView = findViewById(R.id.previewView);
setContentView(R.layout.activity_main);
alarm = findViewById(R.id.textView);

// 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 {


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");
} }
} }


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();
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;
} }
} }

+ 2
- 2
app/src/main/res/layout/activity_demo_kamera2_nebenaktivitaet.xml View File

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"
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"

+ 12
- 0
app/src/main/res/layout/activity_main.xml View File

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>

Loading…
Cancel
Save