问题修改

This commit is contained in:
hayu 2025-08-04 14:57:23 +08:00
parent 01bed0ed7b
commit dccdf40eac
4 changed files with 333 additions and 1433 deletions

View File

@ -79,14 +79,14 @@ export default {
return; return;
} }
document.addEventListener('deviceready', () => { // document.addEventListener('deviceready', () => {
if (cordova.plugins && cordova.plugins.camerapreview) { // if (cordova.plugins && cordova.plugins.camerapreview) {
window.CameraPreview = cordova.plugins.camerapreview; // window.CameraPreview = cordova.plugins.camerapreview;
this.startCamera(); this.startCamera();
} else { // } else {
console.error('CameraPreview插件未加载'); // console.error('CameraPreview');
} // }
}, false); // }, false);
}, },
// //

View File

@ -21,7 +21,7 @@ export default {
methods: { methods: {
// //
openCamera() { openCamera() {
this.$cordova.plugins.cameraPreview.startCamera({ window.CameraPreview.startCamera({
x: 0, x: 0,
y: 0, y: 0,
width: window.screen.width, width: window.screen.width,

File diff suppressed because it is too large Load Diff

View File

@ -76,10 +76,16 @@
</view> </view>
</view> </view>
<!-- 相机预览页面 - 修改为固定全屏 -->
<!-- 相机预览页面 --> <!-- 相机预览页面 -->
<view v-if="showCamera" class="camera-container"> <view v-if="showCamera" class="camera-wrapper">
<!-- 底部控制区 --> <!-- 相机预览容器 - 单独放置 -->
<view class="bottom-controls"> <view class="camera-preview-container"></view>
<!-- UI控制层 - 独立于相机预览 -->
<view class="camera-ui-layer">
<!-- 底部控制区 -->
<view class="bottom-controls">
<view class="control-btn" @click="closeCamera"> <view class="control-btn" @click="closeCamera">
<text class="control-icon"></text> <text class="control-icon"></text>
</view> </view>
@ -99,12 +105,7 @@
</view> </view>
</view> </view>
</view> </view>
</view>
<!-- 隐藏的canvas用于图片处理 -->
<canvas
canvas-id="imageCanvas"
style="position: fixed; top: -9999px; left: -9999px; width: 1px; height: 1px;"
></canvas>
</view> </view>
</template> </template>
@ -163,25 +164,6 @@ export default {
} }
}, },
// Android
setFullscreenMode(enable) {
try {
// #ifdef APP-PLUS
if (this.systemInfo && this.systemInfo.platform === 'android') {
if (enable) {
//
plus.navigator.setFullscreen(true);
} else {
//
plus.navigator.setFullscreen(false);
}
}
// #endif
} catch (error) {
console.error('设置全屏模式失败:', error);
}
},
// Cordova - // Cordova -
initializeCordova() { initializeCordova() {
console.log('开始初始化Cordova...'); console.log('开始初始化Cordova...');
@ -259,10 +241,7 @@ export default {
getCameraPlugin() { getCameraPlugin() {
// //
const possiblePaths = [ const possiblePaths = [
() => window.CameraPreview, // () => window.CameraPreview //
() => window.cordova?.plugins?.CameraPreview,
() => window.plugins?.CameraPreview,
() => cordova?.plugins?.CameraPreview
]; ];
for (let getPlugin of possiblePaths) { for (let getPlugin of possiblePaths) {
try { try {
@ -311,59 +290,45 @@ export default {
} }
}, },
// - // -
// -
async initCamera() { async initCamera() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log('初始化相机预览...');
const CameraPreview = this.getCameraPlugin(); const CameraPreview = this.getCameraPlugin();
if (!CameraPreview) { if (!CameraPreview) {
reject(new Error('相机插件不可用')); reject(new Error('相机插件不可用'));
return; return;
} }
// //
const systemInfo = this.systemInfo || uni.getSystemInfoSync(); const cameraWrapper = document.querySelector('.camera-wrapper');
const screenWidth = systemInfo.screenWidth; if (!cameraWrapper) {
const screenHeight = systemInfo.screenHeight; reject(new Error('找不到相机容器'));
const statusBarHeight = systemInfo.statusBarHeight || 0; return;
}
// - 使
let cameraY = 0;
let cameraHeight = screenHeight;
let toBack = true; // UI
console.log('屏幕信息:', {
platform: systemInfo.platform,
screenWidth,
screenHeight,
statusBarHeight,
cameraY,
cameraHeight,
toBack
});
const rect = cameraWrapper.getBoundingClientRect();
const options = { const options = {
x: 0, x: rect.left,
y: cameraY, y: rect.top,
width: screenWidth, width: rect.width,
height: cameraHeight, height: rect.height,
camera: CameraPreview.CAMERA_DIRECTION?.BACK || 'back', camera: CameraPreview.CAMERA_DIRECTION?.BACK || 'back',
tapPhoto: false, tapPhoto: false,
previewDrag: false, previewDrag: false,
toBack: toBack, // toBack: false, // WebView
alpha: 1, alpha: 1,
tapFocus: true, // tapFocus: true,
disableExifHeaderStripping: false disableExifHeaderStripping: false
}; };
console.log('相机配置:', options); console.log('相机配置:', options);
CameraPreview.startCamera( CameraPreview.startCamera(
options, options,
(result) => { (result) => {
console.log('相机启动成功:', result); console.log('相机启动成功:', result);
this.cameraStarted = true; this.cameraStarted = true;
//
this.performAutoFocus();
resolve(); resolve();
}, },
(error) => { (error) => {
@ -375,153 +340,27 @@ export default {
}); });
}, },
// //
async performAutoFocus() { handleOverlayClick(event) {
if (!this.cameraStarted || !this.autoFocusEnabled) { //
return; const rect = event.currentTarget.getBoundingClientRect();
} const x = event.touches[0].clientX - rect.left;
const y = event.touches[0].clientY - rect.top;
const CameraPreview = this.getCameraPlugin(); //
if (!CameraPreview || typeof CameraPreview.setFocus !== 'function') { this.tapToFocus(x, y);
console.log('相机插件不支持聚焦功能');
return;
}
try {
this.isFocusing = true;
this.focusSuccess = false;
//
if (this.focusTimeout) {
clearTimeout(this.focusTimeout);
}
console.log('开始自动聚焦...');
await new Promise((resolve, reject)=> {
//
this.focusTimeout = setTimeout(() => {
console.log('聚焦超时');
this.isFocusing = false;
resolve();
}, 3000);
CameraPreview.setFocus(
(result) => {
console.log('自动聚焦成功:', result);
if (this.focusTimeout) {
clearTimeout(this.focusTimeout);
this.focusTimeout = null;
}
this.isFocusing = false;
this.focusSuccess = true;
//
setTimeout(() => {
this.focusSuccess = false;
}, 1000);
resolve();
},
(error) => {
console.error('自动聚焦失败:', error);
if (this.focusTimeout) {
clearTimeout(this.focusTimeout);
this.focusTimeout = null;
}
this.isFocusing = false;
resolve(); // 使
}
);
});
} catch (error) {
console.error('聚焦过程出错:', error);
this.isFocusing = false;
}
}, },
// //
async handleTapToFocus(event) { tapToFocus(x, y) {
if (!this.cameraStarted || this.isFocusing) {
return;
}
const CameraPreview = this.getCameraPlugin(); const CameraPreview = this.getCameraPlugin();
if (!CameraPreview || typeof CameraPreview.tapToFocus !== 'function') { if (CameraPreview && this.cameraStarted) {
console.log('相机插件不支持点击聚焦功能'); CameraPreview.tapToFocus(
return; x,
} y,
() => console.log('聚焦成功'),
try { (error) => console.error('聚焦失败', error)
// );
const rect = event.currentTarget.getBoundingClientRect();
const x = event.detail.x - rect.left;
const y = event.detail.y - rect.top;
// (0-1)
const relativeX = x / rect.width;
const relativeY = y / rect.height;
console.log('点击聚焦位置:', { x: relativeX, y: relativeY });
//
this.showFocusIndicator = true;
this.focusIndicatorStyle = {
left: `${x - 30}rpx`,
top: `${y - 30}rpx`
};
this.isFocusing = true;
this.focusSuccess = false;
//
if (this.focusTimeout) {
clearTimeout(this.focusTimeout);
}
await new Promise((resolve, reject)=> {
//
this.focusTimeout = setTimeout(() => {
console.log('点击聚焦超时');
this.isFocusing = false;
this.showFocusIndicator = false;
resolve();
}, 3000);
CameraPreview.tapToFocus(
relativeX,
relativeY,
(result) => {
console.log('点击聚焦成功:', result);
if (this.focusTimeout) {
clearTimeout(this.focusTimeout);
this.focusTimeout = null;
}
this.isFocusing = false;
this.focusSuccess = true;
//
setTimeout(() => {
this.focusSuccess = false;
this.showFocusIndicator = false;
}, 1000);
resolve();
},
(error) => {
console.error('点击聚焦失败:', error);
if (this.focusTimeout) {
clearTimeout(this.focusTimeout);
this.focusTimeout = null;
}
this.isFocusing = false;
this.showFocusIndicator = false;
resolve(); // 使
}
);
}
)
;
} catch (error) {
console.error('点击聚焦过程出错:', error);
this.isFocusing = false;
this.showFocusIndicator = false;
} }
}, },
@ -900,6 +739,15 @@ export default {
// //
cleanup() { cleanup() {
console.log('清理资源...'); console.log('清理资源...');
if (this.cameraStarted) {
const CameraPreview = this.getCameraPlugin();
if (CameraPreview) {
CameraPreview.setOnView(null); //
CameraPreview.stopCamera();
}
this.cameraStarted = false;
}
// //
if (this.deviceReadyTimeout) { if (this.deviceReadyTimeout) {
clearTimeout(this.deviceReadyTimeout); clearTimeout(this.deviceReadyTimeout);
@ -923,6 +771,37 @@ export default {
</script> </script>
<style> <style>
/* 新增相机包装器样式 */
.camera-wrapper {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10000; /* 基础层级 */
}
/* 相机预览容器 - 无内容,仅用于定位 */
.camera-preview-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1; /* 较低层级 */
}
/* UI控制层 - 显示在相机之上 */
.camera-ui-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 2; /* 高于相机预览 */
pointer-events: none; /* 允许点击穿透到相机 */
}
.page-container { .page-container {
display: flex; display: flex;
height: auto; height: auto;
@ -964,27 +843,43 @@ export default {
/* 相机预览样式 */ /* 相机预览样式 */
.camera-container { .camera-container {
position: fixed; position: relative;
top: 0; width: 100%;
left: 0; height: 70vh; /* 增加高度 */
width: 100vw; background-color: #000;
height: 100vh; overflow: hidden;
background-color: #000000; z-index: 1000; /* 确保在H5层级中 */
z-index: 100000; /* 增加相机预览层级 */
} }
/* 控制区 */ .camera-preview {
.bottom-controls {
position: absolute; position: absolute;
bottom: 100rpx; top: 0;
left: 0; left: 0;
right: 0; width: 100%;
display: flex; height: 100%;
flex-direction: row; z-index: 1; /* 相机预览在底层 */
justify-content: space-between; }
align-items: center;
padding: 0 100rpx; /* 透明覆盖层 - 关键 */
z-index: 100001; /* 底部控制按钮层级 */ .camera-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 2; /* 覆盖层在上层 */
pointer-events: auto; /* 捕获点击事件 */
}
/* 控制按钮区域 - 需要单独设置可点击 */
.bottom-controls {
/* 原有样式保持不变 */
position: absolute;
bottom: 0;
left: 0;
width: 100%;
z-index: 3; /* 控制按钮在最高层 */
pointer-events: auto; /* 启用点击事件 */
} }
.control-btn { .control-btn {
@ -1042,17 +937,15 @@ export default {
background-color: #ccc; background-color: #ccc;
} }
/* 加载层 */
.loading-overlay { .loading-overlay {
position: absolute; position: absolute;
top: 0; top: 0;
left: 0; left: 0;
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.7); z-index: 100; /* 加载层在最高层 */
display: flex; pointer-events: auto; /* 阻止底层交互 */
justify-content: center;
align-items: center;
z-index: 10003; /* 加载层级最高 */
} }
.loading-content { .loading-content {