This commit is contained in:
hayu 2025-07-31 15:00:47 +08:00
parent b5958272e2
commit e8ccc952b8
1 changed files with 50 additions and 30 deletions

View File

@ -208,39 +208,59 @@ export default {
// 初始化相机
async initCamera() {
return new Promise((resolve, reject) => {
// 检查插件是否可用
if (!window.CameraPreview) {
reject(new Error('相机插件未加载'));
return;
}
const options = {
x: 0,
y: 0,
width: window.innerWidth || 375,
height: window.innerHeight || 667,
camera: 'rear', // 后置摄像头
tapPhoto: false,
previewDrag: false,
toBack: true, // 将预览放到背景
alpha: 1,
colorEffect: 'none'
};
// 获取相机预览插件的引用
var CameraPreview = cordova.plugins.CameraPreview;
window.CameraPreview.startCamera(
options,
() => {
console.log('相机启动成功');
this.cameraStarted = true;
resolve();
},
(error) => {
console.error('相机启动失败:', error);
reject(new Error('相机启动失败: ' + error));
}
);
// 初始化相机预览并设置预览的容器ID
CameraPreview.startCamera({
x: 0,
y: 0,
width: window.innerWidth,
height: window.innerHeight,
camera: 'rear', // 'front' 或 'rear'
tapPhoto: true, // 点击拍照
previewDrag: false, // 是否允许拖动预览框
toBack: false, // 是否将相机预览置于后台(例如,在其他视图之上)
alpha: 255 // 透明度范围0-255
}, function(state) {
console.log(state); // 'started' 或 'failed'
});
// return new Promise((resolve, reject) => {
// // 检查插件是否可用
// if (!window.CameraPreview) {
// reject(new Error('相机插件未加载'));
// return;
// }
//
// const options = {
// x: 0,
// y: 0,
// width: window.innerWidth || 375,
// height: window.innerHeight || 667,
// camera: 'rear', // 后置摄像头
// tapPhoto: false,
// previewDrag: false,
// toBack: true, // 将预览放到背景
// alpha: 1,
// colorEffect: 'none'
// };
//
// window.CameraPreview.startCamera(
// options,
// () => {
// console.log('相机启动成功');
// this.cameraStarted = true;
// resolve();
// },
// (error) => {
// console.error('相机启动失败:', error);
// reject(new Error('相机启动失败: ' + error));
// }
// );
// });
},
// 关闭相机