|
|
@@ -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; |
|
|
|
} |
|
|
|
} |
|
|
|
|