|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
import android.content.Context; |
|
|
import android.content.pm.PackageManager; |
|
|
import android.content.pm.PackageManager; |
|
|
import android.graphics.ImageFormat; |
|
|
import android.graphics.ImageFormat; |
|
|
|
|
|
import android.graphics.Rect; |
|
|
|
|
|
import android.hardware.SensorManager; |
|
|
import android.media.Image; |
|
|
import android.media.Image; |
|
|
import android.os.CountDownTimer; |
|
|
import android.os.CountDownTimer; |
|
|
import android.util.Log; |
|
|
import android.util.Log; |
|
|
|
|
|
import android.view.Display; |
|
|
|
|
|
import android.view.OrientationEventListener; |
|
|
import android.view.Surface; |
|
|
import android.view.Surface; |
|
|
|
|
|
import android.view.WindowManager; |
|
|
import android.widget.ImageView; |
|
|
import android.widget.ImageView; |
|
|
import android.widget.Toast; |
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.Nullable; |
|
|
import androidx.annotation.Nullable; |
|
|
|
|
|
import androidx.camera.core.AspectRatio; |
|
|
import androidx.camera.core.CameraSelector; |
|
|
import androidx.camera.core.CameraSelector; |
|
|
import androidx.camera.core.ExperimentalGetImage; |
|
|
import androidx.camera.core.ExperimentalGetImage; |
|
|
import androidx.camera.core.ImageAnalysis; |
|
|
import androidx.camera.core.ImageAnalysis; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Camera Provider |
|
|
// Camera Provider |
|
|
private ProcessCameraProvider cameraProvider; |
|
|
private ProcessCameraProvider cameraProvider; |
|
|
private final ImageAnalysis imageAnalysis; |
|
|
|
|
|
private final VideoCapture videoCapture; |
|
|
|
|
|
|
|
|
private ImageAnalysis imageAnalysis; |
|
|
|
|
|
private VideoCapture videoCapture; |
|
|
private final Preview preview; |
|
|
private final Preview preview; |
|
|
|
|
|
|
|
|
// Logic |
|
|
// Logic |
|
|
|
|
|
|
|
|
private ImageView inputImageView = null; |
|
|
private ImageView inputImageView = null; |
|
|
private ImageView outputImageView = null; |
|
|
private ImageView outputImageView = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Recording |
|
|
|
|
|
private final String outputName = "video.mp4"; |
|
|
|
|
|
|
|
|
private int rotation = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Parameters |
|
|
// Parameters |
|
|
|
|
|
|
|
|
getPermissions(); |
|
|
getPermissions(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
imageAnalysis = setupImageAnalysis(); |
|
|
|
|
|
|
|
|
// Open CV startup check |
|
|
// Open CV startup check |
|
|
if (!OpenCVLoader.initDebug()) { |
|
|
if (!OpenCVLoader.initDebug()) { |
|
|
Log.e("OpenCV", "Unable to load OpenCV!"); |
|
|
Log.e("OpenCV", "Unable to load OpenCV!"); |
|
|
|
|
|
|
|
|
builder.setTargetResolution(IMAGE_RES); |
|
|
builder.setTargetResolution(IMAGE_RES); |
|
|
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST); |
|
|
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST); |
|
|
builder.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888); |
|
|
builder.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888); |
|
|
|
|
|
builder.setTargetRotation(Surface.ROTATION_90); |
|
|
ImageAnalysis imageAnalysis = builder.build(); |
|
|
ImageAnalysis imageAnalysis = builder.build(); |
|
|
// Set Analyzer |
|
|
// Set Analyzer |
|
|
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> { |
|
|
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Violation Handling |
|
|
// Violation Handling |
|
|
Mat processed = processImage(imageProxy); |
|
|
Mat processed = processImage(imageProxy); |
|
|
|
|
|
|
|
|
int n = OpenCVHelper.countNonZeroPixels(processed); |
|
|
int n = OpenCVHelper.countNonZeroPixels(processed); |
|
|
int pixelCount = image.getWidth() * image.getHeight(); |
|
|
int pixelCount = image.getWidth() * image.getHeight(); |
|
|
float percentChanged = (float) n / pixelCount; |
|
|
float percentChanged = (float) n / pixelCount; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("RestrictedApi") |
|
|
@SuppressLint("RestrictedApi") |
|
|
private VideoCapture setupVideoCapture() { |
|
|
private VideoCapture setupVideoCapture() { |
|
|
|
|
|
int rotation = getRotation(); |
|
|
return new VideoCapture.Builder() |
|
|
return new VideoCapture.Builder() |
|
|
.setTargetRotation(Surface.ROTATION_0) |
|
|
|
|
|
|
|
|
.setTargetRotation(rotation) |
|
|
.build(); |
|
|
.build(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
videoCapture = setupVideoCapture(); |
|
|
|
|
|
|
|
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context); |
|
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context); |
|
|
cameraProviderFuture.addListener(() -> { |
|
|
cameraProviderFuture.addListener(() -> { |
|
|
try { |
|
|
try { |
|
|
|
|
|
|
|
|
// Show Output Image |
|
|
// Show Output Image |
|
|
if (outputImageView != null) |
|
|
if (outputImageView != null) |
|
|
OpenCVHelper.debugMat(processed, outputImageView); |
|
|
OpenCVHelper.debugMat(processed, outputImageView); |
|
|
|
|
|
|
|
|
return processed; |
|
|
return processed; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Return the timestamp as a string |
|
|
// Return the timestamp as a string |
|
|
return currentTime.format(formatter); |
|
|
return currentTime.format(formatter); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int getRotation() { |
|
|
|
|
|
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
|
|
|
|
|
Display display = windowManager.getDefaultDisplay(); |
|
|
|
|
|
return display.getRotation(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |