devicePhoto.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <template>
  2. <view class="device-photo-page">
  3. <!-- 顶部导航栏 -->
  4. <view class="device-detail__header">
  5. <u-icon
  6. size="36"
  7. class="arrow-left"
  8. name="arrow-left"
  9. @click="handleBack"
  10. ></u-icon>
  11. 查看图片
  12. </view>
  13. <!-- 主图片区域 -->
  14. <view class="main-photo" v-if="currentImg.addr">
  15. <movable-area class="movable-area" :style="{ height: containerHeight }">
  16. <movable-view
  17. :key="currentIndex"
  18. class="movable-view"
  19. direction="all"
  20. scale="true"
  21. scale-min="1"
  22. scale-max="5"
  23. :scale-value="movableScale"
  24. :x="movableX"
  25. :y="movableY"
  26. @tap="onViewerTap"
  27. >
  28. <view class="image-container">
  29. <image
  30. :src="currentImg.addr"
  31. class="main-photo-image"
  32. mode="widthFix"
  33. @load="onImageLoad"
  34. />
  35. <!-- 识别标记层 -->
  36. <view class="bug-markers" v-if="bugMarkers.length">
  37. <view
  38. v-for="(marker, index) in bugMarkers"
  39. :key="index"
  40. class="bug-marker"
  41. :style="{
  42. left: marker.x + '%',
  43. top: marker.y + '%',
  44. width: marker.width + '%',
  45. height: marker.height + '%',
  46. borderColor: marker.color
  47. }"
  48. >
  49. <view class="bug-label" :style="{ color: marker.color }">
  50. {{ marker.id }}
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. </movable-view>
  56. </movable-area>
  57. <!-- 图片来源角标:监测图像(下)显示"下",前后左右显示type(1/2/3/4) -->
  58. <view class="photo-type-badge" v-if="badgeText">{{ badgeText }}</view>
  59. <view class="photo-timestamp">{{ formatDate(currentImg.addtime || addtime) }}</view>
  60. </view>
  61. <view class="main-photo-empty" v-else>
  62. <u-empty text="暂无图片"></u-empty>
  63. </view>
  64. <!-- 缩略图预览:当前分组的所有图片 -->
  65. <view class="thumbnail-preview" v-if="thumbnails.length">
  66. <view class="thumbnail-scroll">
  67. <view
  68. class="thumbnail-item"
  69. v-for="(item, index) in thumbnails"
  70. :key="index"
  71. :class="{ active: currentIndex == index }"
  72. @click="selectThumbnail(index)"
  73. >
  74. <image :src="item.addr" class="thumbnail-image" mode="aspectFill" />
  75. </view>
  76. </view>
  77. </view>
  78. <!-- 识别结果 -->
  79. <view class="recognition-result">
  80. <view class="result-title">当前图片识别结果</view>
  81. <view class="result-table" v-if="pestList.length">
  82. <view class="result-header">
  83. <text class="header-name">鼠类名称</text>
  84. <text class="header-count">数 量</text>
  85. </view>
  86. <view class="result-row" v-for="(pest, index) in pestList" :key="index">
  87. <view class="pest-info">
  88. <view class="pest-color" :style="{ backgroundColor: pest.color }"></view>
  89. <view class="pest-text">
  90. <view class="pest-name">{{ pest.pest_name }}</view>
  91. <view class="pest-attrs" v-if="pest.attrs">{{ pest.attrs }}</view>
  92. </view>
  93. </view>
  94. <text class="pest-count">{{ pest.pest_num }}</text>
  95. </view>
  96. </view>
  97. <u-empty v-else text="暂无数据"></u-empty>
  98. </view>
  99. </view>
  100. </template>
  101. <script>
  102. export default {
  103. data() {
  104. return {
  105. title: '查看图片',
  106. addtime: '',
  107. group_id: '',
  108. // 图片来源角标:从监测图像(下)进入时为"下",从前后左右进入时为空(用图片自身type)
  109. photoType: '',
  110. // 该分组的所有图片
  111. thumbnails: [],
  112. currentIndex: 0,
  113. bugMarkers: [],
  114. imageWidth: 0,
  115. imageHeight: 0,
  116. containerHeight: '300px',
  117. // 主图缩放/位移受控值:双击复位 & 切图重置用(movable-view 缩放拖动后易视图卡住拖不回)
  118. movableX: 0,
  119. movableY: 0,
  120. movableScale: 1,
  121. lastTapTime: 0,
  122. pestList: [],
  123. colors: [
  124. '#FF5951',
  125. '#66EDED',
  126. '#E67B3E',
  127. '#6DE28B',
  128. '#FFC97A',
  129. '#E7EB4B',
  130. '#1561F3',
  131. '#FA73F5',
  132. '#159AFF',
  133. '#FA73F5'
  134. ],
  135. pestColorMap: {}, // 害虫颜色映射 {pestName: color}
  136. };
  137. },
  138. computed: {
  139. currentImg() {
  140. return this.thumbnails[this.currentIndex] || {};
  141. },
  142. // 角标内容:优先用页面带过来的"下",否则用当前图片的type(1/2/3/4)
  143. badgeText() {
  144. return this.photoType || this.currentImg.type || '';
  145. },
  146. },
  147. onLoad(options) {
  148. const { addtime, group_id } = options;
  149. this.addtime = Number(addtime) || 0;
  150. this.group_id = group_id || '';
  151. this.photoType = options.photo_type ? decodeURIComponent(options.photo_type) : '';
  152. // 取出列表页带过来的该分组全部图片
  153. try {
  154. const imgs = JSON.parse(uni.getStorageSync('shuhai_group_imgs') || '[]');
  155. this.thumbnails = (imgs || []).filter(img => img && img.addr);
  156. } catch (error) {
  157. this.thumbnails = [];
  158. }
  159. uni.removeStorageSync('shuhai_group_imgs');
  160. // 初始定位到点击的那张(监测图像(下)列表会带index)
  161. const index = Number(options.index) || 0;
  162. this.currentIndex = Math.min(Math.max(index, 0), Math.max(this.thumbnails.length - 1, 0));
  163. this.processBugMarkers();
  164. this.buildPestList();
  165. },
  166. methods: {
  167. selectThumbnail(index) {
  168. if (index === this.currentIndex) {
  169. return;
  170. }
  171. this.currentIndex = index;
  172. this.resetViewer();
  173. this.processBugMarkers();
  174. this.buildPestList();
  175. },
  176. // 双击主图复位缩放与位置:movable-view 缩放拖动后可能视图卡住、拖不回来
  177. onViewerTap() {
  178. const now = Date.now();
  179. if (now - this.lastTapTime < 300) {
  180. this.resetViewer();
  181. }
  182. this.lastTapTime = now;
  183. },
  184. resetViewer() {
  185. this.movableScale = 1;
  186. // 交替设一个极小值,保证属性变化能触发 movable-view 复位
  187. this.movableX = this.movableX === 0 ? 0.01 : 0;
  188. this.movableY = this.movableY === 0 ? 0.01 : 0;
  189. },
  190. // 根据当前图片的mark生成识别结果列表
  191. buildPestList() {
  192. const marks = this.currentImg.mark || [];
  193. const pestArr = new Map();
  194. marks.forEach(item => {
  195. const name = item.text || '未知';
  196. if (!pestArr.has(name)) {
  197. pestArr.set(name, {
  198. pest_name: name,
  199. pest_num: 0,
  200. attrs: this.getAttrs(item),
  201. });
  202. }
  203. pestArr.get(name).pest_num++;
  204. });
  205. this.pestList = [...pestArr.values()].map(item => ({
  206. ...item,
  207. color: this.getPestColor(item.pest_name),
  208. }));
  209. },
  210. // 拼接害虫属性信息(尾长/体长/重量,接口有字段才展示)
  211. getAttrs(mark) {
  212. const parts = [];
  213. if (mark.tail_length) parts.push('尾长:' + mark.tail_length);
  214. if (mark.body_length) parts.push('体长:' + mark.body_length);
  215. if (mark.weight) parts.push('重量:' + mark.weight);
  216. return parts.join(' ');
  217. },
  218. getPestColor(pestName) {
  219. // 如果该害虫已有颜色,直接返回
  220. if (this.pestColorMap[pestName]) {
  221. return this.pestColorMap[pestName];
  222. }
  223. // 为新害虫分配颜色
  224. const usedColors = Object.values(this.pestColorMap);
  225. let color;
  226. for (const c of this.colors) {
  227. if (!usedColors.includes(c)) {
  228. color = c;
  229. break;
  230. }
  231. }
  232. // 如果所有颜色都用完了,使用哈希值确定颜色
  233. if (!color) {
  234. const hash = pestName.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0);
  235. color = this.colors[hash % this.colors.length];
  236. }
  237. this.pestColorMap[pestName] = color;
  238. return color;
  239. },
  240. onImageLoad(e) {
  241. this.imageWidth = e.detail.width;
  242. this.imageHeight = e.detail.height;
  243. // 计算容器显示高度(根据屏幕宽度和图片宽高比)
  244. const systemInfo = uni.getSystemInfoSync();
  245. const screenWidth = systemInfo.windowWidth;
  246. const containerWidth = screenWidth - 24; // 减去左右margin (24rpx ≈ 12px)
  247. const displayHeight = (containerWidth / this.imageWidth) * this.imageHeight;
  248. this.containerHeight = displayHeight + 'px';
  249. // 图片加载完成后处理bugMarkers
  250. this.processBugMarkers();
  251. },
  252. // 根据mark的坐标信息生成标记框
  253. processBugMarkers() {
  254. const marks = this.currentImg.mark || [];
  255. if (!marks.length) {
  256. this.bugMarkers = [];
  257. return;
  258. }
  259. // 图片未加载完成时先给默认值
  260. if (!this.imageWidth || !this.imageHeight) {
  261. this.imageWidth = 1000;
  262. this.imageHeight = 1000;
  263. }
  264. const scaleRatio = 2.4;
  265. const markers = [];
  266. marks.forEach(item => {
  267. if (item.startX === undefined) {
  268. return;
  269. }
  270. const { startX, startY, text } = item;
  271. // 使用缩放比例计算标记位置和尺寸(负宽高表示从右下角画的框,做翻转)
  272. const x = item.width > 0 ? (startX * scaleRatio / this.imageWidth) * 105 : ((startX + item.width) * scaleRatio / this.imageWidth) * 105;
  273. const y = item.height > 0 ? (startY * scaleRatio / this.imageHeight) * 105 : ((startY + item.height) * scaleRatio / this.imageHeight) * 105;
  274. const width = (item.width > 0 ? item.width : -item.width) * scaleRatio / this.imageWidth * 100;
  275. const height = (item.height > 0 ? item.height : -item.height) * scaleRatio / this.imageHeight * 100;
  276. markers.push({
  277. id: text,
  278. x,
  279. y,
  280. width,
  281. height,
  282. color: this.getPestColor(text)
  283. });
  284. });
  285. this.bugMarkers = markers;
  286. },
  287. // 格式化时间
  288. formatDate(dateString) {
  289. const date = new Date(dateString * 1000);
  290. const year = date.getFullYear();
  291. const month = String(date.getMonth() + 1).padStart(2, '0');
  292. const day = String(date.getDate()).padStart(2, '0');
  293. const hour = String(date.getHours()).padStart(2, '0');
  294. const minute = String(date.getMinutes()).padStart(2, '0');
  295. const second = String(date.getSeconds()).padStart(2, '0');
  296. return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  297. },
  298. handleBack() {
  299. uni.navigateBack({
  300. delta: 1
  301. });
  302. },
  303. }
  304. };
  305. </script>
  306. <style lang="scss" scoped>
  307. .device-photo-page {
  308. background-color: #F5F5F5;
  309. min-height: 100vh;
  310. width: 100%;
  311. padding-top: 112rpx;
  312. padding-bottom: 32rpx;
  313. box-sizing: border-box;
  314. background: linear-gradient(180deg, #ffffff00 0%, #F5F6FA 23.64%, #F5F6FA 100%), linear-gradient(102deg, #BFEADD 6.77%, #B8F1E7 40.15%, #B9EEF5 84.02%);
  315. .device-detail__header {
  316. width: 100%;
  317. font-size: 28rpx;
  318. color: #042118;
  319. font-family: 'Source Han Sans CN VF';
  320. font-weight: 700;
  321. position: relative;
  322. text-align: center;
  323. .arrow-left {
  324. position: absolute;
  325. left: 32rpx;
  326. margin-right: 12rpx;
  327. }
  328. }
  329. /* 主图片区域 */
  330. .main-photo {
  331. position: relative;
  332. margin: 24rpx;
  333. background-color: #FFFFFF;
  334. border-radius: 16rpx;
  335. padding: 0;
  336. overflow: hidden;
  337. .movable-area {
  338. width: 100%;
  339. overflow: hidden;
  340. }
  341. .movable-view {
  342. width: 100%;
  343. // 显式高度:movable-view 依赖宽高计算各方向拖动边界,高度缺失会导致放大后无法上下拖动
  344. height: 100%;
  345. }
  346. .image-container {
  347. position: relative;
  348. width: 100%;
  349. }
  350. .main-photo-image {
  351. width: 100%;
  352. display: block;
  353. }
  354. /* 识别标记层 */
  355. .bug-markers {
  356. position: absolute;
  357. top: 0;
  358. left: 0;
  359. width: 100%;
  360. height: 100%;
  361. pointer-events: none;
  362. }
  363. /* 识别标记 */
  364. .bug-marker {
  365. position: absolute;
  366. border: 2rpx solid;
  367. border-radius: 4rpx;
  368. pointer-events: auto;
  369. }
  370. /* 识别标记标签 */
  371. .bug-label {
  372. position: absolute;
  373. top: -30rpx;
  374. left: 0;
  375. padding: 4rpx 8rpx;
  376. border-radius: 4rpx;
  377. font-size: 20rpx;
  378. font-weight: bold;
  379. white-space: nowrap;
  380. }
  381. .photo-timestamp {
  382. position: absolute;
  383. bottom: 0rpx;
  384. left: 0rpx;
  385. width: 100%;
  386. box-sizing: border-box;
  387. background-color: rgba(0, 0, 0, 0.6);
  388. color: #FFFFFF;
  389. padding: 12rpx 16rpx;
  390. font-size: 26rpx;
  391. }
  392. /* 图片来源角标 */
  393. .photo-type-badge {
  394. position: absolute;
  395. top: 16rpx;
  396. right: 16rpx;
  397. z-index: 10;
  398. background-color: rgba(0, 0, 0, 0.6);
  399. color: #FFFFFF;
  400. padding: 6rpx 16rpx;
  401. border-radius: 8rpx;
  402. font-size: 26rpx;
  403. }
  404. }
  405. .main-photo-empty {
  406. margin: 24rpx;
  407. padding: 120rpx 0;
  408. background-color: #FFFFFF;
  409. border-radius: 16rpx;
  410. }
  411. /* 缩略图预览 */
  412. .thumbnail-preview {
  413. margin: 0 24rpx 24rpx;
  414. .thumbnail-scroll {
  415. gap: 16rpx;
  416. padding-bottom: 16rpx;
  417. box-sizing: border-box;
  418. white-space: nowrap;
  419. overflow-x: auto;
  420. overflow-y: hidden;
  421. width: 100%;
  422. // 隐藏滚动条
  423. -ms-overflow-style: none;
  424. scrollbar-width: none;
  425. .thumbnail-item {
  426. width: 120rpx;
  427. height: 120rpx;
  428. border-radius: 8rpx;
  429. overflow: hidden;
  430. border: 4rpx solid transparent;
  431. display: inline-block;
  432. box-sizing: border-box;
  433. margin-right: 16rpx;
  434. &.active {
  435. border-color: #0BBC58;
  436. }
  437. .thumbnail-image {
  438. width: 100%;
  439. height: 100%;
  440. }
  441. }
  442. }
  443. }
  444. /* 识别结果 */
  445. .recognition-result {
  446. margin: 0 24rpx 24rpx;
  447. background-color: #FFFFFF;
  448. border-radius: 16rpx;
  449. padding: 24rpx;
  450. .result-title {
  451. font-size: 28rpx;
  452. font-weight: 700;
  453. color: #042118;
  454. margin-bottom: 24rpx;
  455. }
  456. .result-table {
  457. border: 1px solid #E4E7ED;
  458. border-radius: 12rpx;
  459. overflow: hidden;
  460. .result-header {
  461. display: flex;
  462. justify-content: space-between;
  463. align-items: center;
  464. background: #F6F8FC;
  465. padding: 18rpx 24rpx;
  466. font-size: 26rpx;
  467. font-weight: 700;
  468. color: #042118;
  469. }
  470. .result-row {
  471. display: flex;
  472. justify-content: space-between;
  473. align-items: center;
  474. padding: 18rpx 24rpx;
  475. border-top: 1px solid #EFF2F7;
  476. .pest-info {
  477. display: flex;
  478. align-items: center;
  479. .pest-color {
  480. width: 16rpx;
  481. height: 16rpx;
  482. border-radius: 50%;
  483. margin-right: 12rpx;
  484. flex-shrink: 0;
  485. }
  486. .pest-text {
  487. .pest-name {
  488. font-size: 26rpx;
  489. color: #042118;
  490. }
  491. .pest-attrs {
  492. font-size: 22rpx;
  493. color: #999999;
  494. margin-top: 6rpx;
  495. }
  496. }
  497. }
  498. .pest-count {
  499. font-size: 28rpx;
  500. font-weight: 500;
  501. color: #042118;
  502. }
  503. }
  504. }
  505. }
  506. }
  507. </style>