Skip to content

Commit

Permalink
fix: Camera wrong orientation on TC-25
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentKobz committed Jun 3, 2024
1 parent 6d1f7be commit e97a44c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1119,37 +1119,17 @@ public void onConfigurationChanged(@NonNull Configuration newConfig) {
View view = findViewById(flashlightViewId).getRootView();

if (switchCamera) {
CharSequence lastScanChar = ((TextView) findViewById(R.id.scanner_text_last_scan)).getText();

String lastScan = lastScanChar.length() != 0 ? Html.toHtml((Spanned) lastScanChar) : "";
boolean showOpenLink = findViewById(openLinkId).getVisibility() == View.VISIBLE;

// Disconnect and reset the camera scanner provider
cameraScannerProvider.disconnect();
cameraScannerProvider.reset();

// Set the content view to the camera layout
setViewContent();

// Reinit the camera
initCamera();
((TextView) findViewById(R.id.scanner_text_last_scan)).setText(Html.fromHtml(lastScan));
Integer cameraViewId = cameraResources.get("camera_view_id");

if (showOpenLink) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(openLinkUrl));
findViewById(openLinkId).setVisibility(View.VISIBLE);
findViewById(openLinkId).setOnClickListener(v -> {
// Pause camera or laser scanner
startActivity(intent);
findViewById(openLinkId).setVisibility(View.GONE);
});
if (cameraViewId != null && cameraScannerProvider != null) {
cameraScannerProvider.orientationChanged(findViewById(cameraViewId));
}
// TODO: we may improve this by not reloading the camera view
//ViewSwitcher.switchCameraOrientation(this, view, cameraResources, orientation == Configuration.ORIENTATION_PORTRAIT);

ViewSwitcher.switchCameraOrientation(this, view, cameraResources, orientation == Configuration.ORIENTATION_PORTRAIT);
} else {
ViewSwitcher.switchLaserOrientation(this, view, flashlightViewId, orientation == Configuration.ORIENTATION_PORTRAIT);
}

resizeScannerLastText((TextView) findViewById(R.id.scanner_text_last_scan));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ public interface CameraScannerProvider {
* Sets the reader engine of the camera scanner.
*/
public void setReaderMode(View cameraView, boolean readerMode);

/**
* Some scanner devices get trouble with the camera when the orientation changes.
* This method should be called when the orientation changes.
* Mainly used for devices that are using CameraBarcodeScanViewV1 API.
*/
public void orientationChanged(View cameraView);
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ public void resetTargetPosition() {
this.proxiedView.resetTargetPosition();
}

/**
* Some scanner devices get trouble with the camera when the orientation changes.
* This method should be called when the orientation changes.
* Mainly used for devices that are using CameraBarcodeScanViewV1 API.
*/
public void orientationChanged() {
this.proxiedView.orientationChanged();
}

/**
* 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 @@ -187,6 +187,8 @@ public void setTorch(boolean value) {

public abstract void resumeCamera();

public abstract void orientationChanged();

/**
* Indicate if the torch mode is handled or not
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,20 @@ void setTorchInternal(boolean value) {
setCameraParameters(prms);
}

/**
* Some scanner devices get trouble with the camera when the orientation changes. This method is called when the orientation changes.
* Used in the CameraBarcodeScanViewV1
*/
@Override
public void orientationChanged() {
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "orientationChanged: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}

this.cam.setDisplayOrientation(getCameraDisplayOrientation());
}

// torch
////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,15 @@ void setTorchInternal(boolean value) {
}
}

/**
* Some scanner devices get trouble with the camera when the orientation changes. This method is called when the orientation changes.
* Used in the CameraBarcodeScanViewV1
*/
@Override
public void orientationChanged() {
// Do nothing;
}

///////////////////////////////////////////////////////////////////////////
// Background thread for handling camera-related events
///////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

if (parent.getCurrentCameraResolution() == null || !respectCameraRatio) {
setMeasuredDimension(parentImposedWidthPx, parentImposedHeightPx);
parent.resetTargetPosition();
} else {
float dataRatio = ((float) parent.getCurrentCameraResolution().x) / parent.getCurrentCameraResolution().y;

Expand All @@ -68,5 +67,6 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension((int) (parentImposedHeightPx * dataRatio), parentImposedHeightPx);
}
}
parent.resetTargetPosition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,13 @@ public void setReaderMode(View cameraView, boolean readerMode) {
throw new IllegalArgumentException("cameraView must be an instance of CameraBarcodeScanView");
}
}

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

0 comments on commit e97a44c

Please sign in to comment.