问题修改

This commit is contained in:
hayu 2025-08-05 19:43:26 +08:00
parent e2046edff2
commit 4378116cff
2 changed files with 187 additions and 54 deletions

View File

@ -445,7 +445,7 @@ export default {
//
console.log('拍照前聚焦...');
console.log('开始拍照...');
CameraPreview.takePicture({width:640, height:640, quality: 50}, async (base64PictureData) => {
CameraPreview.takePicture({width:640, height:540, quality: 50}, async (base64PictureData) => {
console.log('拍照返回数据',base64PictureData);
await this.processImage(base64PictureData);
});
@ -460,49 +460,136 @@ export default {
}
},
//
removeExifData(pureBase64) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
// dataURLbase64
const dataUrl = canvas.toDataURL('image/jpeg', 0.8);
const cleanPureBase64 = dataUrl.split(',')[1]; //
resolve(cleanPureBase64);
};
img.onerror = () => reject(new Error('图像加载失败'));
//
img.src = `data:image/jpeg;base64,${pureBase64}`;
});
},
// - 使ImageUtils
async processImage(imageData) {
this.isProcessing = true;
try {
// base64PictureData base64
const cleanBase64 = await this.removeExifData(imageData);
// cleanBase64 base64
console.log('清理后的纯base64:', cleanBase64);
console.log('开始处理图片...');
console.log("base64数据:", imageData[0]);
console.log('图片处理完成开始OCR识别...');
const response = await new Promise((resolve, reject)=>
{
uni.request({
url: '/material/app/ocr/getOcrCode',
method: 'POST',
data: {
image: imageData[0],
image: cleanBase64,
jiju_type: '',
auth_lic: 'xIWDlaDVdijcBB4mjhGCPYk5Kvk8tHZJbUn+vW+ih15+MYx98e/PXyBmKL5gFcWMPznLgDA15QuSAnZQSLddwdy9HkZgtuQDEEZZ351Eyb1eiDUccUnyoSGIrNimbx5TooBNNPYqU4qJeFrPJXAqjBHzRrxoBxuR2CEGKQPgHC4='
},
header: {
'Content-Type': 'application/json', // POST JSON
"Content-Type": "application/json;charset=UTF-8"
},
timeout: 30000,
success: resolve,
fail: reject
});
}
)
;
if (response.data?.data?.result) {
this.queryCodeParams.maCode = response.data.data.result;
success: async (res) => {
console.log("res", res);
const {data: resData} = res
if (resData.data.data.code===0){
if (resData.data.data.result) {
this.queryCodeParams.maCode = resData.data.data.result
await this.closeCamera();
this.getCode();
uni.showToast({
await this.getCode();
await uni.showToast({
title: '识别成功',
icon: 'success'
});
} else {
uni.showToast({
title: '未识别到有效编码',
icon: 'none'
});
title: '识别失败!' + resData.data.msg,
icon: 'none',
})
}
} else {
uni.showToast({
title: '识别失败!',
icon: 'none',
})
}
},
fail: (err) => {
uni.showToast({
title: '请求失败:' + err.errMsg,
icon: 'none',
})
},
})
// const response = await new Promise((resolve, reject)=>
// {
// uni.request({
// url: '/material/app/ocr/getOcrCode',
// method: 'POST',
// data: {
// image: cleanBase64,
// jiju_type: '',
// auth_lic: 'xIWDlaDVdijcBB4mjhGCPYk5Kvk8tHZJbUn+vW+ih15+MYx98e/PXyBmKL5gFcWMPznLgDA15QuSAnZQSLddwdy9HkZgtuQDEEZZ351Eyb1eiDUccUnyoSGIrNimbx5TooBNNPYqU4qJeFrPJXAqjBHzRrxoBxuR2CEGKQPgHC4='
// },
// header: {
// "Content-Type": "application/json;charset=UTF-8"
// },
// timeout: 30000,
// success: resolve,
// fail: reject
// });
// }
// );
// console.log("response",response);
// console.log("response.data",response.data);
//
// //
// if (response.data?.code === 0) {
// const ocrData = response.data.data;
// if (ocrData?.result) {
// this.queryCodeParams.maCode = ocrData.result;
// await this.closeCamera();
// this.getCode();
// uni.showToast({
// title: '',
// icon: 'success'
// });
// } else {
// uni.showToast({
// title: '',
// icon: 'none'
// });
// }
// } else {
// // HTTP
// const errorMsg = response.data?.msg || 'OCR';
// uni.showToast({
// title: errorMsg,
// icon: 'none'
// });
// }
} catch (error) {
console.error('图片处理或OCR识别失败:', error);
uni.showToast({
@ -599,9 +686,8 @@ export default {
fail: reject
});
}
)
;
);
console.log("response",response.data);
if (response.data?.data?.result) {
this.queryCodeParams.maCode = response.data.data.result;
await this.closeCamera();
@ -788,11 +874,13 @@ export default {
left: 0;
right: 0;
display: flex;
justify-content: space-around; /* 改为space-around使按钮分布更均匀 */
align-items: center;
width: 100%;
z-index: 3; /* 控制按钮在最高层 */
pointer-events: auto; /* 启用点击事件 */
padding: 0 60rpx; /* 添加左右内边距 */
z-index: 3;
pointer-events: auto;
}
.control-btn {
width: 100rpx;
height: 100rpx;
@ -802,6 +890,7 @@ export default {
justify-content: center;
align-items: center;
border: 2rpx solid rgba(255, 255, 255, 0.3);
margin: 0 20rpx; /* 添加按钮间距 */
}
.control-icon {
@ -820,6 +909,7 @@ export default {
align-items: center;
border: 6rpx solid rgba(255, 255, 255, 0.5);
transition: all 0.2s ease;
margin: 0 20rpx; /* 添加按钮间距 */
}
.photo-btn.taking {

View File

@ -445,7 +445,7 @@ export default {
//
console.log('拍照前聚焦...');
console.log('开始拍照...');
CameraPreview.takePicture({width:640, height:640, quality: 50}, async (base64PictureData) => {
CameraPreview.takePicture({width:640, height:540, quality: 50}, async (base64PictureData) => {
console.log('拍照返回数据',base64PictureData);
await this.processImage(base64PictureData);
});
@ -460,12 +460,41 @@ export default {
}
},
//
removeExifData(pureBase64) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
// dataURLbase64
const dataUrl = canvas.toDataURL('image/jpeg', 0.8);
const cleanPureBase64 = dataUrl.split(',')[1]; //
resolve(cleanPureBase64);
};
img.onerror = () => reject(new Error('图像加载失败'));
//
img.src = `data:image/jpeg;base64,${pureBase64}`;
});
},
// - 使ImageUtils
async processImage(imageData) {
this.isProcessing = true;
try {
// base64PictureData base64
const cleanBase64 = await this.removeExifData(imageData);
// cleanBase64 base64
console.log('清理后的纯base64:', cleanBase64);
console.log('开始处理图片...');
console.log("base64数据:", imageData[0]);
console.log('图片处理完成开始OCR识别...');
const response = await new Promise((resolve, reject)=>
@ -474,10 +503,13 @@ export default {
url: '/material/app/ocr/getOcrCode',
method: 'POST',
data: {
image: imageData[0],
image: cleanBase64,
jiju_type: '',
auth_lic: 'xIWDlaDVdijcBB4mjhGCPYk5Kvk8tHZJbUn+vW+ih15+MYx98e/PXyBmKL5gFcWMPznLgDA15QuSAnZQSLddwdy9HkZgtuQDEEZZ351Eyb1eiDUccUnyoSGIrNimbx5TooBNNPYqU4qJeFrPJXAqjBHzRrxoBxuR2CEGKQPgHC4='
},
header: {
"Content-Type": "application/json;charset=UTF-8"
},
timeout: 30000,
success: resolve,
fail: reject
@ -486,8 +518,11 @@ export default {
)
;
if (response.data?.data?.result) {
this.queryCodeParams.maCode = response.data.data.result;
//
if (response.data?.code === 0) {
const ocrData = response.data.data;
if (ocrData?.result) {
this.queryCodeParams.maCode = ocrData.result;
await this.closeCamera();
this.getCode();
uni.showToast({
@ -500,6 +535,14 @@ export default {
icon: 'none'
});
}
} else {
// HTTP
const errorMsg = response.data?.msg || 'OCR识别失败';
uni.showToast({
title: errorMsg,
icon: 'none'
});
}
} catch (error) {
console.error('图片处理或OCR识别失败:', error);
uni.showToast({