Skip to content

Commit

Permalink
android zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
HashirRajah committed Mar 12, 2024
1 parent 30bbf55 commit 4e55608
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/android/CameraPreviewFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ interface HasFlashCallback {
void onResult(boolean result);
}

interface ZoomCallback {
void onZoom(Exception err);
}

public class CameraPreviewFragment extends Fragment {

private PreviewView viewFinder;
Expand All @@ -69,6 +73,8 @@ public class CameraPreviewFragment extends Fragment {

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

public CameraPreviewFragment() {

Expand Down Expand Up @@ -148,6 +154,9 @@ public void startCamera() {
preview,
imageCapture
);
} finally {
minZoomRatio = camera.getCameraInfo().getZoomState().getValue().getMinZoomRatio();
maxZoomRatio = camera.getCameraInfo().getZoomState().getValue().getMaxZoomRatio();
}

preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
Expand Down Expand Up @@ -309,4 +318,20 @@ public void setLocation(Location loc) {
this.location = loc;
}
}

public float getMinZoomRatio() {
return minZoomRatio;
}

public void setZoomRatio(float zoomRatio, ZoomCallback zoomCallback) {
if ((zoomRatio < minZoomRatio) || (zoomRatio > maxZoomRatio)) {
zoomCallback.onZoom(new Exception("Unsupported zoom ratio"));
return;
}
try {
camera.getCameraControl().setZoomRatio(zoomRatio);
} catch (Exception e) {
zoomCallback.onZoom(new Exception("Failed to set zoom ratio to " + zoomRatio, e));
}
}
}
40 changes: 40 additions & 0 deletions src/android/SimpleCameraPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo

case "deviceHasFlash":
return deviceHasFlash(callbackContext);

case "getMinZoomRatio":
return getMinZoomRatio(callbackContext);

case "setZoomRatio":
return setZoomRatio(Double.valueOf(args.getDouble(0)).floatValue(), callbackContext);
default:
break;
}
Expand Down Expand Up @@ -323,6 +329,39 @@ public void run() {
}
}

private boolean getMinZoomRatio(CallbackContext callbackContext) {
if (fragment == null) {
callbackContext.error("Camera is closed, cannot get minimum zoom ratio.");
return true;
}
try {
float minZoomRatio = fragment.getMinZoomRatio();
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, minZoomRatio);
callbackContext.sendPluginResult(pluginResult);
return true;
} catch (Exception e) {
e.printStackTrace();
callbackContext.error(e.getMessage());
return false;
}
}

private boolean setZoomRatio(float zoomRatio, CallbackContext callbackContext) {
if (fragment == null) {
callbackContext.error("Camera is closed, cannot set zoom ratio");
return true;
}

fragment.setZoom(zoomRatio, (Exception err) -> {
if (err == null) {
callbackContext.success();
} else {
callbackContext.error(err.getMessage());
}
});
return true;
}

public boolean hasAllPermissions() {
for(String p : REQUIRED_PERMISSIONS) {
if(!PermissionHelper.hasPermission(this, p)) {
Expand Down Expand Up @@ -395,6 +434,7 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int
}
}
}


@Override
public void onDestroy() {
Expand Down
10 changes: 10 additions & 0 deletions www/SimpleCameraPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ SimpleCameraPreview.deviceHasFlash = function (onSuccess, onError) {
exec(onSuccess, onError, PLUGIN_NAME, "deviceHasFlash", []);
};

SimpleCameraPreview.getMinZoomRatio = function (onSuccess, onError) {
exec(onSuccess, onError, PLUGIN_NAME, "getMinZoomRatio", []);
};

SimpleCameraPreview.setZoomRatio = function (options, onSuccess, onError) {
options = options || {};
options.zoomRatio = options.zoomRatio || 1;
exec(onSuccess, onError, PLUGIN_NAME, "setZoomRatio", [options.zoomRatio]);
};

module.exports = SimpleCameraPreview;

0 comments on commit 4e55608

Please sign in to comment.