WIP: recorder and detection in one class
This commit is contained in:
parent
2e2b400274
commit
0ad340bb32
@ -1,22 +1,32 @@
|
||||
package com.example.ueberwachungssystem.Detection;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.ImageFormat;
|
||||
import android.media.Image;
|
||||
import android.util.Size;
|
||||
import android.view.Surface;
|
||||
import android.widget.Toast;
|
||||
import android.widget.VideoView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.camera.core.CameraSelector;
|
||||
import androidx.camera.core.ExperimentalGetImage;
|
||||
import androidx.camera.core.ImageAnalysis;
|
||||
import androidx.camera.core.Preview;
|
||||
import androidx.camera.core.VideoCapture;
|
||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||
import androidx.camera.view.PreviewView;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@ -46,13 +56,13 @@ public class VideoDetector extends Detector {
|
||||
private Boolean isDetectionRunning = false;
|
||||
// Preview Camera Image
|
||||
private PreviewView previewView = null;
|
||||
private VideoCapture videoCapture = null;
|
||||
// Detect Violation
|
||||
private static final float PIXEL_THRESHOLD = 30f; // Luminosity (brightness channel of YUV_420_888)
|
||||
private static final float ALARM_THRESHOLD = 0.2f; // Percent of pixels changed
|
||||
private ByteBuffer previousBuffer = null;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param context: the context of calling activity (usually "this")
|
||||
@ -82,7 +92,7 @@ public class VideoDetector extends Detector {
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
// No errors need to be handled for this Future. This should never be reached.
|
||||
}
|
||||
},ContextCompat.getMainExecutor(context));
|
||||
}, ContextCompat.getMainExecutor(context));
|
||||
}
|
||||
|
||||
|
||||
@ -111,6 +121,7 @@ public class VideoDetector extends Detector {
|
||||
* Binds the Luminosity Analyzer (configure and run Analysis)
|
||||
* @param cameraProvider: CameraProvider of Context passed by Constructor
|
||||
* */
|
||||
@SuppressLint("RestrictedApi")
|
||||
private void bindAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
||||
// Configure and create Image Analysis
|
||||
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
||||
@ -145,10 +156,55 @@ public class VideoDetector extends Detector {
|
||||
if (previewView != null)
|
||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
||||
|
||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
||||
videoCapture = new VideoCapture.Builder()
|
||||
.setVideoFrameRate(30)
|
||||
.setTargetRotation(Surface.ROTATION_0)
|
||||
.build();
|
||||
|
||||
|
||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, videoCapture);
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
public void startRecording() {
|
||||
|
||||
File dir = context.getFilesDir();
|
||||
String filename = "recording";
|
||||
String vidFilePath = dir.getAbsolutePath() + "/" + filename + ".mp4";
|
||||
File vidFile = new File(vidFilePath);
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||
// TODO: Consider calling
|
||||
// ActivityCompat#requestPermissions
|
||||
// here to request the missing permissions, and then overriding
|
||||
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
||||
// int[] grantResults)
|
||||
// to handle the case where the user grants the permission. See the documentation
|
||||
// for ActivityCompat#requestPermissions for more details.
|
||||
return;
|
||||
}
|
||||
videoCapture.startRecording(
|
||||
new VideoCapture.OutputFileOptions.Builder(vidFile).build(),
|
||||
context.getMainExecutor(),
|
||||
new VideoCapture.OnVideoSavedCallback() {
|
||||
@Override
|
||||
public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) {
|
||||
Toast.makeText(context, "recording saved", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
|
||||
Toast.makeText(context, "recording failed", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
public void stopRecording(){
|
||||
videoCapture.stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Amount of Pixels changed using a threshold
|
||||
@ -193,4 +249,9 @@ public class VideoDetector extends Detector {
|
||||
|
||||
return changedPixelCount;
|
||||
}
|
||||
|
||||
public void playVideo(VideoView videoView) {
|
||||
videoView.setVideoPath(context.getFilesDir() + "/recording.mp4");
|
||||
videoView.start();
|
||||
}
|
||||
}
|
||||
|
@ -10,15 +10,19 @@ import androidx.camera.core.ExperimentalGetImage;
|
||||
import android.Manifest;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import android.widget.ToggleButton;
|
||||
import android.widget.VideoView;
|
||||
|
||||
import com.example.ueberwachungssystem.Detection.DetectionReport;
|
||||
import com.example.ueberwachungssystem.Detection.Detector;
|
||||
import com.example.ueberwachungssystem.Detection.VideoDetector;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
|
||||
@ExperimentalGetImage
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
@ -38,26 +42,40 @@ public class MainActivity extends AppCompatActivity {
|
||||
File directory = getFilesDir();
|
||||
|
||||
|
||||
VideoDetector vd = new VideoDetector(this);
|
||||
vd.setOnDetectionListener(new Detector.OnDetectionListener() {
|
||||
@Override
|
||||
public void onDetection(@NonNull DetectionReport detectionReport) {
|
||||
Log.d("onDetection", detectionReport.toString());
|
||||
}
|
||||
});
|
||||
vd.startDetection();
|
||||
|
||||
|
||||
DetectionRecorder detectionRecorder = new DetectionRecorder(this);
|
||||
detectionRecorder.setPreviewView(previewView);
|
||||
|
||||
|
||||
//DetectionRecorder detectionRecorder = new DetectionRecorder(this);
|
||||
//detectionRecorder.setPreviewView(previewView);
|
||||
|
||||
ToggleButton toggleButton = findViewById(R.id.previewButton);
|
||||
toggleButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
getRecordVideoAccess();
|
||||
File[] files = directory.listFiles();
|
||||
textView.setText(Arrays.toString(files));
|
||||
|
||||
if (isRecordVideoAllowed() && toggleButton.isChecked())
|
||||
{
|
||||
detectionRecorder.playVideo(videoView);
|
||||
//vd.startRecording();
|
||||
vd.playVideo(videoView);
|
||||
//detectionRecorder.playVideo(videoView);
|
||||
//detectionRecorder.start();
|
||||
}
|
||||
else {
|
||||
detectionRecorder.stop();
|
||||
File[] files = directory.listFiles();
|
||||
//textView.setText(Arrays.toString(files));
|
||||
//vd.stopRecording();
|
||||
|
||||
//detectionRecorder.stop();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user