| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- <template>
- <view class="device-photo-page">
- <!-- 顶部导航栏 -->
- <view class="device-detail__header">
- <u-icon
- size="36"
- class="arrow-left"
- name="arrow-left"
- @click="handleBack"
- ></u-icon>
- 查看图片
- </view>
- <!-- 主图片区域 -->
- <view class="main-photo" v-if="currentImg.addr">
- <movable-area class="movable-area" :style="{ height: containerHeight }">
- <movable-view
- class="movable-view"
- direction="all"
- scale="true"
- scale-min="1"
- scale-max="5"
- >
- <view class="image-container">
- <image
- :src="currentImg.addr"
- class="main-photo-image"
- mode="widthFix"
- @load="onImageLoad"
- />
- <!-- 识别标记层 -->
- <view class="bug-markers" v-if="bugMarkers.length">
- <view
- v-for="(marker, index) in bugMarkers"
- :key="index"
- class="bug-marker"
- :style="{
- left: marker.x + '%',
- top: marker.y + '%',
- width: marker.width + '%',
- height: marker.height + '%',
- borderColor: marker.color
- }"
- >
- <view class="bug-label" :style="{ color: marker.color }">
- {{ marker.id }}
- </view>
- </view>
- </view>
- </view>
- </movable-view>
- </movable-area>
- <view class="photo-timestamp">{{ formatDate(addtime) }}</view>
- </view>
- <view class="main-photo-empty" v-else>
- <u-empty text="暂无图片"></u-empty>
- </view>
- <!-- 缩略图预览:当前分组的所有图片 -->
- <view class="thumbnail-preview" v-if="thumbnails.length">
- <view class="thumbnail-scroll">
- <view
- class="thumbnail-item"
- v-for="(item, index) in thumbnails"
- :key="index"
- :class="{ active: currentIndex == index }"
- @click="selectThumbnail(index)"
- >
- <image :src="item.addr" class="thumbnail-image" mode="aspectFill" />
- </view>
- </view>
- </view>
- <!-- 识别结果 -->
- <view class="recognition-result">
- <view class="result-title">当前图片识别结果</view>
- <view class="result-table" v-if="pestList.length">
- <view class="result-header">
- <text class="header-name">鼠类名称</text>
- <text class="header-count">数 量</text>
- </view>
- <view class="result-row" v-for="(pest, index) in pestList" :key="index">
- <view class="pest-info">
- <view class="pest-color" :style="{ backgroundColor: pest.color }"></view>
- <view class="pest-text">
- <view class="pest-name">{{ pest.pest_name }}</view>
- <view class="pest-attrs" v-if="pest.attrs">{{ pest.attrs }}</view>
- </view>
- </view>
- <text class="pest-count">{{ pest.pest_num }}</text>
- </view>
- </view>
- <u-empty v-else text="暂无数据"></u-empty>
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- title: '查看图片',
- addtime: '',
- group_id: '',
- // 该分组的所有图片
- thumbnails: [],
- currentIndex: 0,
- bugMarkers: [],
- imageWidth: 0,
- imageHeight: 0,
- containerHeight: '300px',
- pestList: [],
- colors: [
- '#FF5951',
- '#66EDED',
- '#E67B3E',
- '#6DE28B',
- '#FFC97A',
- '#E7EB4B',
- '#1561F3',
- '#FA73F5',
- '#159AFF',
- '#FA73F5'
- ],
- pestColorMap: {}, // 害虫颜色映射 {pestName: color}
- };
- },
- computed: {
- currentImg() {
- return this.thumbnails[this.currentIndex] || {};
- },
- },
- onLoad(options) {
- const { addtime, group_id } = options;
- this.addtime = Number(addtime) || 0;
- this.group_id = group_id || '';
- // 取出列表页带过来的该分组全部图片
- try {
- const imgs = JSON.parse(uni.getStorageSync('shuhai_group_imgs') || '[]');
- this.thumbnails = (imgs || []).filter(img => img && img.addr);
- } catch (error) {
- this.thumbnails = [];
- }
- uni.removeStorageSync('shuhai_group_imgs');
- this.currentIndex = 0;
- this.processBugMarkers();
- this.buildPestList();
- },
- methods: {
- selectThumbnail(index) {
- if (index === this.currentIndex) {
- return;
- }
- this.currentIndex = index;
- this.processBugMarkers();
- this.buildPestList();
- },
- // 根据当前图片的mark生成识别结果列表
- buildPestList() {
- const marks = this.currentImg.mark || [];
- const pestArr = new Map();
- marks.forEach(item => {
- const name = item.text || '未知';
- if (!pestArr.has(name)) {
- pestArr.set(name, {
- pest_name: name,
- pest_num: 0,
- attrs: this.getAttrs(item),
- });
- }
- pestArr.get(name).pest_num++;
- });
- this.pestList = [...pestArr.values()].map(item => ({
- ...item,
- color: this.getPestColor(item.pest_name),
- }));
- },
- // 拼接害虫属性信息(尾长/体长/重量,接口有字段才展示)
- getAttrs(mark) {
- const parts = [];
- if (mark.tail_length) parts.push('尾长:' + mark.tail_length);
- if (mark.body_length) parts.push('体长:' + mark.body_length);
- if (mark.weight) parts.push('重量:' + mark.weight);
- return parts.join(' ');
- },
- getPestColor(pestName) {
- // 如果该害虫已有颜色,直接返回
- if (this.pestColorMap[pestName]) {
- return this.pestColorMap[pestName];
- }
- // 为新害虫分配颜色
- const usedColors = Object.values(this.pestColorMap);
- let color;
- for (const c of this.colors) {
- if (!usedColors.includes(c)) {
- color = c;
- break;
- }
- }
- // 如果所有颜色都用完了,使用哈希值确定颜色
- if (!color) {
- const hash = pestName.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0);
- color = this.colors[hash % this.colors.length];
- }
- this.pestColorMap[pestName] = color;
- return color;
- },
- onImageLoad(e) {
- this.imageWidth = e.detail.width;
- this.imageHeight = e.detail.height;
- // 计算容器显示高度(根据屏幕宽度和图片宽高比)
- const systemInfo = uni.getSystemInfoSync();
- const screenWidth = systemInfo.windowWidth;
- const containerWidth = screenWidth - 24; // 减去左右margin (24rpx ≈ 12px)
- const displayHeight = (containerWidth / this.imageWidth) * this.imageHeight;
- this.containerHeight = displayHeight + 'px';
- // 图片加载完成后处理bugMarkers
- this.processBugMarkers();
- },
- // 根据mark的坐标信息生成标记框
- processBugMarkers() {
- const marks = this.currentImg.mark || [];
- if (!marks.length) {
- this.bugMarkers = [];
- return;
- }
- // 图片未加载完成时先给默认值
- if (!this.imageWidth || !this.imageHeight) {
- this.imageWidth = 1000;
- this.imageHeight = 1000;
- }
- const scaleRatio = 2.4;
- const markers = [];
- marks.forEach(item => {
- if (item.startX === undefined) {
- return;
- }
- const { startX, startY, text } = item;
- // 使用缩放比例计算标记位置和尺寸(负宽高表示从右下角画的框,做翻转)
- const x = item.width > 0 ? (startX * scaleRatio / this.imageWidth) * 105 : ((startX + item.width) * scaleRatio / this.imageWidth) * 105;
- const y = item.height > 0 ? (startY * scaleRatio / this.imageHeight) * 105 : ((startY + item.height) * scaleRatio / this.imageHeight) * 105;
- const width = (item.width > 0 ? item.width : -item.width) * scaleRatio / this.imageWidth * 100;
- const height = (item.height > 0 ? item.height : -item.height) * scaleRatio / this.imageHeight * 100;
- markers.push({
- id: text,
- x,
- y,
- width,
- height,
- color: this.getPestColor(text)
- });
- });
- this.bugMarkers = markers;
- },
- // 格式化时间
- formatDate(dateString) {
- const date = new Date(dateString * 1000);
- const year = date.getFullYear();
- const month = String(date.getMonth() + 1).padStart(2, '0');
- const day = String(date.getDate()).padStart(2, '0');
- const hour = String(date.getHours()).padStart(2, '0');
- const minute = String(date.getMinutes()).padStart(2, '0');
- const second = String(date.getSeconds()).padStart(2, '0');
- return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
- },
- handleBack() {
- uni.navigateBack({
- delta: 1
- });
- },
- }
- };
- </script>
- <style lang="scss" scoped>
- .device-photo-page {
- background-color: #F5F5F5;
- min-height: 100vh;
- width: 100%;
- padding-top: 112rpx;
- padding-bottom: 32rpx;
- box-sizing: border-box;
- background: linear-gradient(180deg, #ffffff00 0%, #F5F6FA 23.64%, #F5F6FA 100%), linear-gradient(102deg, #BFEADD 6.77%, #B8F1E7 40.15%, #B9EEF5 84.02%);
- .device-detail__header {
- width: 100%;
- font-size: 28rpx;
- color: #042118;
- font-family: 'Source Han Sans CN VF';
- font-weight: 700;
- position: relative;
- text-align: center;
- .arrow-left {
- position: absolute;
- left: 32rpx;
- margin-right: 12rpx;
- }
- }
- /* 主图片区域 */
- .main-photo {
- position: relative;
- margin: 24rpx;
- background-color: #FFFFFF;
- border-radius: 16rpx;
- padding: 0;
- overflow: hidden;
- .movable-area {
- width: 100%;
- overflow: hidden;
- }
- .movable-view {
- width: 100%;
- }
- .image-container {
- position: relative;
- width: 100%;
- }
- .main-photo-image {
- width: 100%;
- display: block;
- }
- /* 识别标记层 */
- .bug-markers {
- position: absolute;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- pointer-events: none;
- }
- /* 识别标记 */
- .bug-marker {
- position: absolute;
- border: 2rpx solid;
- border-radius: 4rpx;
- pointer-events: auto;
- }
- /* 识别标记标签 */
- .bug-label {
- position: absolute;
- top: -30rpx;
- left: 0;
- padding: 4rpx 8rpx;
- border-radius: 4rpx;
- font-size: 20rpx;
- font-weight: bold;
- white-space: nowrap;
- }
- .photo-timestamp {
- position: absolute;
- bottom: 0rpx;
- left: 0rpx;
- width: 100%;
- box-sizing: border-box;
- background-color: rgba(0, 0, 0, 0.6);
- color: #FFFFFF;
- padding: 12rpx 16rpx;
- font-size: 26rpx;
- }
- }
- .main-photo-empty {
- margin: 24rpx;
- padding: 120rpx 0;
- background-color: #FFFFFF;
- border-radius: 16rpx;
- }
- /* 缩略图预览 */
- .thumbnail-preview {
- margin: 0 24rpx 24rpx;
- .thumbnail-scroll {
- gap: 16rpx;
- padding-bottom: 16rpx;
- box-sizing: border-box;
- white-space: nowrap;
- overflow-x: auto;
- overflow-y: hidden;
- width: 100%;
- // 隐藏滚动条
- -ms-overflow-style: none;
- scrollbar-width: none;
- .thumbnail-item {
- width: 120rpx;
- height: 120rpx;
- border-radius: 8rpx;
- overflow: hidden;
- border: 4rpx solid transparent;
- display: inline-block;
- box-sizing: border-box;
- margin-right: 16rpx;
- &.active {
- border-color: #0BBC58;
- }
- .thumbnail-image {
- width: 100%;
- height: 100%;
- }
- }
- }
- }
- /* 识别结果 */
- .recognition-result {
- margin: 0 24rpx 24rpx;
- background-color: #FFFFFF;
- border-radius: 16rpx;
- padding: 24rpx;
- .result-title {
- font-size: 28rpx;
- font-weight: 700;
- color: #042118;
- margin-bottom: 24rpx;
- }
- .result-table {
- border: 1px solid #E4E7ED;
- border-radius: 12rpx;
- overflow: hidden;
- .result-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- background: #F6F8FC;
- padding: 18rpx 24rpx;
- font-size: 26rpx;
- font-weight: 700;
- color: #042118;
- }
- .result-row {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 18rpx 24rpx;
- border-top: 1px solid #EFF2F7;
- .pest-info {
- display: flex;
- align-items: center;
- .pest-color {
- width: 16rpx;
- height: 16rpx;
- border-radius: 50%;
- margin-right: 12rpx;
- flex-shrink: 0;
- }
- .pest-text {
- .pest-name {
- font-size: 26rpx;
- color: #042118;
- }
- .pest-attrs {
- font-size: 22rpx;
- color: #999999;
- margin-top: 6rpx;
- }
- }
- }
- .pest-count {
- font-size: 28rpx;
- font-weight: 500;
- color: #042118;
- }
- }
- }
- }
- }
- </style>
|