Files
sgse-app/apps/web/src/lib/utils/webcam.ts
deyvisonwanderley f0c6e4468f feat: integrate point management features into the dashboard
- Added a new "Meu Ponto" section for users to register their work hours, breaks, and attendance.
- Introduced a "Controle de Ponto" category in the Recursos Humanos section for managing employee time records.
- Enhanced the backend schema to support point registration and configuration settings.
- Updated various components to improve UI consistency and user experience across the dashboard.
2025-11-18 11:44:12 -03:00

151 lines
3.5 KiB
TypeScript

/**
* Verifica se webcam está disponível
*/
export async function validarWebcamDisponivel(): Promise<boolean> {
if (typeof navigator === 'undefined' || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
return false;
}
try {
const devices = await navigator.mediaDevices.enumerateDevices();
return devices.some((device) => device.kind === 'videoinput');
} catch {
return false;
}
}
/**
* Captura imagem da webcam
*/
export async function capturarWebcam(): Promise<Blob | null> {
if (typeof navigator === 'undefined' || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
return null;
}
let stream: MediaStream | null = null;
try {
// Solicitar acesso à webcam
stream = await navigator.mediaDevices.getUserMedia({
video: {
width: { ideal: 1280 },
height: { ideal: 720 },
facingMode: 'user',
},
});
// Criar elemento de vídeo temporário
const video = document.createElement('video');
video.srcObject = stream;
video.play();
// Aguardar vídeo estar pronto
await new Promise<void>((resolve, reject) => {
video.onloadedmetadata = () => {
video.width = video.videoWidth;
video.height = video.videoHeight;
resolve();
};
video.onerror = reject;
setTimeout(() => reject(new Error('Timeout ao carregar vídeo')), 5000);
});
// Capturar frame
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext('2d');
if (!ctx) {
throw new Error('Não foi possível obter contexto do canvas');
}
ctx.drawImage(video, 0, 0);
// Converter para blob
return await new Promise<Blob | null>((resolve) => {
canvas.toBlob(
(blob) => {
resolve(blob);
},
'image/jpeg',
0.9
);
});
} catch (error) {
console.error('Erro ao capturar webcam:', error);
return null;
} finally {
// Parar stream
if (stream) {
stream.getTracks().forEach((track) => track.stop());
}
}
}
/**
* Captura imagem da webcam com preview
*/
export async function capturarWebcamComPreview(
videoElement: HTMLVideoElement,
canvasElement: HTMLCanvasElement
): Promise<Blob | null> {
if (typeof navigator === 'undefined' || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
return null;
}
let stream: MediaStream | null = null;
try {
// Solicitar acesso à webcam
stream = await navigator.mediaDevices.getUserMedia({
video: {
width: { ideal: 1280 },
height: { ideal: 720 },
facingMode: 'user',
},
});
videoElement.srcObject = stream;
await videoElement.play();
// Aguardar vídeo estar pronto
await new Promise<void>((resolve, reject) => {
videoElement.onloadedmetadata = () => {
canvasElement.width = videoElement.videoWidth;
canvasElement.height = videoElement.videoHeight;
resolve();
};
videoElement.onerror = reject;
setTimeout(() => reject(new Error('Timeout ao carregar vídeo')), 5000);
});
// Capturar frame
const ctx = canvasElement.getContext('2d');
if (!ctx) {
throw new Error('Não foi possível obter contexto do canvas');
}
ctx.drawImage(videoElement, 0, 0);
// Converter para blob
return await new Promise<Blob | null>((resolve) => {
canvasElement.toBlob(
(blob) => {
resolve(blob);
},
'image/jpeg',
0.9
);
});
} catch (error) {
console.error('Erro ao capturar webcam:', error);
return null;
} finally {
// Parar stream
if (stream) {
stream.getTracks().forEach((track) => track.stop());
}
}
}