inspection-painter.vue
7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<template>
<view class="inspection-painter">
<canvas ref="painterRef" :style="{ width: '100%',height:canvasHeight+'px' }" canvas-id="firstCanvas" id="firstCanvas" disable-scroll
@touchstart="handleStart" @touchmove="handleMove" @touchend="handleEnd"></canvas>
<!-- <view @click="getCanvasBase64">base64</view> -->
</view>
</template>
<script setup>
import {
ref,
onMounted,
getCurrentInstance,
nextTick
} from 'vue';
import {
testDrawLine
} from '../draw.js'
import {
getImagePath,
} from '../utils'
const { proxy } = getCurrentInstance()
const props = defineProps({
imageUrl: {
type: String,
required: true,
},
})
const canvasHeight = ref(150)
// 绘制部分
const linePointList = [] // 绘制路线,数据集合
let currentLinePointList = [] // 本次绘制的坐标集合
let context = null
function drawLine() {
context.beginPath();
context.moveTo(50, 50);
context.lineTo(200, 100);
context.strokeStyle = "red";
context.stroke();
}
function getPointData(data) {
const coord = data.changedTouches[0]
if (coord) {
// 记录数据
currentLinePointList.push([coord.x, coord.y])
return [coord.x, coord.y]
} else {
return null
}
}
/**
* "changedTouches": {
"0": { "x": 195, "y": 60 }
},
*/
const isDrawing = ref(false);
let lastCoord = []
function handleStart(event) {
const coord = getPointData(event)
console.log('handleStart', coord)
isDrawing.value = true
console.log(context, '=s=s=');
// context.beginPath();
// context.moveTo(50, 50);
context.moveTo(...coord);
lastCoord = coord
}
function handleMove(event) {
console.log('handleMove', event)
// if(!isDrawing.value) return;
const coord = getPointData(event)
context.beginPath();
context.setStrokeStyle("red")
context.setLineWidth(2)
context.moveTo(...lastCoord);
context.lineTo(...coord);
context.stroke();
context.draw(true)
lastCoord = coord
}
function handleEnd(event) {
console.log('handleEnd', event)
linePointList.push(currentLinePointList)
currentLinePointList = []
}
// 小程序获取64
function getBase64FromTempPath(tempFilePath,callback=()=>{}) {
uni.getFileSystemManager().readFile({
filePath: tempFilePath,
encoding: 'base64',
success: (res) => {
callback(res.data)
},
fail: (err) => {
console.error('读取文件失败:', err);
}
});
}
// 获取base64
function getCanvasBase64() {
console.log('getCanvasBase64')
// #ifdef H5
return getCanvasBase64H5()
// #endif
return new Promise((resolve, reject) => {
uni.canvasToTempFilePath({
canvasId: 'firstCanvas',
success: async (res) => {
const tempFilePath = res.tempFilePath;
// #ifdef MP-WEIXIN
getBase64FromTempPath(tempFilePath,resolve)
// #endif
// #ifdef APP-PLUS
const platform = uni.getSystemInfoSync().platform
if (platform === 'ios') {
const uniPath = plus.io.convertLocalFileSystemURL(res.tempFilePath);
plus.io.resolveLocalFileSystemURL(uniPath, (entry) => {
entry.file((file) => {
const fileReader = new plus.io.FileReader();
// 使用原生大文件处理模式
fileReader.readAsDataURL(file, "utf-8", 1024 * 1024); // 1MB 分块
fileReader.onload = (event) => {
if (event.type === 'progress') {
console.log(`加载进度: ${event.loaded}/${event.total}`);
} else if (event.type === 'load') {
const base64 = event.target.result;
console.log('base64加载成功', base64.length, base64.slice(0, 54));
// console.log('Base64 加载完成', base64);
const targetData = base64.split(',')
if (targetData[1]) {
resolve(targetData[1])
} else {
reject('图片数据不存在')
}
}
};
fileReader.onerror = (error) => {
console.error('大文件读取错误:', error.message);
};
});
});
} else {
// console.log(22222)
plus.io.resolveLocalFileSystemURL(res.tempFilePath, entry => {
entry.file(file => {
const reader = new plus.io.FileReader();
reader.onloadend = event => {
const targetData = event.target.result.split(',')
if (targetData[1]) {
resolve(targetData[1])
} else {
reject('图片数据不存在')
}
};
reader.readAsDataURL(file);
});
});
}
// #endif
},
fail: err => {
console.error('canvasToTempFilePath 失败:', err);
reject('canvasToTempFilePath 获取失败')
}
},proxy);
})
}
function getCanvasBase64H5() {
return new Promise((resolve, reject) => {
uni.canvasToTempFilePath({
canvasId: 'firstCanvas',
success: res => {
console.log('res', res)
const targetData = res.tempFilePath.split(',')
if (targetData[1]) {
resolve(targetData[1])
} else {
reject('图片数据不存在')
}
},
fail: err => {
reject(err)
}
},proxy)
})
}
const painterRef = ref(null)
function initPainter(url) {
const imgPath = decodeURIComponent(url)
getImagePath(imgPath).then(res => {
// 计算展示的宽和高
const systemInfo = uni.getSystemInfoSync()
const showWidth = systemInfo.windowWidth - 20 // 20
// 保持图片比例
const showHeight = (res.height * showWidth) / res.width
console.log(res.width, res.height, '------', showWidth, showHeight);
canvasHeight.value = showHeight
// 设置宽高
console.log('painterRef', painterRef, showWidth, showHeight)
// 不设置,也可使用
// painterRef.value.style.width = showWidth + 'px'
// painterRef.value.style.height = showHeight + 'px'
// 保证canvas的宽高与图片一致
nextTick(()=>{
setTimeout(()=>{
const ctx = uni.createCanvasContext('firstCanvas', proxy)
ctx.drawImage(res.path, 0, 0, showWidth, showHeight)
ctx.draw()
// 记录context值
context = ctx
},200)
})
})
}
onMounted(() => {
// const targetUrl = 'https://store.keliuyun.com/images/patrol%2Fscreenshot%2F20250605%2Fbd56cd4c-729f-45c4-9d3e-0eff767bb663.jpg'
const targetUrl = props.imageUrl
initPainter(targetUrl)
})
// 暴露方法给父组件调用
defineExpose({
getCanvasBase64H5,
getCanvasBase64,
});
</script>
<style scoped lang="less">
.inspection-painter {}
</style>