9***@qq.com
9***@qq.com
  • 发布:2022-09-20 09:22
  • 更新:2023-11-13 16:39
  • 阅读:809

【报Bug】安卓12拍照获取不到照片

分类:MUI

产品分类: 其他/MUI

示例代码:

<div class="paz" id="yslsc2" onclick="cameraIdCardImd('lhxyPhoto')">
<img src="../../images/camera.png" class="pimg">
</div>
function cameraIdCardImd(imgId) {
toCameraImage(imgId, function(msg) {
if(msg == '拍照成功') {
var imgPath = document.getElementById(imgId).src;
console.log('imgPath='+imgPath);
var indexS = imgPath.indexOf('?r=');
console.log('indexS='+indexS);
var imgPath2 = '';
if(indexS >-1){
imgPath2 = imgPath.substring(0,indexS);
}else{
imgPath2 = imgPath;
}
console.log('imgPath2='+imgPath2);
var imgPath3 = imgPath2 +'?r=' + new Date().getTime();
console.log('imgPath3='+imgPath3);
getBase64Image(imgPath,imgId);
}
});
}
function toCameraImage(imgId, callback) {
console.log('开始拍照');
plus.camera.getCamera().captureImage(function(p) {
console.log('拍照成功');
plus.io.resolveLocalFileSystemURL(p, function(entry) {
console.log('拍照图片地址:' + entry.toLocalURL());
entry.getMetadata(function(metadata) {
console.log("uploadCameraImage >> src: " + entry.toLocalURL());
document.getElementById(imgId).src = entry.toLocalURL() + "?r=" + new Date().getTime();
callback.call(this, '拍照成功');
}, function(e) {
console.log('获取拍照文件"' + entry.name + '"信息失败:' + e.message);
plus.nativeUI.toast('获取拍照文件"' + entry.name + '"信息失败:' + e.message);
callback.call(this, '获取拍照文件"' + entry.name + '"信息失败:' + e.message);
});
}, function(e) {
console.log('读取拍照文件错误:' + e.message);
plus.nativeUI.toast('读取拍照文件错误:' + e.message);
callback.call(this, '读取拍照文件错误:' + e.message);
});
}, function(e) {
console.log('拍照失败:' + e.message);
if (e.message != 'resultCode is wrong') {
plus.nativeUI.toast('拍照失败:' + e.message);
}
callback.call(this, '拍照失败:' + e.message);
}, {
filename: '_doc/camera/',
index: 1
});
}
function getBase64Image(imgPath,imgId) {
console.log('imgPath2=0');

        var img = new Image();  
        console.log('imgPath2=3');  
        img.crossOrigin = 'Anonymous';  
        console.log('imgPath2=2');  
        img.onload = function(){  
            console.log('imgPath2=1');  
            //创建canvas画布  
            var canvas = document.createElement("canvas");   
            //在css中不要直接给img设置宽高,否则此处会获取到css设置的值  
            var width  = img.width;  
            var height = img.height;  
            //比较图片宽高设置图片显示和canvas画布尺寸  
            if (width > height) {   
                if (width > 892) {   
                    height = Math.round(height *= 892 / width);   
                    width = 892;   
                }   
            } else {   
                if (height > 892) {   
                    width = Math.round(width *= 892 / height);   
                    height = 892;   
                }   
            }   
            canvas.width  = width;                               //设置新的图片的宽度  
            canvas.height = height;                              //设置新的图片的长度  
            var hanzi = canvas.getContext("2d");  
            hanzi.clearRect(0, 0, width, height);  
            hanzi.drawImage(img, 0, 0, width, height);  
            //绘图  
            dataURL = canvas.toDataURL("image/jpeg", 0.92);    //供img标签使用的src路径  
            console.log('dataURL='+dataURL);  
             if(imgId =='lhxyPhoto'){  
                $("#yslsc2").hide();  
                $("#zs2").show();  
                hasUploadLhxy = true;  
                //$("#lhxyImgId").val(data.obj);  
                $("#lhxyImgBase64").val(dataURL.replace("data:image/jpeg;base64,",""));  
                console.log('lhxyImgBase64='+$("#lhxyImgBase64").val());  
            }  
            document.getElementById(imgId).src = dataURL;  
        }  

        img.src = imgPath;  

    }  

操作步骤:

点击div 触发cameraIdCardImd 拍照方法

预期结果:

回显照片

实际结果:

没有回显照片

bug描述:

比较老的项目使用的是5+app+mui框架,HBuilder 9.1.29.201811231920,最近使用安卓12手机拍照的时候,发现获取不到照片。具体调用摄像头拍照后,照片没有显示预览,跟踪代码发现没有进入Image.onload方法(拍照流程大致为调用摄像头拍照,获取到拍照成功后用Image读取照片,转base64);根据获取到的拍照成功路径,访问手机目录,发现照片文件为0kb,代码如下:

2022-09-20 09:22 负责人:无 分享
已邀请:
4***@qq.com

4***@qq.com

这个问题解决了吗?我有一个16年的项目和你的问题一样

DCloud_Android_zl

DCloud_Android_zl

升级到HBuilderX最新版重新打包试一下。

所遇所思

所遇所思

一样拍照后,图片为0B

要回复问题请先登录注册