|
|
|
|
|
|
|
|
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 android.widget.TextView; |
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
import androidx.camera.core.CameraSelector; |
|
|
import androidx.camera.core.CameraSelector; |
|
|
import androidx.camera.core.ImageAnalysis; |
|
|
import androidx.camera.core.ImageAnalysis; |
|
|
import androidx.camera.core.Preview; |
|
|
|
|
|
import androidx.camera.lifecycle.ProcessCameraProvider; |
|
|
import androidx.camera.lifecycle.ProcessCameraProvider; |
|
|
import androidx.camera.view.PreviewView; |
|
|
|
|
|
|
|
|
// PREVIEW |
|
|
|
|
|
//import androidx.camera.view.PreviewView; |
|
|
import androidx.core.content.ContextCompat; |
|
|
import androidx.core.content.ContextCompat; |
|
|
|
|
|
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
|
|
|
|
|
public class KameraAktivitaet extends AppCompatActivity { |
|
|
public class KameraAktivitaet extends AppCompatActivity { |
|
|
private PreviewView previewView; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// PREVIEW |
|
|
|
|
|
// private PreviewView previewView; |
|
|
private boolean isMotionDetected; |
|
|
private boolean isMotionDetected; |
|
|
|
|
|
private boolean camera_alarm; |
|
|
private TextView alarm; |
|
|
private TextView alarm; |
|
|
private ByteBuffer previousBuffer; |
|
|
private ByteBuffer previousBuffer; |
|
|
private int previousWidth; |
|
|
private int previousWidth; |
|
|
private int previousHeight; |
|
|
private int previousHeight; |
|
|
private boolean camera_alarm = false; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
super.onCreate(savedInstanceState); |
|
|
super.onCreate(savedInstanceState); |
|
|
setContentView(R.layout.activity_main); |
|
|
setContentView(R.layout.activity_main); |
|
|
|
|
|
|
|
|
|
|
|
// NUR FÜR TESTS |
|
|
alarm = findViewById(R.id.textView); |
|
|
alarm = findViewById(R.id.textView); |
|
|
|
|
|
|
|
|
// PREVIEW // |
|
|
// PREVIEW // |
|
|
//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(() -> { |
|
|
|
|
|
|
|
|
currentImage.close(); |
|
|
currentImage.close(); |
|
|
|
|
|
|
|
|
if (isMotionDetected) { |
|
|
if (isMotionDetected) { |
|
|
|
|
|
|
|
|
// Code um den Alarm zu setzen aktuell noch über setText |
|
|
|
|
|
alarm.setText("ALARM"); |
|
|
alarm.setText("ALARM"); |
|
|
camera_alarm = true; |
|
|
camera_alarm = true; |
|
|
|
|
|
|
|
|
runOnUiThread(() -> { |
|
|
|
|
|
|
|
|
|
|
|
// Platz um die Videoaufzeichnung zu starten |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
// Optional evtl nicht nötig |
|
|
|
|
|
if (camera_alarm) { |
|
|
|
|
|
/* |
|
|
|
|
|
try { |
|
|
|
|
|
Thread.sleep(2000); // Wartezeit wie lange der Alarm gesetzt sein soll bzw. die Aufzeichnung laufen soll |
|
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} */ |
|
|
|
|
|
|
|
|
|
|
|
camera_alarm = false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
camera_alarm = false; |
|
|
alarm.setText("OK"); |
|
|
alarm.setText("OK"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
imageProxy.close(); |
|
|
imageProxy.close(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
Preview preview = new Preview.Builder().build(); |
|
|
|
|
|
|
|
|
// Preview preview = new Preview.Builder().build(); |
|
|
|
|
|
|
|
|
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build(); |
|
|
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build(); |
|
|
|
|
|
|
|
|
// PREVIEW // |
|
|
// PREVIEW // |
|
|
//preview.setSurfaceProvider(previewView.getSurfaceProvider()); |
|
|
|
|
|
|
|
|
// preview.setSurfaceProvider(previewView.getSurfaceProvider()); |
|
|
|
|
|
|
|
|
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis, preview); |
|
|
|
|
|
|
|
|
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int threshold = 120; |
|
|
int threshold = 120; |
|
|
|
|
|
|
|
|
if (pixelDifference > threshold) { |
|
|
if (pixelDifference > threshold) { |
|
|
String text = String.valueOf(pixelDifference); |
|
|
|
|
|
|
|
|
// Testzwecke |
|
|
|
|
|
//String text = String.valueOf(pixelDifference); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |