Fixed Grey Stripes and Rotation Problems

This commit is contained in:
Bastian Kohler 2023-06-18 18:12:03 +02:00
parent 6954d38143
commit 493fa8ca83
3 changed files with 31 additions and 8 deletions

View File

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

View File

@ -53,6 +53,7 @@ public class MainActivity extends AppCompatActivity {
if (toggleButton.isChecked())
{
//vd.startDetection();
vd.stopDetection();
vd.startRecording();
audioRecorder.startRecording();
}

View File

@ -24,15 +24,18 @@
<ImageView
android:id="@+id/inputImageView"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars" />
<ImageView
android:id="@+id/outputImageView"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars" />
<androidx.camera.view.PreviewView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>