Browse Source

Kamera implementiert und Preview im Interface

Zusätzlich eintragen der Berechtigungen und der extra Klasse KameraAktivitaet im Manifest eingetragen
Kamera
Michael Pilhoefer 1 year ago
parent
commit
94b763f676

+ 0
- 4
app/src/main/java/com/example/greenwatch/Kamera.java View File

@@ -1,4 +0,0 @@
package com.example.greenwatch;

public class Kamera {
}

+ 81
- 1
app/src/main/java/com/example/greenwatch/KameraAktivitaet.java View File

@@ -4,6 +4,7 @@ import android.graphics.ImageFormat;
import android.media.Image;
import android.os.Bundle;
import android.util.Size;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
@@ -15,15 +16,25 @@ import androidx.core.content.ContextCompat;

import com.google.common.util.concurrent.ListenableFuture;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutionException;

public class KameraAktivitaet extends AppCompatActivity {
private PreviewView previewView;
private TextView alarm;
private TextView alarm2;
private boolean isMotionDetected;

private ByteBuffer previousBuffer;
private int previousWidth;
private int previousHeight;

@Override
protected void onCreate(Bundle 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);
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
@@ -43,9 +54,38 @@ public class KameraAktivitaet extends AppCompatActivity {
imageAnalysis.setAnalyzer(
ContextCompat.getMainExecutor(this),
imageProxy -> {

int imageFormat = imageProxy.getFormat();

if (imageFormat == ImageFormat.YUV_420_888) {
Image image = imageProxy.getImage();

Image currentImage = imageProxy.getImage();

if (previousHeight != 0) {
assert currentImage != null;
isMotionDetected = compareFrames(currentImage);
}

assert currentImage != null;
Image.Plane[] planes = currentImage.getPlanes();
ByteBuffer buffer = planes[0].getBuffer().duplicate();
previousBuffer = ByteBuffer.allocateDirect(buffer.remaining());
previousBuffer.put(buffer);
previousWidth = currentImage.getWidth();
previousHeight = currentImage.getHeight();

currentImage.close();

if (isMotionDetected) {

alarm.setText("ALARM");
runOnUiThread(() -> {
// Hier kannst du den Code für die Videoaufzeichnung oder andere Aktionen einfügen
// Beispiel: starteVideoAufzeichnung();
});
} else {
alarm.setText("OK");
}
}
imageProxy.close();
});
@@ -54,6 +94,46 @@ public class KameraAktivitaet extends AppCompatActivity {
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis, preview);


}

private boolean compareFrames(Image currentImage) {

ByteBuffer currentBuffer = currentImage.getPlanes()[0].getBuffer();

int currentWidth = currentImage.getWidth();
int currentHeight = currentImage.getHeight();

// Überprüfe, ob die Größe der beiden Bilder übereinstimmt
if (previousWidth != currentWidth || previousHeight != currentHeight) {
return false;
}

// Überprüfe die Pixelwerte für jede Zeile und Spalte
for (int row = 0; row < previousHeight; row++) {
for (int col = 0; col < previousWidth; col++) {
// Berechne den Index des aktuellen Pixels im ByteBuffer
int previousIndex = row * previousWidth + col;
int currentIndex = row * currentWidth + col;

// Lese die Pixelwerte für den aktuellen Pixel
int previousPixel = previousBuffer.get(previousIndex) & 0xFF;
int currentPixel = currentBuffer.get(currentIndex) & 0xFF;

// Vergleiche die Pixelwerte und prüfe auf einen signifikanten Unterschied
int pixelDifference = Math.abs(previousPixel - currentPixel);
int threshold = 120;

if (pixelDifference > threshold) {
String text = String.valueOf(pixelDifference);
alarm2.setText(text);
return true;
}
}
}

return false;
}
}


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

@@ -21,19 +21,23 @@
android:orientation="vertical">

<TextView
android:id="@+id/alarm"
android:id="@+id/luminanz"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="" />

android:layout_gravity="center"
android:gravity="center"
android:text=""
android:textSize="100sp"
android:textColor="#9999ff"/>
<TextView
android:id="@+id/luminanz"
android:id="@+id/orientierung"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:text=""
android:textSize="100sp"
android:textColor="#9999ff"/>
</LinearLayout>

Loading…
Cancel
Save