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