2024-06-25 04:09:42 +00:00
|
|
|
|
<template>
|
|
|
|
|
|
<view class="canvas-con">
|
|
|
|
|
|
<canvas :canvas-id="canvasId" :style="{width:canvasWidth + 'px', height: canvasHeight + 'px'}"></canvas>
|
|
|
|
|
|
</view>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
|
|
|
|
|
|
import { showLoading, showToast, hideLoading, canvasGetImageData, getImageInfo, promiseTimeout } from "@/common/untool"
|
|
|
|
|
|
import * as canvasUtil from "@/common/canvasUtil"
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
props:{
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
computed: {
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
data () {
|
|
|
|
|
|
return {
|
|
|
|
|
|
canvasId: uni.$u.guid(20),
|
|
|
|
|
|
canvasWidth: 1,
|
|
|
|
|
|
canvasHeight: 1,
|
|
|
|
|
|
imgData: '',
|
|
|
|
|
|
imageUrl: '',
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
mounted () {
|
|
|
|
|
|
this.initData()
|
|
|
|
|
|
},
|
|
|
|
|
|
unmounted() {
|
|
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
async initData() {
|
|
|
|
|
|
this.imageUrl && await this.renderCanvasImg(this.imageUrl);
|
|
|
|
|
|
},
|
|
|
|
|
|
renderCanvasImg(filePath, paperWidth) {
|
|
|
|
|
|
let that = this
|
|
|
|
|
|
showLoading()
|
|
|
|
|
|
console.log("===renderCanvasImg:filePath=====", filePath)
|
|
|
|
|
|
return new Promise(async (resolve, reject)=> {
|
|
|
|
|
|
try{
|
|
|
|
|
|
let res = await getImageInfo(filePath)
|
|
|
|
|
|
console.log("getImageInfo res====", res)
|
|
|
|
|
|
const ctx = uni.createCanvasContext(that.canvasId, that);
|
|
|
|
|
|
// 打印宽度须是8的整数倍,这里处理掉多余的,使得宽度合适,不然有可能乱码
|
|
|
|
|
|
const remainder = paperWidth % 8;
|
|
|
|
|
|
const w = remainder === 0 ? paperWidth : paperWidth - remainder;
|
|
|
|
|
|
// 等比算出图片的高度
|
|
|
|
|
|
const h = Math.floor((res.height * w) / res.width);
|
|
|
|
|
|
that.canvasWidth = w
|
|
|
|
|
|
that.canvasHeight = h
|
|
|
|
|
|
// 在canvas 画一张图片
|
|
|
|
|
|
let imgRect = canvasUtil.containImg(0, 0, w, h, res.width, res.height)
|
|
|
|
|
|
let ctData = [imgRect.dx, imgRect.dy, imgRect.dWidth, imgRect.dHeight]
|
|
|
|
|
|
ctx.fillStyle = 'rgba(255,255,255,1)';
|
|
|
|
|
|
ctx.clearRect(...ctData);
|
|
|
|
|
|
ctx.fillRect(...ctData);
|
|
|
|
|
|
// 把原图缩放到 打印宽度的比例 绘制
|
|
|
|
|
|
ctx.drawImage(filePath, ...ctData);
|
|
|
|
|
|
ctx.draw(false, async() => {
|
|
|
|
|
|
let imgData = await canvasGetImageData({ canvasId: that.canvasId, width: w, height: h, })
|
|
|
|
|
|
hideLoading()
|
|
|
|
|
|
that.imgData = imgData
|
|
|
|
|
|
console.log("canvasGetImageData imgData.data.length =====", imgData.data.length)
|
|
|
|
|
|
// 多次渲染,createCanvasContext,必须使用新的才可以
|
|
|
|
|
|
that.canvasId = uni.$u.guid(20)
|
|
|
|
|
|
resolve({
|
|
|
|
|
|
imgData,
|
|
|
|
|
|
width: w,
|
|
|
|
|
|
height: h
|
|
|
|
|
|
})
|
|
|
|
|
|
});
|
|
|
|
|
|
}catch(e){
|
2024-06-25 08:12:48 +00:00
|
|
|
|
console.log("renderCanvasImg error==========", e)
|
2024-06-25 04:09:42 +00:00
|
|
|
|
// 多次渲染,createCanvasContext,必须使用新的才可以
|
|
|
|
|
|
that.canvasId = uni.$u.guid(20)
|
|
|
|
|
|
hideLoading()
|
|
|
|
|
|
reject(e)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
|
|
.canvas-con{
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
}
|
|
|
|
|
|
/*图片隐藏到窗口外*/
|
|
|
|
|
|
canvas {
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
z-index: -999;
|
|
|
|
|
|
top: -999px;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|