Skip to content

Commit

Permalink
refactor android part(camera selector) and capture device value into …
Browse files Browse the repository at this point in the history
…consideration when enabling camera
  • Loading branch information
parveshneedhoo committed Apr 4, 2024
1 parent 694e6ce commit 9705d29
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 81 deletions.
163 changes: 82 additions & 81 deletions src/android/CameraPreviewFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public class CameraPreviewFragment extends Fragment {

private static float ratio = (4 / (float) 3);
private static final String TAG = "SimpleCameraPreview";
private float minZoomRatio;
private float maxZoomRatio;
private String captureDevice;

public CameraPreviewFragment() {

Expand All @@ -100,6 +99,11 @@ public CameraPreviewFragment(int cameraDirection, CameraStartedCallback cameraSt
} catch (JSONException e) {
e.printStackTrace();
}
try {
this.captureDevice = options.getString("captureDevice");
} catch (JSONException e) {
e.printStackTrace();
}
startCameraCallback = cameraStartedCallback;
}

Expand Down Expand Up @@ -132,10 +136,7 @@ public void startCamera() {
startCameraCallback.onCameraStarted(new Exception("Unable to start camera"));
return;
}

CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(direction)
.build();
CameraSelector cameraSelector = getCameraSelector(captureDevice);

Size targetResolution = null;
if (targetSize > 0) {
Expand Down Expand Up @@ -372,88 +373,88 @@ public void setLocation(Location loc) {

public void switchCameraTo(String device, CameraSwitchedCallback cameraSwitchedCallback) {
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
@SuppressLint("RestrictedApi")
@Override
public void run() {
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(getActivity());
ProcessCameraProvider cameraProvider = null;
CameraSelector cameraSelector;
try {
cameraProvider = cameraProviderFuture.get();
} catch (ExecutionException | InterruptedException e) {
Log.e(TAG, "Error occurred while trying to obtain the camera provider: " + e.getMessage());
e.printStackTrace();
cameraSwitchedCallback.onSwitch(false);
return;
}

if (device.equals("ultra-wide-angle")) {
cameraSelector = new CameraSelector.Builder()
.addCameraFilter(cameraInfos -> {
List<Camera2CameraInfoImpl> backCameras = new ArrayList<>();
for (CameraInfo cameraInfo : cameraInfos) {
if (cameraInfo instanceof Camera2CameraInfoImpl) {
Camera2CameraInfoImpl camera2CameraInfo = (Camera2CameraInfoImpl) cameraInfo;
if (camera2CameraInfo.getLensFacing() == CameraSelector.LENS_FACING_BACK) {
backCameras.add(camera2CameraInfo);
}
}
}
mainHandler.post(() -> {
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(getActivity());
ProcessCameraProvider cameraProvider = null;
try {
cameraProvider = cameraProviderFuture.get();
} catch (ExecutionException | InterruptedException e) {
Log.e(TAG, "Error occurred while trying to obtain the camera provider: " + e.getMessage());
e.printStackTrace();
cameraSwitchedCallback.onSwitch(false);
return;
}

Camera2CameraInfoImpl selectedCamera = Collections.min(backCameras, (o1, o2) -> {
Float focalLength1 = o1.getCameraCharacteristicsCompat().get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS)[0];
Float focalLength2 = o2.getCameraCharacteristicsCompat().get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS)[0];
return Float.compare(focalLength1, focalLength2);
});
CameraSelector cameraSelector = getCameraSelector(device);

if (selectedCamera != null) {
return Collections.singletonList(selectedCamera);
} else {
return cameraInfos;
}
})
.build();
} else {
cameraSelector = new CameraSelector.Builder()
.requireLensFacing(direction)
.build();
}
Size targetResolution = null;
if (targetSize > 0) {
targetResolution = CameraPreviewFragment.calculateResolution(getContext(), targetSize);
}

Size targetResolution = null;
if (targetSize > 0) {
targetResolution = CameraPreviewFragment.calculateResolution(getContext(), targetSize);
}
preview = new Preview.Builder().build();
imageCapture = new ImageCapture.Builder()
.setTargetResolution(targetResolution)
.build();

preview = new Preview.Builder().build();
cameraProvider.unbindAll();
try {
camera = cameraProvider.bindToLifecycle(
getActivity(),
cameraSelector,
preview,
imageCapture
);
} catch (IllegalArgumentException e) {
// Error with result in capturing image with default resolution
e.printStackTrace();
imageCapture = new ImageCapture.Builder()
.setTargetResolution(targetResolution)
.build();

cameraProvider.unbindAll();
try {
camera = cameraProvider.bindToLifecycle(
getActivity(),
cameraSelector,
preview,
imageCapture
);
} catch (IllegalArgumentException e) {
// Error with result in capturing image with default resolution
e.printStackTrace();
imageCapture = new ImageCapture.Builder()
.build();
camera = cameraProvider.bindToLifecycle(
getActivity(),
cameraSelector,
preview,
imageCapture
);
}

preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
cameraSwitchedCallback.onSwitch(true);
camera = cameraProvider.bindToLifecycle(
getActivity(),
cameraSelector,
preview,
imageCapture
);
}

preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
cameraSwitchedCallback.onSwitch(true);
});
}

@SuppressLint("RestrictedApi")
public CameraSelector getCameraSelector(String captureDevice) {
if (captureDevice.equals("ultra-wide-angle")) {
return new CameraSelector.Builder()
.addCameraFilter(cameraInfos -> {
List<Camera2CameraInfoImpl> backCameras = new ArrayList<>();
for (CameraInfo cameraInfo : cameraInfos) {
if (cameraInfo instanceof Camera2CameraInfoImpl) {
Camera2CameraInfoImpl camera2CameraInfo = (Camera2CameraInfoImpl) cameraInfo;
if (camera2CameraInfo.getLensFacing() == CameraSelector.LENS_FACING_BACK) {
backCameras.add(camera2CameraInfo);
}
}
}

Camera2CameraInfoImpl selectedCamera = Collections.min(backCameras, (o1, o2) -> {
Float focalLength1 = o1.getCameraCharacteristicsCompat().get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS)[0];
Float focalLength2 = o2.getCameraCharacteristicsCompat().get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS)[0];
return Float.compare(focalLength1, focalLength2);
});

if (selectedCamera != null) {
return Collections.singletonList(selectedCamera);
} else {
return cameraInfos;
}
})
.build();
} else {
return new CameraSelector.Builder()
.requireLensFacing(direction)
.build();
}
}
}
15 changes: 15 additions & 0 deletions src/android/SimpleCameraPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,28 @@ private boolean enable(JSONObject options, CallbackContext callbackContext) {
e.printStackTrace();
}

String captureDevice = "";
try {
if (options.getString("captureDevice") != null && !options.getString("captureDevice").equals("null")) {
captureDevice = options.getString("captureDevice");
}
} catch (JSONException | NumberFormatException e) {
e.printStackTrace();
}

JSONObject cameraPreviewOptions = new JSONObject();
try {
cameraPreviewOptions.put("targetSize", targetSize);
} catch (JSONException e) {
e.printStackTrace();
}

try {
cameraPreviewOptions.put("captureDevice", captureDevice);
} catch (JSONException e) {
e.printStackTrace();
}

fragment = new CameraPreviewFragment(cameraDirection, (err) -> {
if (err != null) {
callbackContext.error(err.getMessage());
Expand Down

0 comments on commit 9705d29

Please sign in to comment.