Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Fixing Camera Stretch #1365

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/mlkit/mlkit-cameraview.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
parameters.setPreviewFormat(android.graphics.ImageFormat.NV21);

this.setRotation(this.camera, parameters, requestedCameraId);
this.fixStretch(previewSize.width, previewSize.height);

if (parameters.getSupportedFocusModes().contains(android.hardware.Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
parameters.setFocusMode(android.hardware.Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
Expand Down Expand Up @@ -240,6 +241,36 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
}, 500);
}

private fixStretch(width, height): void {
let measuredWidth = this.surfaceView.getMeasuredWidth();
let measuredHeight = this.surfaceView.getMeasuredHeight();

let scale = width/height;
let invertedScale = height/width;
let measuredScale = measuredWidth/measuredHeight;

let scaleX = 1, scaleY = 1;
if( this.rotation == 1 || this.rotation == 3 ){
if( measuredScale <= scale ){
scaleY = (measuredWidth * scale) / measuredHeight;
}
else{
scaleX = (measuredHeight * scale) / measuredWidth;
}
}
else{
if( measuredScale >= invertedScale ){
scaleY = (measuredWidth * invertedScale) / measuredHeight;
}
else{
scaleX = (measuredHeight * invertedScale) / measuredWidth;
}
}

this.surfaceView.setScaleX( scaleX );
this.surfaceView.setScaleY( scaleY );
}

protected updateTorch(): void {
if (this.camera) {
const parameters = this.camera.getParameters();
Expand Down