兼容前置后置摄像头

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();
}
async function setupNativeRecorder() {
cameraReady.value = false;
type CameraFacing = 'front' | 'rear';
function getCameraPreviewLayout() {
const vw = Math.round(window.innerWidth);
const vh = Math.round(window.innerHeight);
// contain
@ -184,46 +185,58 @@ async function setupNativeRecorder() {
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));
return { pw, ph, px, py };
}
await CameraPreview.start({
parent: PREVIEW_ID,
toBack: true,
position: 'front',
enableVideoMode: true,
disableAudio: true,
// /
aspectMode: 'contain',
width: pw,
height: ph,
x: px,
y: py,
storeToFile: true,
rotateWhenOrientationChanged: true,
force: true
});
async function setupNativeRecorder() {
cameraReady.value = false;
const { pw, ph, px, py } = getCameraPreviewLayout();
const startPreviewAndRecord = async (position: CameraFacing) => {
await CameraPreview.start({
parent: PREVIEW_ID,
toBack: true,
position,
enableVideoMode: true,
disableAudio: true,
// /
aspectMode: 'contain',
width: pw,
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;
isRecording.value = true;
cameraReady.value = true;
statusText.value = '前置摄像头录制中';
statusText.value ='前置摄像头录制中'
//
const onResize = async () => {
if (!Capacitor.isNativePlatform() || released) return;
try {
const vw = Math.round(window.innerWidth);
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));
const { pw, ph, px, py } = getCameraPreviewLayout();
await CameraPreview.setPreviewSize({
width: pw,
height: ph,