albumDrawBox.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <photo-consumer
  3. class="carousel-item"
  4. :src="watermark || base_img_url2 + (photo.resFilename && !name ? photo.resFilename : photo.filename) + resize"
  5. >
  6. <img v-if="Math.abs(current - index) < 3" crossorigin="anonymous" @load="drawWatermark($event, name)" loading="lazy" :src="watermark || (base_img_url2 + (photo.resFilename && !name ? photo.resFilename : photo.filename) + resize)" style="width: 100%;" />
  7. <canvas ref="canvasRef" style="position: absolute;"></canvas>
  8. <!-- <div class="tag-text" v-if="showTagBox" >
  9. <span v-html="photo.growText"></span>
  10. <button class="close-button" @click="hideTagBox">✖</button>
  11. </div> -->
  12. <div class="tag-box right" :class="{'leftTop': 'leftTop'}">{{ index+1 }}/{{ length }}</div>
  13. <!-- <div class="center-mark">mark</div>-->
  14. </photo-consumer>
  15. </template>
  16. <script setup>
  17. import { ref, onMounted, onBeforeUnmount, defineProps } from "vue";
  18. import { base_img_url2 } from "@/api/config";
  19. import {imageCache,loadImage} from "./cacheImg.js"
  20. import {dateFormat} from "@/utils/date_util.js"
  21. import {pointToFormat} from "@/utils/util.js"
  22. import {drawTextInRect, drawBorderImageInRect, drawImageInRect, drawRectInRect, drawHorizontalTextList, drawTargetRectangles} from "./utils"
  23. const resize = "?x-oss-process=image/resize,p_50/format,webp/quality,q_50";
  24. const canvasRef = ref(null);
  25. const watermark = ref(null)
  26. const baseMapBig = ref(false)
  27. const props = defineProps({
  28. farmId:{
  29. type: [Number,String],
  30. required: true
  31. },
  32. photo:{
  33. type: Object,
  34. required: true
  35. },
  36. index:{
  37. type: Number,
  38. required: true
  39. },
  40. length:{
  41. type: Number,
  42. required: true
  43. },
  44. current:{
  45. type: Number,
  46. required: true
  47. },
  48. name:{
  49. type: String,
  50. default: ""
  51. }
  52. })
  53. let img = null;
  54. let ctx = null;
  55. let data = {year:props.photo.uploadDate.substring(0,4),
  56. monthDay:dateFormat(new Date(props.photo.uploadDate),'mm/dd'),
  57. address:props.photo.district.replaceAll("\"","") + props.photo.gardenName,
  58. tempImg:imageCache.get("temp"),temp:"10°C-20°C",wendu:"适宜",
  59. feiniao:imageCache.get("feiniao"),
  60. baseMap:imageCache.get("base_map_"+props.photo.treeId),
  61. fusheImg:imageCache.get("fushe"),fushe:"光照优",
  62. shiduImg:imageCache.get("shidu"),shidu:"湿度适宜",
  63. text:"病害风险,及时喷药",
  64. shotCode:props.photo.shotCode,
  65. treeCode:props.photo.treeCode,
  66. pingzhong:props.photo.pingzhong,
  67. uploadDate:props.photo.uploadDate,
  68. }
  69. async function drawWatermark(event,name) {
  70. if(!name){
  71. // drawWatermark1(event)
  72. }else{
  73. // drawTargetRectangles2(event.target,props.photo.watermarks.find(item=>item.name === name))
  74. }
  75. }
  76. function drawTargetRectangles2(img, obj) {
  77. const canvas = canvasRef.value;
  78. let scale = 3
  79. canvas.width = img.width * scale;
  80. canvas.height = img.height * scale;
  81. ctx = canvas.getContext('2d');
  82. ctx.scale(scale, scale)
  83. ctx.drawImage(img, 0, 0, img.width, img.height);
  84. drawTargetRectangles(ctx, obj, 4000,3000,img.width, img.height)
  85. watermark.value = canvas.toDataURL();
  86. }
  87. async function drawWatermark1(event) {
  88. img = event.target
  89. await loadImage(props.photo.baseMap,"base_map_"+props.photo.treeId)
  90. data.baseMap = imageCache.get("base_map_"+props.photo.treeId)
  91. if(!watermark.value){
  92. let param = {farmId:props.farmId, date: props.photo.uploadDate}
  93. let weather = null
  94. VE_API.image.findSuitabilityByPoint(param).then((res)=>{
  95. if(res.code === 0){
  96. weather = res.data
  97. drawWatermark2(img,weather)
  98. }else{
  99. drawWatermark2(img,null)
  100. }
  101. })
  102. }
  103. }
  104. function drawWatermark2(img,weather) {
  105. const canvas = canvasRef.value;
  106. let scale = 3
  107. canvas.width = img.width * scale;
  108. canvas.height = img.height * scale;
  109. ctx = canvas.getContext('2d');
  110. ctx.scale(scale, scale)
  111. ctx.drawImage(img, 0, 0, img.width, img.height);
  112. drawBottom(img.width, img.height, weather)
  113. watermark.value = canvas.toDataURL();
  114. }
  115. // console.log(data)
  116. const drawBottom = (imgWidth, imgHeight, weather) => {
  117. if (weather) {
  118. data["temp"] = weather.tempMin + "°C" + "-" + weather.tempMax + "°C" + " " + weather.tempSuitability
  119. data["fushe"] = "光照"+weather.vindexSuitability
  120. data["shidu"] = "湿度"+weather.humiditySuitability
  121. }
  122. // 设置文本样式
  123. ctx.font = "8px Arial";
  124. ctx.textAlign = "left"; // 设置为左对齐
  125. let imgRect = { x: 0, y: 0, width: imgWidth, height: imgHeight}
  126. // 绘制头部黑色半透明遮罩
  127. let topRect = drawRectInRect(ctx,imgRect, 0, 0, 30, 10,"rgba(0, 0, 0, 0.6)")
  128. // 绘制黑色半透明遮罩
  129. let bottomRect = drawRectInRect(ctx,imgRect, 0, 5/6 * 100, 100, 1/6 * 100,"rgba(0, 0, 0, 0.6)")
  130. // drawHorizontalTextList(ctx, topRect, '#ffffff90',[data.treeCode, "蓬径:5m", "高度:3m", "高产树",data.pingzhong],
  131. drawHorizontalTextList(ctx, topRect, '#ffffff90',[data.treeCode],
  132. 40, 0,
  133. 65 , 0,"|",0,
  134. 30,"#ffffff50",1,2)
  135. ctx.fillStyle = "white"; // 文本颜色为白色
  136. // 绘制温度
  137. let startXPercent = 1;
  138. drawImageInRect(ctx, bottomRect, data.tempImg, startXPercent, 5+10, 5, 30)
  139. drawTextInRect(ctx, bottomRect,`${data.temp}`,startXPercent + 4, 28+10, 20)
  140. // 绘制湿度
  141. drawImageInRect(ctx, bottomRect, data.shiduImg, startXPercent+26, 7 + 10, 4, 30)
  142. drawTextInRect(ctx, bottomRect,`${data.shidu}`,startXPercent + 31, 28 + 10, 20)
  143. // 绘制辐射
  144. drawImageInRect(ctx, bottomRect, data.fusheImg, startXPercent+26 + 18, 7 + 10, 5, 30)
  145. drawTextInRect(ctx, bottomRect,`${data.fushe}`,startXPercent+31 + 18, 28 + 10, 20)
  146. //绘制日期信息
  147. ctx.fillStyle = "#FFFFFF";
  148. drawTextInRect(ctx, bottomRect,`${formatDate(new Date(data.uploadDate))}`,startXPercent +1.7, 75, 24)
  149. //绘制位置信息
  150. ctx.fillStyle = "#FFFFFF90";
  151. drawTextInRect(ctx, bottomRect,`${data.treeCode}_S3_SCS3-3_D0P0G1`,startXPercent +13, 75, 18)
  152. if(data.baseMap){
  153. drawBorderImageInRect(ctx, imgRect, data.baseMap, 2/3*100, 2/3*100,
  154. 1/3*100, 1/3*100, 5, 5)
  155. }
  156. }
  157. const showTagBox = ref(true); // 控制 tag-box 的显示状态
  158. const hideTagBox = (event) => {
  159. event.stopPropagation();
  160. showTagBox.value = false; // 隐藏 tag-box
  161. };
  162. const formatDate = (date) => {
  163. const year = date.getFullYear();
  164. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1
  165. const day = String(date.getDate()).padStart(2, '0');
  166. return `${(year+"").substring(2)}${month}${day}`;
  167. };
  168. </script>
  169. <style lang="scss" scoped>
  170. .canvas-container {
  171. width: 100%;
  172. height: 100%;
  173. display: flex;
  174. justify-content: center;
  175. }
  176. .carousel-item {
  177. min-width: 100%;
  178. flex-shrink: 0;
  179. width: 100%;
  180. pointer-events: auto;
  181. position: relative;
  182. .tag-box {
  183. position: absolute;
  184. bottom: 30%;
  185. left: 50%;
  186. transform: translate(-50%, 50%); // 确保在高二分之一的位置水平居中
  187. height: 18px;
  188. padding: 0 6px;
  189. background: rgba(108, 108, 108, 0.67);
  190. border-radius: 10px;
  191. display: flex;
  192. align-items: center;
  193. color: #FFFFFF;
  194. font-size: 12px;
  195. &.right {
  196. left: auto;
  197. right: 10px;
  198. }
  199. &.leftTop {
  200. height: 25px;
  201. line-height: 26px;
  202. padding: 0 8px;
  203. border-radius: 16px;
  204. background: rgba(0, 0, 0, 0.6);
  205. bottom: auto;
  206. top: 6px;
  207. }
  208. }
  209. .tag-text {
  210. position: absolute;
  211. bottom: 31%;
  212. left: 50%;
  213. width: 80%;
  214. transform: translate(-50%, 50%); // 确保在高二分之一的位置水平居中
  215. height: 24px;
  216. padding: 10px 0px 10px 0px;
  217. background: rgba(0, 0, 0, 0.67);
  218. border-radius: 6px;
  219. display: flex;
  220. align-items: center;
  221. justify-content: center;
  222. text-align: center;
  223. color: #FFFFFF;
  224. font-size: 12px;
  225. }
  226. .center-mark {
  227. position: absolute;
  228. bottom: 10px;
  229. left: 50%;
  230. transform: translateX(-50%);
  231. color: #36402c;
  232. font-size: rpx(24);
  233. font-weight: bold;
  234. padding: rpx(14) rpx(30);
  235. background: linear-gradient(
  236. 90deg,
  237. rgba(255, 255, 255, 0) 0%,
  238. rgba(255, 255, 255, 0.6) 24%,
  239. rgba(255, 255, 255, 0.6) 76%,
  240. rgba(255, 255, 255, 0) 100%
  241. );
  242. }
  243. }
  244. .carousel-item img {
  245. width: 100%;
  246. display: block;
  247. border-radius: 12px;
  248. }
  249. canvas {
  250. position: absolute;
  251. }
  252. .close-button {
  253. background: transparent;
  254. border: none;
  255. color: #FFFFFF;
  256. cursor: pointer;
  257. font-size: 10px; // 可以根据需求调整大小
  258. position: absolute;
  259. top: -1px;
  260. right: -9px; // 调整为合适的间距
  261. transform: translateY(-50%);
  262. }
  263. .floating-img {
  264. position: absolute;
  265. bottom: 0;
  266. right: 0;
  267. width: auto !important;
  268. height: 25% !important;
  269. }
  270. .floating-img-big {
  271. position: fixed !important;
  272. z-index: 99999 !important;
  273. top: 50% !important;
  274. left: 50% !important;
  275. width: auto !important;
  276. height: 100% !important;
  277. transform: translate(-50%, -50%) !important;
  278. }
  279. </style>