WEB-476-fix(clients): preserve aspect ratio when capturing client image (#2852)
* fix(clients): preserve aspect ratio when capturing client image * Remove aspect-ratio and change object-fit: cover to object-fit: contain
This commit is contained in:
+1
-1
@@ -1,7 +1,7 @@
|
||||
<div class="layout-column gap-2px">
|
||||
<h1 mat-dialog-title align="center">{{ 'labels.heading.Capture Client Image' | translate }}</h1>
|
||||
|
||||
<video #video width="540" height="480" autoplay></video>
|
||||
<video #video class="capture-video" autoplay></video>
|
||||
|
||||
<!-- Using a class will break renderer changes -->
|
||||
<canvas #canvas [ngStyle]="{ display: 'none' }"></canvas>
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
@use '../../../../../assets/styles/colours' as colors;
|
||||
|
||||
.capture-video {
|
||||
width: 100%;
|
||||
max-width: 640px;
|
||||
height: auto;
|
||||
object-fit: contain;
|
||||
border-radius: 4px;
|
||||
background-color: colors.$black;
|
||||
}
|
||||
|
||||
+21
-3
@@ -60,8 +60,13 @@ export class CaptureImageDialogComponent implements AfterViewInit, OnDestroy {
|
||||
*/
|
||||
startCamera() {
|
||||
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
||||
const videoConstraints: MediaTrackConstraints = {
|
||||
width: { ideal: 640 },
|
||||
height: { ideal: 480 },
|
||||
facingMode: 'user'
|
||||
};
|
||||
navigator.mediaDevices
|
||||
.getUserMedia({ video: true })
|
||||
.getUserMedia({ video: videoConstraints })
|
||||
.then((stream: MediaStream) => {
|
||||
this.renderer.setProperty(this.video.nativeElement, 'srcObject', stream);
|
||||
this.video.nativeElement.play();
|
||||
@@ -101,13 +106,26 @@ export class CaptureImageDialogComponent implements AfterViewInit, OnDestroy {
|
||||
|
||||
/**
|
||||
* Captures the image by drawing video state on canvas, then converts canvas state to data URL.
|
||||
* Uses actual video dimensions to prevent aspect ratio distortion.
|
||||
* See https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL for details.
|
||||
*/
|
||||
capture() {
|
||||
this.isCaptured = true;
|
||||
this.video.nativeElement.pause();
|
||||
this.canvas.nativeElement.getContext('2d').drawImage(this.video.nativeElement, 0, 0, 150, 150);
|
||||
this.clientImageDataURL = this.canvas.nativeElement.toDataURL();
|
||||
|
||||
// Get actual video stream dimensions to preserve aspect ratio
|
||||
const videoWidth = this.video.nativeElement.videoWidth;
|
||||
const videoHeight = this.video.nativeElement.videoHeight;
|
||||
|
||||
// Set canvas dimensions to match video stream
|
||||
this.canvas.nativeElement.width = videoWidth;
|
||||
this.canvas.nativeElement.height = videoHeight;
|
||||
|
||||
// Draw the video frame at full resolution without distortion
|
||||
this.canvas.nativeElement.getContext('2d').drawImage(this.video.nativeElement, 0, 0, videoWidth, videoHeight);
|
||||
|
||||
// Convert to data URL with JPEG format for smaller file size
|
||||
this.clientImageDataURL = this.canvas.nativeElement.toDataURL('image/jpeg', 0.9);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user