added Functions to VideoDetector:
startDetection(), stopDetection(), isRunning()
This commit is contained in:
parent
323786859e
commit
c4ed247a05
@ -4,8 +4,6 @@ import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.camera.core.Camera;
|
||||
import androidx.camera.core.CameraSelector;
|
||||
import androidx.camera.core.ImageAnalysis;
|
||||
import androidx.camera.core.ImageProxy;
|
||||
import androidx.camera.core.Preview;
|
||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||
import androidx.camera.view.PreviewView;
|
||||
@ -16,11 +14,7 @@ import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.ImageFormat;
|
||||
import android.media.Image;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.util.Size;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
@ -28,7 +22,6 @@ import android.widget.Toast;
|
||||
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@ExperimentalGetImage
|
||||
@ -39,6 +32,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
private TextView textView;
|
||||
private Button previewButton;
|
||||
private Boolean cameraPreviewIsRunning = false;
|
||||
private Boolean isPressed = false;
|
||||
|
||||
|
||||
@Override
|
||||
@ -52,19 +46,25 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
|
||||
VideoDetector vd = new VideoDetector(this);
|
||||
vd.startDetection();
|
||||
|
||||
previewButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
getCameraAccess();
|
||||
if (isCameraAccessAllowed())
|
||||
vd.runImageAnalysis();
|
||||
if (isCameraAccessAllowed() && !isPressed)
|
||||
{
|
||||
vd.stopDetection();
|
||||
isPressed = true;
|
||||
}
|
||||
else if (isCameraAccessAllowed() && isPressed) {
|
||||
vd.startDetection();
|
||||
isPressed = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
private boolean isCameraAccessAllowed() {
|
||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
@ -21,19 +21,27 @@ import com.google.common.util.concurrent.ListenableFuture;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
|
||||
@ExperimentalGetImage
|
||||
public class VideoDetector {
|
||||
|
||||
private final Context context;
|
||||
private float currentLuminosity;
|
||||
|
||||
private ProcessCameraProvider cameraProvider;
|
||||
private Boolean isDetectionRunning = false;
|
||||
//private float currentLuminosity;
|
||||
|
||||
/** Constructor */
|
||||
public VideoDetector(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void runImageAnalysis() {
|
||||
/** Return State of Video Detector */
|
||||
public Boolean isRunning() {
|
||||
return isDetectionRunning;
|
||||
}
|
||||
|
||||
/** Starts Video Detection */
|
||||
public void startDetection() {
|
||||
isDetectionRunning = true;
|
||||
// Request Camera Provider
|
||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||
//Check for Camera availability
|
||||
@ -41,8 +49,8 @@ public class VideoDetector {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
|
||||
bindImageAnalysis(cameraProvider);
|
||||
cameraProvider = cameraProviderFuture.get();
|
||||
bindLuminosityAnalysis(cameraProvider);
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
// No errors need to be handled for this Future. This should never be reached.
|
||||
}
|
||||
@ -50,7 +58,14 @@ public class VideoDetector {
|
||||
},ContextCompat.getMainExecutor(context));
|
||||
}
|
||||
|
||||
private void bindImageAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
||||
/** Stops Video Detection */
|
||||
public void stopDetection() {
|
||||
cameraProvider.unbindAll();
|
||||
isDetectionRunning = false;
|
||||
}
|
||||
|
||||
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
||||
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
||||
// Configure and create Image Analysis
|
||||
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
||||
builder.setTargetResolution(new Size(640, 480));
|
||||
@ -65,8 +80,8 @@ public class VideoDetector {
|
||||
Image image = imageProxy.getImage();
|
||||
assert image != null;
|
||||
|
||||
// Analyze frame
|
||||
float luminosity = calculateLuminosity(image);
|
||||
currentLuminosity = luminosity;
|
||||
Log.d("Video Detector", String.valueOf(luminosity));
|
||||
}
|
||||
imageProxy.close();
|
||||
@ -80,7 +95,7 @@ public class VideoDetector {
|
||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
||||
}
|
||||
|
||||
|
||||
/** Return Luminosity from Image */
|
||||
private float calculateLuminosity (Image image) {
|
||||
int width = image.getWidth();
|
||||
int height = image.getHeight();
|
||||
|
Loading…
x
Reference in New Issue
Block a user