Skip to content

Commit

Permalink
feat: Add function to move and resize the target rectangle on the cam…
Browse files Browse the repository at this point in the history
…era preview
  • Loading branch information
VincentKobz committed Jun 5, 2024
1 parent d5ec8c0 commit 0ae8aa9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
11 changes: 10 additions & 1 deletion docs/api/camera.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,20 @@ Pauses the camera's capture.
Resumes the camera's capture.
:::

:::{methode} orientationChanged() -> void
:::{method} orientationChanged() -> void

Notifies the view that the orientation of the device has changed by calling
`setDisplayOrientation()` with the correct clockwise rotation of the camera preview, depending on
the device's orientation.

:::{method} setTargetPosition(float y) -> void

Set the top target's vertical position on the preview (y coordinate).
:::

:::{method} setTargetDimension(float width, float height) -> void

Set the target's dimensions on the preview.
:::

:::{method} getLatestSuccessfulScanJpeg() -> byte[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,14 @@ public interface CameraScannerProvider {
* Mainly used for devices that are using CameraBarcodeScanViewV1 API.
*/
public void orientationChanged(View cameraView);

/**
* Set the top position (in y axis) of the target rectangle.
*/
public void setTargetPosition(View cameraView, float y);

/**
* Set the dimension of the target rectangle.
*/
public void setTargetDimension(View cameraView, float width, float height);
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ public void orientationChanged() {
this.proxiedView.orientationChanged();
}

/**
* Set the top position (in y axis) of the target rectangle.
*/
public void setTargetPosition(float y) {
this.proxiedView.setTargetPosition(y);
}

/**
* Set the dimension of the target rectangle.
*/
public void setTargetDimension(float width, float height) {
this.proxiedView.setTargetDimension(width, height);
}

/**
* Get the JPEG data of the image used in the latest successful scan.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,58 @@ protected void computeCropRectangle() {
Log.i(TAG, "The preview surface view is at (top, left, bottom, right) " + camPreviewSurfaceView.getTop() + "," + camPreviewSurfaceView.getLeft() + " - " + camPreviewSurfaceView.getBottom() + "," + camPreviewSurfaceView.getRight());
}

/**
* Set the top position (in y axis) of the target rectangle.
* Automatically refreshes the autofocus zone.
*/
public void setTargetPosition(float y) {
float newTop = y;
float newBottom = y + cropRect.height();

if (newTop < 0 || newBottom > camPreviewSurfaceView.getHeight()) {
Log.i(TAG, "Targeting rectangle moved outside the preview surface, ignoring");
return;
}

cropRect.top = (int) newTop;
cropRect.bottom = (int) newBottom;

final FrameLayout.LayoutParams prms = (LayoutParams) targetView.getLayoutParams();
prms.topMargin = cropRect.top;
targetView.setLayoutParams(prms);
}

/**
* Set the dimensions of the target rectangle.
*/
public void setTargetDimension(float width, float height) {
cropRect.left = (int) (camPreviewSurfaceView.getMeasuredWidth() / 2 - width / 2);
cropRect.right = (int) (camPreviewSurfaceView.getMeasuredWidth() / 2 + width / 2);

int newTop = cropRect.top + cropRect.height() / 2 - (int) (height / 2);

if (newTop < 0) {
newTop = 0;
}

int newBottom = newTop + (int) height;

if (newBottom > camPreviewSurfaceView.getHeight()) {
newBottom = camPreviewSurfaceView.getHeight();
Log.i(TAG, "Dimension of targeting rectangle is too large, ignoring");
}

cropRect.top = newTop;
cropRect.bottom = newBottom;

final FrameLayout.LayoutParams prms = (LayoutParams) targetView.getLayoutParams();
prms.height = (int) height;
prms.width = (int) width;
prms.setMargins(cropRect.left, cropRect.top, 0, 0);

targetView.setLayoutParams(prms);
}

/**
* Adds the targeting view to layout. Must be called after computeCropRectangle was called. Separate from computeCropRectangle
* because: we want to add this view last, in order to put it on top. (and we need to calculate the crop rectangle early).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,22 @@ public void orientationChanged(View cameraView) {
throw new IllegalArgumentException("cameraView must be an instance of CameraBarcodeScanView");
}
}

@Override
public void setTargetPosition(View cameraView, float y) {
if (cameraView instanceof CameraBarcodeScanView) {
((CameraBarcodeScanView) cameraView).setTargetPosition(y);
} else {
throw new IllegalArgumentException("cameraView must be an instance of CameraBarcodeScanView");
}
}

@Override
public void setTargetDimension(View cameraView, float width, float height) {
if (cameraView instanceof CameraBarcodeScanView) {
((CameraBarcodeScanView) cameraView).setTargetDimension(width, height);
} else {
throw new IllegalArgumentException("cameraView must be an instance of CameraBarcodeScanView");
}
}
}

0 comments on commit 0ae8aa9

Please sign in to comment.