|
|
@@ -0,0 +1,320 @@ |
|
|
|
package com.example.greenwatch; |
|
|
|
|
|
|
|
import android.graphics.ImageFormat; |
|
|
|
import android.hardware.Sensor; |
|
|
|
import android.hardware.SensorEvent; |
|
|
|
import android.hardware.SensorEventListener; |
|
|
|
import android.hardware.SensorManager; |
|
|
|
import android.media.Image; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Handler; |
|
|
|
import android.os.HandlerThread; |
|
|
|
import android.os.Looper; |
|
|
|
import android.util.Size; |
|
|
|
|
|
|
|
import android.widget.TextView; |
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
|
import androidx.camera.core.CameraSelector; |
|
|
|
import androidx.camera.core.ImageAnalysis; |
|
|
|
import androidx.camera.lifecycle.ProcessCameraProvider; |
|
|
|
// PREVIEW |
|
|
|
//import androidx.camera.view.PreviewView; |
|
|
|
import androidx.core.content.ContextCompat; |
|
|
|
|
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
|
|
|
|
import java.nio.ByteBuffer; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
import java.util.concurrent.Executor; |
|
|
|
|
|
|
|
public class Kamera_Beschleunigungssensor extends AppCompatActivity implements SensorEventListener { |
|
|
|
|
|
|
|
private HandlerThread cameraThread; |
|
|
|
private Handler cameraHandler; |
|
|
|
// PREVIEW |
|
|
|
// private PreviewView previewView; |
|
|
|
private boolean isMotionDetected; |
|
|
|
private boolean camera_alarm; |
|
|
|
private TextView alarm; |
|
|
|
private ByteBuffer previousBuffer; |
|
|
|
private int previousWidth; |
|
|
|
private int previousHeight; |
|
|
|
|
|
|
|
///////////////////////////////////// |
|
|
|
|
|
|
|
private SensorManager bsmanager; |
|
|
|
private ArrayList<Float> Gesamt_be; |
|
|
|
private int sensorType = Sensor.TYPE_GYROSCOPE, zaehler_runde =0, listen_groesse = 500, kalibrieren, Offset_shared = 0, shared_merk, mittelwertsumme_shared; |
|
|
|
private Sensor sens, sensor_l; |
|
|
|
private float x_value = 0, y_value, z_value, Vorbesetzung = 0, gesamt_runde =0; |
|
|
|
private double Schwellwert = 0.15, mittelwertsumme = 0.0, Offset = 0.1; |
|
|
|
String Daten, Warnung, Zeitstempel, Array_shared; |
|
|
|
private final static String KEY_kalibirieren = "KEY_KALIBRIEREN_"+Beschleunigungssensor.class.getSimpleName(); |
|
|
|
private final static String KEY_Gesamt_be = "KEY_GESAMT_BE_"+Beschleunigungssensor.class.getSimpleName(); |
|
|
|
private final static String KEY_Offset = "KEY_OFFSET_"+Beschleunigungssensor.class.getSimpleName(); |
|
|
|
private final static String KEY_Mittelwertsumme = "KEY_MITTELWERTSUMME_"+Beschleunigungssensor.class.getSimpleName(); |
|
|
|
TextView Daten_Bsensor, Warnung_Bsensor, test; |
|
|
|
private boolean toggle = true, ts_setzen = true, start_messen; |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
setContentView(R.layout.activity_beschleunigung_kamera); |
|
|
|
//////// |
|
|
|
Daten_Bsensor = (TextView) findViewById(R.id.DatenBsensor); |
|
|
|
Warnung_Bsensor = (TextView) findViewById(R.id.WarnungBsensor); |
|
|
|
test = (TextView) findViewById(R.id.test); |
|
|
|
|
|
|
|
bsmanager = (SensorManager) getSystemService(SENSOR_SERVICE); |
|
|
|
if (bsmanager.getSensorList(Sensor.TYPE_GYROSCOPE).size() == 0) { |
|
|
|
sens = null; |
|
|
|
Daten = "kein B Sensor voranden"; |
|
|
|
} else { |
|
|
|
sens = bsmanager.getSensorList(sensorType).get(0); |
|
|
|
} |
|
|
|
Gesamt_be = new ArrayList<Float>(); |
|
|
|
for (int i = 0; i < listen_groesse ; i++){ |
|
|
|
Gesamt_be.add(Vorbesetzung); |
|
|
|
} |
|
|
|
kalibrieren = 0; |
|
|
|
start_messen = false; |
|
|
|
|
|
|
|
/////// |
|
|
|
// NUR FÜR TESTS |
|
|
|
alarm = findViewById(R.id.Alarm_Kamera); |
|
|
|
|
|
|
|
// PREVIEW // |
|
|
|
// previewView = findViewById(R.id.previewView); |
|
|
|
|
|
|
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this); |
|
|
|
cameraProviderFuture.addListener(() -> { |
|
|
|
try { |
|
|
|
bindImageAnalysis(cameraProviderFuture.get()); |
|
|
|
} catch (ExecutionException | InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
}, ContextCompat.getMainExecutor(this)); |
|
|
|
} |
|
|
|
|
|
|
|
public static String getTimestamp(){ |
|
|
|
Long tslong = System.currentTimeMillis(); |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); |
|
|
|
Date date = new Date(tslong); |
|
|
|
String result = sdf.format(date); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onSensorChanged(SensorEvent event) { |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
sb.append("x=").append(event.values[0])//.append(event.timestamp) |
|
|
|
.append("\ny=").append(event.values[1]) |
|
|
|
.append("\nz=").append(event.values[2]); |
|
|
|
Daten = sb.toString(); |
|
|
|
Daten_Bsensor.setText(Daten); |
|
|
|
gesamt_runde = event.values[0] + event.values[1] + event.values[2]; |
|
|
|
Gesamt_be.remove(zaehler_runde); |
|
|
|
Gesamt_be.add(zaehler_runde, gesamt_runde); |
|
|
|
mittelwertsumme = 0.0; |
|
|
|
|
|
|
|
//Mittelwert des Arrays berechnen |
|
|
|
for (int i = 0; i < listen_groesse; i++){ |
|
|
|
if (Gesamt_be.get(i) < 0){ |
|
|
|
mittelwertsumme += (Gesamt_be.get(i) * (-1)); |
|
|
|
}else { |
|
|
|
mittelwertsumme += Gesamt_be.get(i); |
|
|
|
} |
|
|
|
} |
|
|
|
mittelwertsumme = mittelwertsumme/listen_groesse; |
|
|
|
if (kalibrieren < listen_groesse){ |
|
|
|
Offset = mittelwertsumme; |
|
|
|
kalibrieren++; |
|
|
|
}else { |
|
|
|
start_messen = true; |
|
|
|
} |
|
|
|
StringBuilder testsb = new StringBuilder(); |
|
|
|
testsb.append(mittelwertsumme); |
|
|
|
String tests = testsb.toString(); |
|
|
|
test.setText(tests); |
|
|
|
|
|
|
|
if((mittelwertsumme > (Schwellwert+Offset) ) & (ts_setzen == true) &(start_messen == true)){ |
|
|
|
Zeitstempel = getTimestamp(); |
|
|
|
StringBuilder ts = new StringBuilder(); |
|
|
|
ts.append(Zeitstempel) |
|
|
|
.append(", Gruppe3") |
|
|
|
.append(", An") |
|
|
|
.append(", Bewegung, ") |
|
|
|
.append(mittelwertsumme); |
|
|
|
Warnung = ts.toString(); |
|
|
|
Warnung_Bsensor.setText(Warnung); |
|
|
|
ts_setzen = false; |
|
|
|
}else if((mittelwertsumme > (Schwellwert+Offset)) & (ts_setzen == false)){ |
|
|
|
} else if ((mittelwertsumme < (Schwellwert+Offset)) & (ts_setzen == false)) { |
|
|
|
Warnung_Bsensor.setText(""); |
|
|
|
ts_setzen = true; |
|
|
|
} |
|
|
|
|
|
|
|
if (zaehler_runde < (listen_groesse -1)){ |
|
|
|
zaehler_runde++; |
|
|
|
}else { |
|
|
|
zaehler_runde = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
//mittelwertsumme ist kein int in diesem programm |
|
|
|
//shared prefenrences: bei Kippen speichern -> nochmal neu klalibrieren? -> eig ja, weil neuer Standort |
|
|
|
//Vorbelegung nicht nochmal neu vornehmen -> aus onCreate Funktion raus -> hilfsvariablen auch in shared preferences speichern |
|
|
|
@Override |
|
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onResume() { |
|
|
|
super.onResume(); |
|
|
|
if (sens != null) { |
|
|
|
if (bsmanager.registerListener(this, sens, SensorManager.SENSOR_DELAY_GAME)) { |
|
|
|
Daten = "Wir haben uns beim Sensor angemeldet"; |
|
|
|
} else { |
|
|
|
Daten = "Das anmelden hat nicht geklappt"; |
|
|
|
} |
|
|
|
} |
|
|
|
//SharedPreferences p = getPreferences(Context.MODE_PRIVATE); |
|
|
|
//mittelwertsumme_shared = p.getInt(KEY_Mittelwertsumme, 0); |
|
|
|
//kalibrieren = p.getInt(KEY_kalibirieren, 0); |
|
|
|
//Offset_shared = p.getInt(KEY_Offset, 0 ); |
|
|
|
//Array_shared = p.getString(KEY_Gesamt_be, ""); |
|
|
|
|
|
|
|
//mittelwertsumme = (double) mittelwertsumme_shared; |
|
|
|
//mittelwertsumme /= 1000000.0; |
|
|
|
|
|
|
|
//Offset = (double) Offset_shared; |
|
|
|
//Offset /= 1000000.0; |
|
|
|
|
|
|
|
//String zw[] = Array_shared.split(";", listen_groesse); |
|
|
|
|
|
|
|
//if(shared_merk == 2){ |
|
|
|
//for (int i=0; i<listen_groesse; i++){ |
|
|
|
//float wert = Float.valueOf(zw[i]); |
|
|
|
//Gesamt_be.add(i, wert); |
|
|
|
//}} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onPause () { |
|
|
|
super.onPause(); |
|
|
|
if (sens != null) { |
|
|
|
bsmanager.unregisterListener(this, sens); |
|
|
|
} |
|
|
|
//shared_merk = 2; |
|
|
|
// Offset_shared = (int) (Offset * 1000000.0); |
|
|
|
//mittelwertsumme_shared = (int) (mittelwertsumme * 1000000.0); |
|
|
|
|
|
|
|
//StringBuilder ar = new StringBuilder(); |
|
|
|
//for (int z=0; z < listen_groesse; z++){ |
|
|
|
//ar.append(Gesamt_be.get(z)).append(";"); |
|
|
|
//} |
|
|
|
//Array_shared = ar.toString(); |
|
|
|
|
|
|
|
//SharedPreferences p = getPreferences(Context.MODE_PRIVATE); |
|
|
|
//SharedPreferences.Editor editor = p.edit(); |
|
|
|
//editor.putInt(KEY_Mittelwertsumme, mittelwertsumme_shared); |
|
|
|
//editor.putInt(KEY_kalibirieren, kalibrieren); |
|
|
|
//editor.putInt(KEY_Offset, Offset_shared); |
|
|
|
//editor.putString(KEY_Gesamt_be, Array_shared); |
|
|
|
//editor.commit(); |
|
|
|
} |
|
|
|
|
|
|
|
// KAMERA // |
|
|
|
private void bindImageAnalysis(ProcessCameraProvider cameraProvider) { |
|
|
|
ImageAnalysis.Builder builder = new ImageAnalysis.Builder(); |
|
|
|
builder.setTargetResolution(new Size(640, 480)); |
|
|
|
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST); |
|
|
|
ImageAnalysis imageAnalysis = builder.build(); |
|
|
|
imageAnalysis.setAnalyzer( |
|
|
|
ContextCompat.getMainExecutor(this), |
|
|
|
imageProxy -> { |
|
|
|
|
|
|
|
int imageFormat = imageProxy.getFormat(); |
|
|
|
|
|
|
|
if (imageFormat == ImageFormat.YUV_420_888) { |
|
|
|
|
|
|
|
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"); |
|
|
|
camera_alarm = true; |
|
|
|
} else { |
|
|
|
camera_alarm = false; |
|
|
|
alarm.setText("OK"); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imageProxy.close(); |
|
|
|
}); |
|
|
|
|
|
|
|
// Preview preview = new Preview.Builder().build(); |
|
|
|
|
|
|
|
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build(); |
|
|
|
|
|
|
|
// PREVIEW // |
|
|
|
// preview.setSurfaceProvider(previewView.getSurfaceProvider()); |
|
|
|
|
|
|
|
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Bildverarbeitung zur Bewegungserkennung |
|
|
|
private boolean compareFrames(Image currentImage) { |
|
|
|
|
|
|
|
ByteBuffer currentBuffer = currentImage.getPlanes()[0].getBuffer(); |
|
|
|
|
|
|
|
int currentWidth = currentImage.getWidth(); |
|
|
|
int currentHeight = currentImage.getHeight(); |
|
|
|
|
|
|
|
if (previousWidth != currentWidth || previousHeight != currentHeight) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
for (int row = 0; row < previousHeight; row++) { |
|
|
|
for (int col = 0; col < previousWidth; col++) { |
|
|
|
|
|
|
|
int previousIndex = row * previousWidth + col; |
|
|
|
int currentIndex = row * currentWidth + col; |
|
|
|
|
|
|
|
int previousPixel = previousBuffer.get(previousIndex) & 0xFF; |
|
|
|
int currentPixel = currentBuffer.get(currentIndex) & 0xFF; |
|
|
|
|
|
|
|
int pixelDifference = Math.abs(previousPixel - currentPixel); |
|
|
|
int threshold = 120; |
|
|
|
|
|
|
|
if (pixelDifference > threshold) { |
|
|
|
// Testzwecke |
|
|
|
//String text = String.valueOf(pixelDifference); |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |