feat: auto focus for scan qrcode
This commit is contained in:
@@ -309,6 +309,31 @@ function updateOverlay(result: Result): void {
|
|||||||
drawOverlay();
|
drawOverlay();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 根据摄像头支持的模式启用自动对焦。 */
|
||||||
|
async function enableAutoFocus(video: HTMLVideoElement): Promise<void> {
|
||||||
|
const stream = video.srcObject;
|
||||||
|
if (!(stream instanceof MediaStream)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const track = stream.getVideoTracks()[0];
|
||||||
|
if (!track || typeof track.getCapabilities !== "function") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const capabilities = track.getCapabilities() as MediaTrackCapabilities & { focusMode?: string[] };
|
||||||
|
const focusMode = capabilities.focusMode?.includes("continuous")
|
||||||
|
? "continuous"
|
||||||
|
: capabilities.focusMode?.includes("single-shot") ? "single-shot" : undefined;
|
||||||
|
if (focusMode) {
|
||||||
|
await track.applyConstraints({ advanced: [{ focusMode } as MediaTrackConstraintSet] });
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// 不支持对焦控制时继续使用摄像头默认设置。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function startScanner(): Promise<void> {
|
async function startScanner(): Promise<void> {
|
||||||
if (!props.visible) {
|
if (!props.visible) {
|
||||||
return;
|
return;
|
||||||
@@ -329,6 +354,7 @@ async function startScanner(): Promise<void> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const video = scannerVideoRef.value;
|
||||||
syncCanvasSize();
|
syncCanvasSize();
|
||||||
try {
|
try {
|
||||||
scannerControls.value = await scannerReader.decodeFromConstraints({
|
scannerControls.value = await scannerReader.decodeFromConstraints({
|
||||||
@@ -338,7 +364,7 @@ async function startScanner(): Promise<void> {
|
|||||||
ideal: "environment"
|
ideal: "environment"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, scannerVideoRef.value, (result, error) => {
|
}, video, (result, error) => {
|
||||||
if (result) {
|
if (result) {
|
||||||
updateOverlay(result);
|
updateOverlay(result);
|
||||||
clearOverlay(900);
|
clearOverlay(900);
|
||||||
@@ -362,6 +388,7 @@ async function startScanner(): Promise<void> {
|
|||||||
clearOverlay();
|
clearOverlay();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
await enableAutoFocus(video);
|
||||||
} catch {
|
} catch {
|
||||||
updateStatus(props.permissionDeniedText);
|
updateStatus(props.permissionDeniedText);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user