兼容前置后置摄像头

This commit is contained in:
mzhang93 2026-05-12 15:18:03 +08:00
parent d0bda0d0cd
commit a165eb63c5

View File

@ -168,8 +168,9 @@ async function videoUrlToBlob(videoUrl: string): Promise<Blob> {
return res.blob(); return res.blob();
} }
async function setupNativeRecorder() { type CameraFacing = 'front' | 'rear';
cameraReady.value = false;
function getCameraPreviewLayout() {
const vw = Math.round(window.innerWidth); const vw = Math.round(window.innerWidth);
const vh = Math.round(window.innerHeight); const vh = Math.round(window.innerHeight);
// contain // contain
@ -184,46 +185,58 @@ async function setupNativeRecorder() {
const desiredCenterY = Math.round(vh * RETICLE_CENTER_Y); const desiredCenterY = Math.round(vh * RETICLE_CENTER_Y);
const pyRaw = Math.round(desiredCenterY - ph / 2); const pyRaw = Math.round(desiredCenterY - ph / 2);
const py = Math.max(0, Math.min(vh - ph, pyRaw)); const py = Math.max(0, Math.min(vh - ph, pyRaw));
return { pw, ph, px, py };
}
await CameraPreview.start({ async function setupNativeRecorder() {
parent: PREVIEW_ID, cameraReady.value = false;
toBack: true, const { pw, ph, px, py } = getCameraPreviewLayout();
position: 'front',
enableVideoMode: true, const startPreviewAndRecord = async (position: CameraFacing) => {
disableAudio: true, await CameraPreview.start({
// / parent: PREVIEW_ID,
aspectMode: 'contain', toBack: true,
width: pw, position,
height: ph, enableVideoMode: true,
x: px, disableAudio: true,
y: py, // /
storeToFile: true, aspectMode: 'contain',
rotateWhenOrientationChanged: true, width: pw,
force: true height: ph,
}); x: px,
y: py,
storeToFile: true,
rotateWhenOrientationChanged: true,
force: true
});
await CameraPreview.startRecordVideo({});
};
let usedFacing: CameraFacing = 'front';
try {
await startPreviewAndRecord('front');
} catch (firstErr) {
console.warn('前置摄像头不可用,尝试后置:', firstErr);
try {
await CameraPreview.stop({ force: true });
} catch {
/* ignore */
}
await startPreviewAndRecord('rear');
usedFacing = 'rear';
}
await CameraPreview.startRecordVideo({});
started = true; started = true;
isRecording.value = true; isRecording.value = true;
cameraReady.value = true; cameraReady.value = true;
statusText.value = '前置摄像头录制中'; statusText.value ='前置摄像头录制中'
// //
const onResize = async () => { const onResize = async () => {
if (!Capacitor.isNativePlatform() || released) return; if (!Capacitor.isNativePlatform() || released) return;
try { try {
const vw = Math.round(window.innerWidth); const { pw, ph, px, py } = getCameraPreviewLayout();
const vh = Math.round(window.innerHeight);
let pw = vw;
let ph = Math.round(pw * PREVIEW_ASPECT);
if (ph > vh) {
ph = vh;
pw = Math.round(ph / PREVIEW_ASPECT);
}
const px = Math.round((vw - pw) / 2);
const desiredCenterY = Math.round(vh * RETICLE_CENTER_Y);
const pyRaw = Math.round(desiredCenterY - ph / 2);
const py = Math.max(0, Math.min(vh - ph, pyRaw));
await CameraPreview.setPreviewSize({ await CameraPreview.setPreviewSize({
width: pw, width: pw,
height: ph, height: ph,