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