var XData // x轴坐标
var YData // Y轴坐标
var TopData = 70 //二维码距离顶部的距离
var BottomData = 60 //二维码距离底部的距离
var LeftData = 60 //二维码距离左侧的距离
var RightData = 80 //二维码距离底部的距离
var ImgWidth = 1080 // 固定宽度
var CodeWidth = 220 //二维码的宽度
var CodeHeight = 220 //二维码的高度
/**
* @function 返回坐标
* @param type 二维码的位置 0:左上 1:右上 2:左下 3:下中 4:右下角
* @param height canvas的高度
* @return x,y
*/
function coordinate(type, height) {
switch (Number(type)) {
case 0:
XData = LeftData;
YData = TopData;
break;
case 1:
XData = ImgWidth - RightData - CodeWidth;
YData = TopData;
break;
case 2:
XData = LeftData;
YData = height - BottomData - CodeHeight;
break;
case 3:
XData = (ImgWidth - CodeWidth) / 2;
YData = height - BottomData - CodeHeight;
break;
case 4:
XData = ImgWidth - RightData - CodeWidth;
YData = height - BottomData - CodeHeight;
break;
}
}
// 图片转base64
function getBase64Image(img) {
var canvas = document.createElement('canvas')
canvas.width = img.width
canvas.height = img.height
var ctx = canvas.getContext('2d')
ctx.drawImage(img, 0, 0, img.width, img.height)
const dataURL = canvas.toDataURL('image/jpeg') // 可根据图片后缀,动态变换如 image/png
return dataURL
}
function imageUrlToBase64({
codePosition,
url = "",
cb = null
} = {}) {
var userData = JSON.parse(uni.getStorageSync("userData"));
var mask = userData.shopName || "开发环境" // 水印
var qrCode = userData.path // 二维码
//一定要设置为let,不然图片不显示
let image = document.createElement("img");
//解决跨域问题
image.setAttribute('crossOrigin', 'anonymous');
image.src = url
let imageCode = document.createElement("img");
imageCode.setAttribute('crossOrigin', 'anonymous');
imageCode.src = qrCode
var canvas = document.createElement("canvas");
var context = canvas.getContext('2d');
image.onload = () => {
canvas.width = image.width;
canvas.height = image.height; //图片大小不同导致不一样 是否可以要求上传的尺寸是相同的
coordinate(codePosition, canvas.height)
console.log('宽高', canvas.width, canvas.height)
context.fillStyle = "#fff"; // 解决透明背景变为黑色问题
context.fillRect(0, 0, canvas.width, canvas.height);
context.drawImage(image, 0, 0, image.width, image.height);
context.font = '50px "微软雅黑"'; //设置字体
context.fillStyle = "rgba(51,51,51,0.7)";
console.log('imageCode1',imageCode)
imageCode.onload = () => {
console.log('imageCode2', XData , YData)
context.drawImage(imageCode, XData, YData, 220, 220);
for (let j = 50; j <= image.height * 2;) {
context.fillText(mask, 0, j);
j += 260
}
console.log('imageCode3', XData , YData)
//这里的dataurl就是base64类型
let originalBase64Url = canvas.toDataURL("image/jpeg",
0.8); //使用toDataUrl将图片转换成jpeg的格式,不要把图片压缩成png,因为压缩成png后base64的字符串可能比不转换前的长!
cb && cb(originalBase64Url);
}
}
}
export default imageUrlToBase64
测试url:http://static.hongliaouv.com/2022/09/05/68f0872ff93d4eada1018c51f4ea4010.jpg
测试qrcdoe: http://static.hongliaouv.com/2022/09/05/8e755bcd34a34fe4b107b15c2f8dcf27.png
coordinate是用来动态计算x y坐标的 您那二维码的位置可以先写死
BugEngineer (作者)
http://117.78.35.40:9000/coulee/2022/07/14/740cd2269421414ca42c6b7be93d3cf5.jpg
我们的需求是显示后端返回的二维码生成海报,我试过来自不同域的图片,也是偶尔出来偶尔不出来。IOS和MAC系统不执行的概率更大,感谢您的回复 麻烦您看下呢
2022-09-05 11:09
BugEngineer (作者)
您好,我把关键代码贴到回复中了,其中第一个onload每次都执行,第二个就偶尔出不来
2022-09-05 11:12