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

package com.example.greenwatch;

public class Kamera {
}

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

import android.media.Image; import android.media.Image;
import android.os.Bundle; import android.os.Bundle;
import android.util.Size; import android.util.Size;
import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector; import androidx.camera.core.CameraSelector;


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


import java.nio.ByteBuffer;
import java.util.concurrent.ExecutionException; 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 ByteBuffer previousBuffer;
private int previousWidth;
private int previousHeight;


@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_demo_kamera2_nebenaktivitaet);
alarm = findViewById(R.id.luminanz);
alarm2 = findViewById(R.id.orientierung);
previewView = findViewById(R.id.previewView); previewView = findViewById(R.id.previewView);
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this); final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> { cameraProviderFuture.addListener(() -> {
imageAnalysis.setAnalyzer( imageAnalysis.setAnalyzer(
ContextCompat.getMainExecutor(this), ContextCompat.getMainExecutor(this),
imageProxy -> { imageProxy -> {

int imageFormat = imageProxy.getFormat(); int imageFormat = imageProxy.getFormat();

if (imageFormat == ImageFormat.YUV_420_888) { 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(); imageProxy.close();
}); });
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()); preview.setSurfaceProvider(previewView.getSurfaceProvider());
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis, preview); 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

android:orientation="vertical"> android:orientation="vertical">


<TextView <TextView
android:id="@+id/alarm"
android:id="@+id/luminanz"
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:text="" />

android:layout_gravity="center"
android:gravity="center"
android:text=""
android:textSize="100sp"
android:textColor="#9999ff"/>
<TextView <TextView
android:id="@+id/luminanz"
android:id="@+id/orientierung"
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:layout_gravity="center" android:layout_gravity="center"
android:gravity="center" android:gravity="center"
android:text=""
android:textSize="100sp" android:textSize="100sp"
android:textColor="#9999ff"/> android:textColor="#9999ff"/>
</LinearLayout> </LinearLayout>

Loading…
Cancel
Save