ocr
This commit is contained in:
parent
19432eb323
commit
1a9c571d9a
|
|
@ -75,14 +75,12 @@
|
|||
</uni-forms>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 相机预览页面 -->
|
||||
<view v-if="showCamera" class="camera-container">
|
||||
<!-- 顶部提示 -->
|
||||
<view class="top-tip">
|
||||
<text class="tip-text">请将识别编码置于取景框内,完成扫描</text>
|
||||
</view>
|
||||
|
||||
<!-- 取景框 -->
|
||||
<view class="viewfinder-container">
|
||||
<view class="viewfinder">
|
||||
|
|
@ -92,7 +90,6 @@
|
|||
<view class="corner corner-bottom-right"></view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 底部控制区 -->
|
||||
<view class="bottom-controls">
|
||||
<view class="control-btn" @click="closeCamera">
|
||||
|
|
@ -105,7 +102,6 @@
|
|||
<text class="control-icon">📷</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 加载提示 -->
|
||||
<view v-if="isProcessing" class="loading-overlay">
|
||||
<view class="loading-content">
|
||||
|
|
@ -118,6 +114,9 @@
|
|||
</template>
|
||||
|
||||
<script>
|
||||
import uni from 'uni-app';
|
||||
import cordova from 'cordova';
|
||||
import plus from 'plus';
|
||||
|
||||
export default {
|
||||
data() {
|
||||
|
|
@ -134,34 +133,33 @@ export default {
|
|||
codeData: {},
|
||||
optionList: [],
|
||||
cameraReady: false,
|
||||
deviceReadyTimeout: null
|
||||
deviceReadyTimeout: null,
|
||||
pluginCheckRetries: 0,
|
||||
maxRetries: 10
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.initializeCordova();
|
||||
},
|
||||
|
||||
onHide() {
|
||||
this.cleanup();
|
||||
},
|
||||
|
||||
beforeDestroy() {
|
||||
this.cleanup();
|
||||
},
|
||||
|
||||
methods: {
|
||||
// 初始化Cordova
|
||||
// 初始化Cordova - 改进版本
|
||||
initializeCordova() {
|
||||
console.log('开始初始化Cordova...');
|
||||
this.pluginCheckRetries = 0;
|
||||
|
||||
// 清除之前的超时
|
||||
if (this.deviceReadyTimeout) {
|
||||
clearTimeout(this.deviceReadyTimeout);
|
||||
}
|
||||
|
||||
// 检查是否已经准备好
|
||||
if (window.cordova && document.readyState === 'complete') {
|
||||
// 如果Cordova已经准备好,直接检查插件
|
||||
if (this.isCordovaReady()) {
|
||||
this.onDeviceReady();
|
||||
return;
|
||||
}
|
||||
|
|
@ -171,9 +169,17 @@ export default {
|
|||
|
||||
// 设置超时,防止无限等待
|
||||
this.deviceReadyTimeout = setTimeout(() => {
|
||||
console.warn('Cordova初始化超时');
|
||||
this.cameraReady = false;
|
||||
}, 10000);
|
||||
console.warn('Cordova初始化超时,尝试直接检查插件');
|
||||
this.checkCameraPlugin();
|
||||
}, 5000);
|
||||
},
|
||||
|
||||
// 检查Cordova是否准备好
|
||||
isCordovaReady() {
|
||||
return !!(window.cordova && (
|
||||
document.readyState === 'complete' ||
|
||||
window.cordova.platformId
|
||||
));
|
||||
},
|
||||
|
||||
// 设备准备就绪
|
||||
|
|
@ -186,96 +192,141 @@ export default {
|
|||
this.deviceReadyTimeout = null;
|
||||
}
|
||||
|
||||
// 检查相机插件是否可用
|
||||
// 移除事件监听器,避免重复调用
|
||||
document.removeEventListener('deviceready', this.onDeviceReady);
|
||||
|
||||
// 检查相机插件
|
||||
this.checkCameraPlugin();
|
||||
},
|
||||
|
||||
// 检查相机插件
|
||||
// 检查相机插件 - 改进版本
|
||||
checkCameraPlugin() {
|
||||
console.log('检查相机插件...');
|
||||
console.log(`检查相机插件... (尝试 ${this.pluginCheckRetries + 1}/${this.maxRetries})`);
|
||||
|
||||
// 多种方式检查插件
|
||||
const pluginAvailable =
|
||||
(window.cordova && window.cordova.plugins && window.cordova.plugins.CameraPreview) ||
|
||||
(window.CameraPreview) ||
|
||||
(window.plugins && window.plugins.CameraPreview);
|
||||
// 按优先级检查插件可用性
|
||||
const pluginAvailable = this.getCameraPlugin();
|
||||
|
||||
if (pluginAvailable) {
|
||||
console.log('相机插件可用');
|
||||
console.log('相机插件可用:', pluginAvailable);
|
||||
this.cameraReady = true;
|
||||
} else {
|
||||
console.error('相机插件不可用');
|
||||
this.cameraReady = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// 延迟重试
|
||||
this.pluginCheckRetries++;
|
||||
|
||||
if (this.pluginCheckRetries < this.maxRetries) {
|
||||
// 递增延迟重试
|
||||
const delay = Math.min(1000 * this.pluginCheckRetries, 5000);
|
||||
console.log(`插件未就绪,${delay}ms后重试...`);
|
||||
setTimeout(() => {
|
||||
this.checkCameraPlugin();
|
||||
}, 1000);
|
||||
}, delay);
|
||||
} else {
|
||||
console.error('相机插件检查失败,已达到最大重试次数');
|
||||
this.cameraReady = false;
|
||||
uni.showToast({
|
||||
title: '相机插件初始化失败',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 获取相机插件引用
|
||||
// 获取相机插件引用 - 改进版本
|
||||
getCameraPlugin() {
|
||||
return window.cordova?.plugins?.CameraPreview ||
|
||||
window.CameraPreview ||
|
||||
window.plugins?.CameraPreview;
|
||||
// 按照插件注册的实际路径检查
|
||||
const possiblePaths = [
|
||||
() => window.CameraPreview, // 全局注册的路径
|
||||
() => window.cordova?.plugins?.CameraPreview,
|
||||
() => window.plugins?.CameraPreview,
|
||||
() => cordova?.plugins?.CameraPreview
|
||||
];
|
||||
|
||||
for (let getPlugin of possiblePaths) {
|
||||
try {
|
||||
const plugin = getPlugin();
|
||||
if (plugin && typeof plugin.startCamera === 'function') {
|
||||
console.log('找到相机插件:', plugin);
|
||||
return plugin;
|
||||
}
|
||||
} catch (e) {
|
||||
// 忽略访问错误,继续尝试下一个路径
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
// 检查相机权限
|
||||
// 检查相机权限 - 改进版本
|
||||
async checkCameraPermission() {
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise((resolve) => {
|
||||
// 检查权限插件是否可用
|
||||
if (!window.cordova?.plugins?.permissions) {
|
||||
const permissions = window.cordova?.plugins?.permissions;
|
||||
|
||||
if (!permissions) {
|
||||
console.warn('权限插件不可用,跳过权限检查');
|
||||
resolve(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const permissions = window.cordova.plugins.permissions;
|
||||
|
||||
permissions.checkPermission(
|
||||
permissions.CAMERA,
|
||||
(status) => {
|
||||
if (status.hasPermission) {
|
||||
console.log('相机权限已授予');
|
||||
resolve(true);
|
||||
} else {
|
||||
// 请求权限
|
||||
console.log('请求相机权限...');
|
||||
permissions.requestPermission(
|
||||
permissions.CAMERA,
|
||||
(status) => {
|
||||
console.log('权限请求结果:', status.hasPermission);
|
||||
resolve(status.hasPermission);
|
||||
},
|
||||
(error) => {
|
||||
console.error('请求权限失败:', error);
|
||||
reject(new Error('请求权限失败: ' + error));
|
||||
resolve(false);
|
||||
}
|
||||
);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
console.error('检查权限失败:', error);
|
||||
reject(new Error('检查权限失败: ' + error));
|
||||
resolve(true); // 权限检查失败时假设有权限
|
||||
}
|
||||
);
|
||||
});
|
||||
},
|
||||
|
||||
// 打开相机
|
||||
// 打开相机 - 改进版本
|
||||
async openCamera() {
|
||||
console.log('尝试打开相机...');
|
||||
|
||||
if (!this.cameraReady) {
|
||||
uni.showToast({
|
||||
title: '相机插件未准备好,请稍后重试',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
return;
|
||||
// 再次尝试检查插件
|
||||
this.checkCameraPlugin();
|
||||
|
||||
if (!this.cameraReady) {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '相机插件未准备好,请确保应用已正确安装相机插件,或尝试重启应用',
|
||||
showCancel: false
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查权限
|
||||
await this.checkCameraPermission();
|
||||
const hasPermission = await this.checkCameraPermission();
|
||||
if (!hasPermission) {
|
||||
uni.showToast({
|
||||
title: '需要相机权限才能使用此功能',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示相机界面
|
||||
this.showCamera = true;
|
||||
|
|
@ -297,28 +348,32 @@ export default {
|
|||
}
|
||||
},
|
||||
|
||||
// 初始化相机
|
||||
// 初始化相机 - 改进版本
|
||||
async initCamera() {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log('初始化相机预览...');
|
||||
|
||||
const CameraPreview = this.getCameraPlugin();
|
||||
|
||||
if (!CameraPreview) {
|
||||
reject(new Error('相机插件不可用'));
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取屏幕尺寸
|
||||
const screenWidth = uni.getSystemInfoSync().screenWidth;
|
||||
const screenHeight = uni.getSystemInfoSync().screenHeight;
|
||||
|
||||
const options = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: window.innerWidth || 375,
|
||||
height: window.innerHeight || 667,
|
||||
camera: 'rear',
|
||||
camera: CameraPreview.CAMERA_DIRECTION?.BACK || 'back',
|
||||
tapPhoto: false,
|
||||
previewDrag: false,
|
||||
toBack: true,
|
||||
alpha: 1
|
||||
toBack: false, // 改为 false,让相机显示在前面
|
||||
alpha: 1,
|
||||
tapFocus: true
|
||||
};
|
||||
|
||||
console.log('相机配置:', options);
|
||||
|
|
@ -333,7 +388,7 @@ export default {
|
|||
(error) => {
|
||||
console.error('相机启动失败:', error);
|
||||
this.cameraStarted = false;
|
||||
reject(new Error('相机启动失败: ' + error));
|
||||
reject(new Error(`相机启动失败: ${error}`));
|
||||
}
|
||||
);
|
||||
});
|
||||
|
|
@ -342,7 +397,6 @@ export default {
|
|||
// 关闭相机
|
||||
async closeCamera() {
|
||||
console.log('关闭相机...');
|
||||
|
||||
try {
|
||||
await this.stopCamera();
|
||||
} catch (error) {
|
||||
|
|
@ -360,7 +414,6 @@ export default {
|
|||
}
|
||||
|
||||
const CameraPreview = this.getCameraPlugin();
|
||||
|
||||
if (!CameraPreview) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
|
@ -392,7 +445,6 @@ export default {
|
|||
|
||||
try {
|
||||
const CameraPreview = this.getCameraPlugin();
|
||||
|
||||
if (!CameraPreview) {
|
||||
throw new Error('相机插件不可用');
|
||||
}
|
||||
|
|
@ -412,7 +464,7 @@ export default {
|
|||
},
|
||||
(error) => {
|
||||
console.error('拍照失败:', error);
|
||||
reject(new Error('拍照失败: ' + error));
|
||||
reject(new Error(`拍照失败: ${error}`));
|
||||
}
|
||||
);
|
||||
});
|
||||
|
|
@ -433,10 +485,8 @@ export default {
|
|||
// 处理图片
|
||||
async processImage(imageData) {
|
||||
this.isProcessing = true;
|
||||
|
||||
try {
|
||||
console.log('开始OCR识别...');
|
||||
|
||||
const response = await new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: '/material/app/ocr/getOcrCode',
|
||||
|
|
@ -466,7 +516,6 @@ export default {
|
|||
icon: 'none'
|
||||
});
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('OCR识别失败:', error);
|
||||
uni.showToast({
|
||||
|
|
@ -511,7 +560,6 @@ export default {
|
|||
});
|
||||
});
|
||||
// #endif
|
||||
|
||||
// #ifndef APP-PLUS
|
||||
this.processImage(filePath);
|
||||
// #endif
|
||||
|
|
@ -663,8 +711,8 @@ export default {
|
|||
left: 0;
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
background-color: #000;
|
||||
z-index: 9999;
|
||||
background-color: rgba(0, 0, 0, 0.8); /* 添加半透明背景 */
|
||||
z-index: 99999; /* 提高 z-index */
|
||||
}
|
||||
|
||||
.top-tip {
|
||||
|
|
|
|||
Loading…
Reference in New Issue