兼容前置后置摄像头
This commit is contained in:
parent
d0bda0d0cd
commit
a165eb63c5
@ -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,11 +185,18 @@ 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 };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setupNativeRecorder() {
|
||||||
|
cameraReady.value = false;
|
||||||
|
const { pw, ph, px, py } = getCameraPreviewLayout();
|
||||||
|
|
||||||
|
const startPreviewAndRecord = async (position: CameraFacing) => {
|
||||||
await CameraPreview.start({
|
await CameraPreview.start({
|
||||||
parent: PREVIEW_ID,
|
parent: PREVIEW_ID,
|
||||||
toBack: true,
|
toBack: true,
|
||||||
position: 'front',
|
position,
|
||||||
enableVideoMode: true,
|
enableVideoMode: true,
|
||||||
disableAudio: true,
|
disableAudio: true,
|
||||||
// 不铺满:按比例居中显示(可能出现上下/左右留黑边)
|
// 不铺满:按比例居中显示(可能出现上下/左右留黑边)
|
||||||
@ -201,29 +209,34 @@ async function setupNativeRecorder() {
|
|||||||
rotateWhenOrientationChanged: true,
|
rotateWhenOrientationChanged: true,
|
||||||
force: true
|
force: true
|
||||||
});
|
});
|
||||||
|
|
||||||
await CameraPreview.startRecordVideo({});
|
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';
|
||||||
|
}
|
||||||
|
|
||||||
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,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user