<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;
}
3 个回复
4***@qq.com
这个问题解决了吗?我有一个16年的项目和你的问题一样
DCloud_Android_zl
升级到HBuilderX最新版重新打包试一下。
所遇所思
https://ask.dcloud.net.cn/article/35617
做H5+app的,可以调相机,并且可以获取到图片路径,但是后续就没效果了,请问如何解决
2023-11-13 16:08
所遇所思
一样拍照后,图片为0B