|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ExperimentalGetImage |
|
|
@ExperimentalGetImage |
|
|
public class VideoDetector { |
|
|
public class VideoDetector { |
|
|
|
|
|
|
|
|
// Calling Activity |
|
|
// Calling Activity |
|
|
private final Context context; |
|
|
private final Context context; |
|
|
// Camera Provider |
|
|
// Camera Provider |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Constructor */ |
|
|
/** Constructor */ |
|
|
public VideoDetector(Context context) { this.context = context; } |
|
|
|
|
|
|
|
|
public VideoDetector(Context context) { |
|
|
|
|
|
this.context = context; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** On Detection Listener - runs when a violation is reported */ |
|
|
/** On Detection Listener - runs when a violation is reported */ |
|
|
public interface OnDetectionListener { |
|
|
public interface OnDetectionListener { |
|
|
public void onDetection(DetectionReport detectionReport); |
|
|
|
|
|
|
|
|
void onDetection(DetectionReport detectionReport); |
|
|
} |
|
|
} |
|
|
public void setOnDetectionListener(OnDetectionListener listener) { |
|
|
public void setOnDetectionListener(OnDetectionListener listener) { |
|
|
this.listener = listener; |
|
|
this.listener = listener; |
|
|
} |
|
|
} |
|
|
public void reportViolation(float amplitude) { |
|
|
public void reportViolation(float amplitude) { |
|
|
DetectionReport detectionReport = new DetectionReport("123", "Video", amplitude); |
|
|
|
|
|
if (listener != null) |
|
|
|
|
|
|
|
|
if (listener != null) { |
|
|
|
|
|
DetectionReport detectionReport = new DetectionReport("123", "Video", amplitude); |
|
|
listener.onDetection(detectionReport); |
|
|
listener.onDetection(detectionReport); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Starts Video Detection */ |
|
|
/** Starts Video Detection */ |
|
|
public void startDetection() { |
|
|
public void startDetection() { |
|
|
isDetectionRunning = true; |
|
|
|
|
|
|
|
|
if (isDetectionRunning) |
|
|
|
|
|
return; |
|
|
// 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 |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
cameraProvider = cameraProviderFuture.get(); |
|
|
cameraProvider = cameraProviderFuture.get(); |
|
|
bindLuminosityAnalysis(cameraProvider); |
|
|
bindLuminosityAnalysis(cameraProvider); |
|
|
|
|
|
isDetectionRunning = true; |
|
|
} 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. |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Stops Video Detection */ |
|
|
/** Stops Video Detection */ |
|
|
public void stopDetection() { |
|
|
public void stopDetection() { |
|
|
|
|
|
if (!isDetectionRunning) |
|
|
|
|
|
return; |
|
|
cameraProvider.unbindAll(); |
|
|
cameraProvider.unbindAll(); |
|
|
isDetectionRunning = false; |
|
|
isDetectionRunning = false; |
|
|
} |
|
|
} |