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