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

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


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



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

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

}

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

android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/erklaerung"> 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 <androidx.camera.view.PreviewView
android:id="@+id/previewView" android:id="@+id/previewView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent">

</androidx.camera.view.PreviewView>
</FrameLayout> </FrameLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:orientation="vertical"> android:orientation="vertical">


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

Loading…
Cancel
Save