PDA手持扫码枪(Android)使用uniapp进行拍照操作

利用plus.camera.getCamera()实现PDA中的uniapp拍照,并返回文件路径
前提:
- uni自带的拍照插件不支持app使用,因此不能使用常规调用摄像头方法
- 需要实现PDA拍照
- uniapp的app端用不了tesseract.js ,因为nodejs没有blob,放弃前端识别文本
plus.camera.getCamera( index );
指定要获取摄像头的索引值,1表示主摄像头,2表示辅摄像头。如果没有设置则使用系统默认主摄像头。
cmr.captureImage(successCB, errorCB, options);
successCB: ( CameraSuccessCallback ) 必选 拍照操作成功的回调函数
errorCB: ( CameraErrorCallback ) 可选 拍照操作失败的回调函数
options: ( CameraOptions ) 必选 摄像头拍照参数
输出camera对象
//获取摄像头管理对象 var camera = plus.camera.getCamera(); console.log(camera)
camera对象输出:(分辨率等信息)
{
"__busy__": false,
"supportedImageResolutions": [
"4160*3120",
"4096*2304",
"4096*2160",
"4000*3000",
"3840*2160",
"3264*2448",
"3200*2400",
"2976*2976",
"2688*1512",
"2592*1944",
"2592*1940",
"2048*1536",
"1920*1440",
"1920*1080",
"1600*1200",
"1440*1080",
"1280*960",
"1280*768",
"1280*720",
"1080*1080",
"1024*738",
"1024*768",
"864*480",
"800*600",
"800*480",
"720*1280",
"720*480",
"640*480",
"640*400",
"640*360",
"352*288",
"320*240",
"240*320",
"224*1557",
"224*2941",
"224*865",
"176*144"
],
"supportedVideoResolutions": [
"1920*1080",
"1440*1080",
"1280*960",
"1280*768",
"1280*720",
"1080*1080",
"1024*738",
"1024*768",
"864*480",
"800*600",
"800*480",
"720*480",
"640*480",
"640*400",
"640*360",
"352*288",
"320*240",
"240*320",
"224*865",
"176*144"
],
"supportedImageFormats": [
"jpg"
],
"supportedVideoFormats": [
"mp4"
]
}
拍照方法:
// 拍照
function takePic() {
console.log("拍照识别------------------------------")
//获取摄像头管理对象
var camera = plus.camera.getCamera();
//分辨率
var res = camera.supportedImageResolutions[0];
//文件格式
var fmt = camera.supportedImageFormats[0];
//图片文件保存的路径
var filename = "_doc/ocr_pic/";
console.log(camera)
camera.captureImage(
(path) => {
//获取图片信息(图片在PDA中的路径)
plus.io.getImageInfo({
src: path,
success: function (res) {
console.log("图片在PDA中的路径是:", res);
ocrPath = res.path;
},
fail: function (err) {
console.log("获取失败", err);
}
})
console.log("拍照成功,图片路径是: " + path);
},
(error) => {
console.log("拍照失败: " + error.message);
},
{
resolution: res,
format: fmt,
filename: filename
}
);
}
输出结果:
拍照成功,图片路径是: _doc/ocr_pic/1740647043533.jpg
图片在PDA中的路径是:, [Object] {"path":"file:///storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/ocr_p...}
------
{
"path": "file:///storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/ocr_pic/1740647043533.jpg",
"width": 3120,
"height": 4160,
"orientation": "up",
"type": "jpeg"
}