Browse Source

Preview entfernt ausgabe des Alarmtext aktuell über activity_main.xml

Kamera
Michael Pilhoefer 1 year ago
parent
commit
6154414f12

+ 1
- 0
app/src/main/AndroidManifest.xml View File

@@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera"/>


+ 21
- 32
app/src/main/java/com/example/greenwatch/KameraAktivitaet.java View File

@@ -4,14 +4,15 @@ 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;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
// PREVIEW
//import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;

import com.google.common.util.concurrent.ListenableFuture;
@@ -20,22 +21,26 @@ import java.nio.ByteBuffer;
import java.util.concurrent.ExecutionException;

public class KameraAktivitaet extends AppCompatActivity {
private PreviewView previewView;

// PREVIEW
// private PreviewView previewView;
private boolean isMotionDetected;
private boolean camera_alarm;
private TextView alarm;
private ByteBuffer previousBuffer;
private int previousWidth;
private int previousHeight;
private boolean camera_alarm = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// NUR FÜR TESTS
alarm = findViewById(R.id.textView);

// PREVIEW //
//previewView = findViewById(R.id.previewView);
// previewView = findViewById(R.id.previewView);

final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
@@ -78,42 +83,26 @@ public class KameraAktivitaet extends AppCompatActivity {
currentImage.close();

if (isMotionDetected) {

// Code um den Alarm zu setzen aktuell noch über setText
alarm.setText("ALARM");
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");
}

}

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();

// PREVIEW //
//preview.setSurfaceProvider(previewView.getSurfaceProvider());
// preview.setSurfaceProvider(previewView.getSurfaceProvider());

cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis, preview);
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis);

}

@@ -142,12 +131,12 @@ public class KameraAktivitaet extends AppCompatActivity {
int threshold = 120;

if (pixelDifference > threshold) {
String text = String.valueOf(pixelDifference);
// Testzwecke
//String text = String.valueOf(pixelDifference);
return true;
}
}
}
return false;
}
}

}

+ 11
- 2
app/src/main/res/layout/activity_demo_kamera2_nebenaktivitaet.xml View File

@@ -10,10 +10,19 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/erklaerung">

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent">

</androidx.camera.view.PreviewView>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
@@ -21,7 +30,7 @@
android:orientation="vertical">

<TextView
android:id="@+id/alarm_test"
android:id="@+id/alarm"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"

Loading…
Cancel
Save