|
|
@@ -3,6 +3,8 @@ package com.example.greenwatch; |
|
|
|
import android.graphics.ImageFormat; |
|
|
|
import android.media.Image; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Handler; |
|
|
|
import android.util.Log; |
|
|
|
import android.util.Size; |
|
|
|
|
|
|
|
import android.widget.TextView; |
|
|
@@ -27,9 +29,22 @@ public class KameraAktivitaet extends AppCompatActivity { |
|
|
|
private boolean isMotionDetected; |
|
|
|
private boolean camera_alarm; |
|
|
|
private TextView alarm; |
|
|
|
private TextView test; |
|
|
|
|
|
|
|
|
|
|
|
// Bildverarbeitung Variablen |
|
|
|
private ByteBuffer previousBuffer; |
|
|
|
private int previousWidth; |
|
|
|
private int previousHeight; |
|
|
|
private int threshold = 50; |
|
|
|
private int startX; |
|
|
|
private int startY; |
|
|
|
private int endX; |
|
|
|
private int endY; |
|
|
|
|
|
|
|
private static final long ALARM_RESET_DELAY = 5000; |
|
|
|
private boolean isAlarmSet = false; |
|
|
|
private Handler alarmResetHandler = new Handler(); |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
@@ -38,6 +53,7 @@ public class KameraAktivitaet extends AppCompatActivity { |
|
|
|
|
|
|
|
// NUR FÜR TESTS |
|
|
|
alarm = findViewById(R.id.textView); |
|
|
|
test = findViewById(R.id.textView2); |
|
|
|
|
|
|
|
// PREVIEW // |
|
|
|
// previewView = findViewById(R.id.previewView); |
|
|
@@ -85,9 +101,8 @@ public class KameraAktivitaet extends AppCompatActivity { |
|
|
|
if (isMotionDetected) { |
|
|
|
alarm.setText("ALARM"); |
|
|
|
camera_alarm = true; |
|
|
|
} else { |
|
|
|
camera_alarm = false; |
|
|
|
alarm.setText("OK"); |
|
|
|
|
|
|
|
alarmResetHandler.postDelayed(alarmResetRunnable, ALARM_RESET_DELAY); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
@@ -108,35 +123,87 @@ public class KameraAktivitaet extends AppCompatActivity { |
|
|
|
|
|
|
|
// Bildverarbeitung zur Bewegungserkennung |
|
|
|
private boolean compareFrames(Image currentImage) { |
|
|
|
|
|
|
|
ByteBuffer currentBuffer = currentImage.getPlanes()[0].getBuffer(); |
|
|
|
|
|
|
|
Image.Plane[] planes = currentImage.getPlanes(); |
|
|
|
ByteBuffer currentBuffer = planes[0].getBuffer(); |
|
|
|
int currentWidth = currentImage.getWidth(); |
|
|
|
int currentHeight = currentImage.getHeight(); |
|
|
|
int yRowStride = planes[0].getRowStride(); |
|
|
|
int yPixelStride = planes[0].getPixelStride(); |
|
|
|
|
|
|
|
|
|
|
|
//System.out.println("Höhe: " + currentHeight + " Breite: " + currentWidth); |
|
|
|
|
|
|
|
if (previousWidth != currentWidth || previousHeight != currentHeight) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
for (int row = 0; row < previousHeight; row++) { |
|
|
|
for (int col = 0; col < previousWidth; col++) { |
|
|
|
// Blöcke weiter verkleinern |
|
|
|
int blockSize = kleinstesQuadrat(previousHeight, previousWidth) / 8; |
|
|
|
|
|
|
|
//System.out.println(blockSize); |
|
|
|
|
|
|
|
int numBlocksX = currentWidth / blockSize; |
|
|
|
int numBlocksY = currentHeight / blockSize; |
|
|
|
|
|
|
|
int previousIndex = row * previousWidth + col; |
|
|
|
int currentIndex = row * currentWidth + col; |
|
|
|
for (int blockY = 0; blockY < numBlocksY; blockY++) { |
|
|
|
for (int blockX = 0; blockX < numBlocksX; blockX++) { |
|
|
|
startX = blockX * blockSize; |
|
|
|
startY = blockY * blockSize; |
|
|
|
endX = startX + blockSize; |
|
|
|
endY = startY + blockSize; |
|
|
|
|
|
|
|
int previousPixel = previousBuffer.get(previousIndex) & 0xFF; |
|
|
|
int currentPixel = currentBuffer.get(currentIndex) & 0xFF; |
|
|
|
float currentLuminance = berechneMittlereLuminanz(currentBuffer, yRowStride, yPixelStride); |
|
|
|
float previousLuminance = berechneMittlereLuminanz(previousBuffer, yRowStride, yPixelStride); |
|
|
|
|
|
|
|
int pixelDifference = Math.abs(previousPixel - currentPixel); |
|
|
|
int threshold = 120; |
|
|
|
int pixelDifference = Math.abs((int) previousLuminance - (int) currentLuminance); |
|
|
|
|
|
|
|
//System.out.println(pixelDifference); |
|
|
|
|
|
|
|
if (pixelDifference > threshold) { |
|
|
|
// Testzwecke |
|
|
|
//String text = String.valueOf(pixelDifference); |
|
|
|
System.out.println(pixelDifference); |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private float berechneMittlereLuminanz(ByteBuffer buffer, int rowStride, int pixelStride) { |
|
|
|
int sumLuminance = 0; |
|
|
|
int countPixels = 0; |
|
|
|
|
|
|
|
for (int row = startY; row < endY; row++) { |
|
|
|
for (int col = startX; col < endX; col++) { |
|
|
|
int bufferIndex = row * rowStride + col * pixelStride; |
|
|
|
int currentPixel = buffer.get(bufferIndex) & 0xFF; |
|
|
|
|
|
|
|
sumLuminance += currentPixel; |
|
|
|
countPixels++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
float averageLuminance = (float) sumLuminance / countPixels; |
|
|
|
|
|
|
|
return averageLuminance; |
|
|
|
} |
|
|
|
|
|
|
|
private int kleinstesQuadrat(int height, int width) { |
|
|
|
if (height == width) { |
|
|
|
return height; |
|
|
|
} else if (height > width) { |
|
|
|
return kleinstesQuadrat(height - width, width); |
|
|
|
} else { |
|
|
|
return kleinstesQuadrat(height, width - height); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private Runnable alarmResetRunnable = new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
camera_alarm = false; |
|
|
|
alarm.setText("OK"); |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
|