Control commit

This commit is contained in:
Captain Arepa 2024-05-28 14:10:47 -04:00
parent 01bc596284
commit ebdff6b407
4 changed files with 22 additions and 26 deletions

View file

@ -48,4 +48,5 @@ dependencies {
testImplementation libs.junit testImplementation libs.junit
androidTestImplementation libs.ext.junit androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core androidTestImplementation libs.espresso.core
testImplementation libs.robolectric
} }

View file

@ -3,7 +3,6 @@ package com.example.cameraxtestappjava.segpass;
import android.Manifest; import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.ImageFormat; import android.graphics.ImageFormat;
@ -22,7 +21,6 @@ import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.StreamConfigurationMap; import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image; import android.media.Image;
import android.media.ImageReader; import android.media.ImageReader;
import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.util.Log; import android.util.Log;
@ -45,7 +43,6 @@ import com.example.cameraxtestappjava.segpass.camera.utils.SegpassCameraCallback
import com.example.cameraxtestappjava.segpass.camera.utils.SegpassPermissionListener; import com.example.cameraxtestappjava.segpass.camera.utils.SegpassPermissionListener;
import com.example.cameraxtestappjava.segpass.camera.view.AutoFitTextureView; import com.example.cameraxtestappjava.segpass.camera.view.AutoFitTextureView;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -135,21 +132,18 @@ public class SegpassCamera {
*/ */
private ImageReader mImageReader; private ImageReader mImageReader;
/** private ImageReader mCaptureImageReader;
* This is the folder where the pictures will be saved.
*/
private File mFileFolder;
/** /**
* This is the resulting image encoded to Base64 * This is the resulting image encoded to Base64
*/ */
private String mBase64Value; private String mBase64Value;
private final CaptureRequest.Key<Integer> mCaptureAfModeKey = CaptureRequest.CONTROL_AF_MODE; private static final CaptureRequest.Key<Integer> mCaptureAfModeKey = CaptureRequest.CONTROL_AF_MODE;
private final int mCaptureAfModeValue = CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE; private static final int mCaptureAfModeValue = CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE;
private final CaptureRequest.Key<Integer> mCaptureAfTriggerKey = CaptureRequest.CONTROL_AF_TRIGGER; private static final CaptureRequest.Key<Integer> mCaptureAfTriggerKey = CaptureRequest.CONTROL_AF_TRIGGER;
private final int mCaptureAfTriggervalue = CaptureRequest.CONTROL_AF_TRIGGER_START; private static final int mCaptureAfTriggerValue = CameraMetadata.CONTROL_AF_TRIGGER_START;
/** /**
* {@link CaptureRequest.Builder} for the camera preview * {@link CaptureRequest.Builder} for the camera preview
@ -454,7 +448,7 @@ public class SegpassCamera {
// If facis is null, return // If facis is null, return
if (facing == null) return null; if (facing == null) return null;
if (facing == CameraCharacteristics.LENS_FACING_BACK) return null; if (facing == CameraMetadata.LENS_FACING_BACK) return null;
return characteristics.get( return characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
@ -598,7 +592,7 @@ public class SegpassCamera {
// Auto focus should be continuous for camera preview. // Auto focus should be continuous for camera preview.
mPreviewRequestBuilder.set(mCaptureAfModeKey, mCaptureAfModeValue); mPreviewRequestBuilder.set(mCaptureAfModeKey, mCaptureAfModeValue);
// Trigger AF // Trigger AF
mPreviewRequestBuilder.set(mCaptureAfTriggerKey, mCaptureAfTriggervalue); mPreviewRequestBuilder.set(mCaptureAfTriggerKey, mCaptureAfTriggerValue);
// Finally, we start displaying the camera preview. // Finally, we start displaying the camera preview.
mPreviewRequest = mPreviewRequestBuilder.build(); mPreviewRequest = mPreviewRequestBuilder.build();
@ -648,7 +642,7 @@ public class SegpassCamera {
(float) viewHeight / mPreviewSize.getHeight(), (float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth()); (float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY); matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2L), centerX, centerY); matrix.postRotate(90L * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) { } else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY); matrix.postRotate(180, centerX, centerY);
} }
@ -683,23 +677,23 @@ public class SegpassCamera {
} }
// Set a new instance of ImageReader // Set a new instance of ImageReader
ImageReader mReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1); mCaptureImageReader = getCaptureImageReader(width, height);
// Set the output surfaces (from where the picture is taken) // Set the output surfaces (from where the picture is taken)
List<Surface> outputSurfaces = new ArrayList<>(2); List<Surface> outputSurfaces = new ArrayList<>(2);
outputSurfaces.add(mReader.getSurface()); outputSurfaces.add(mCaptureImageReader.getSurface());
outputSurfaces.add(new Surface(mTextureView.getSurfaceTexture())); outputSurfaces.add(new Surface(mTextureView.getSurfaceTexture()));
// Create the CaptiureBuilder // Create the CaptiureBuilder
final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(mReader.getSurface()); captureBuilder.addTarget(mCaptureImageReader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
// Picture orientation // Picture orientation
int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation(); int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation)); captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
mReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler); mCaptureImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() { final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override @Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
@ -712,6 +706,7 @@ public class SegpassCamera {
mCameraCallback.onPictureTakenFailError("Error saving picture..."); mCameraCallback.onPictureTakenFailError("Error saving picture...");
} }
Log.d(TAG, "Recreating camera preview..."); Log.d(TAG, "Recreating camera preview...");
mCaptureImageReader.close();
createCameraPreviewSession(); createCameraPreviewSession();
} }
}; };
@ -729,6 +724,7 @@ public class SegpassCamera {
@Override @Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) { public void onConfigureFailed(@NonNull CameraCaptureSession session) {
// No implementation needed here.
} }
}, mBackgroundHandler); }, mBackgroundHandler);
} catch (CameraAccessException e) { } catch (CameraAccessException e) {
@ -738,6 +734,11 @@ public class SegpassCamera {
} }
@NonNull
private static ImageReader getCaptureImageReader(int width, int height) {
return ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
}
private int getOrientation(int rotation) { private int getOrientation(int rotation) {
// Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X) // Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X)
// We have to take that into account and rotate JPEG properly. // We have to take that into account and rotate JPEG properly.

View file

@ -2,19 +2,11 @@ package com.example.cameraxtestappjava.segpass.camera.exceptions;
public class SegpassCameraException extends RuntimeException { public class SegpassCameraException extends RuntimeException {
// Optional: You can define a constructor with a message argument
public SegpassCameraException(String message) { public SegpassCameraException(String message) {
super(message); super(message);
} }
// Optional: You can define a constructor with a message and a cause (throwable)
public SegpassCameraException(String message, Throwable cause) { public SegpassCameraException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
// Optional: You can define custom methods specific to your exception
public String getCustomDetails() {
// ... Add logic to return additional details related to the exception
return "This is a custom detail";
}
} }

View file

@ -8,6 +8,7 @@ material = "1.12.0"
activity = "1.8.0" activity = "1.8.0"
constraintlayout = "2.1.4" constraintlayout = "2.1.4"
camerax = "1.3.3" camerax = "1.3.3"
robolectric = "4.8.1"
[libraries] [libraries]
junit = { group = "junit", name = "junit", version.ref = "junit" } junit = { group = "junit", name = "junit", version.ref = "junit" }
@ -22,6 +23,7 @@ camera-camera2 = { group = "androidx.camera", name = "camera-camera2", version.r
camera-lifecycle = { group = "androidx.camera", name = "camera-lifecycle", version.ref = "camerax"} camera-lifecycle = { group = "androidx.camera", name = "camera-lifecycle", version.ref = "camerax"}
camera-view = { group = "androidx.camera", name = "camera-view", version.ref = "camerax"} camera-view = { group = "androidx.camera", name = "camera-view", version.ref = "camerax"}
camera-extensions = { group = "androidx.camera", name = "camera-extensions", version.ref = "camerax"} camera-extensions = { group = "androidx.camera", name = "camera-extensions", version.ref = "camerax"}
robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
[plugins] [plugins]
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }