get-image-info.uvue 3.9 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
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
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <view class="uni-title">
        <text class="uni-subtitle-text">获取本地相对路径图片信息</text>
      </view>
      <image class="image" :src="relativeImagePath" mode="aspectFit"></image>
      <text class="margin-top-10">{{absoluteImageInfo}}</text>
      <view class="uni-title">
        <text class="uni-subtitle-text">获取网络路径图片信息</text>
      </view>
      <image class="image" :src="remoteImagePath" mode="aspectFit"></image>
      <text class="margin-top-10">{{remoteImageInfo}}</text>
      <view class="uni-title">
        <text class="uni-subtitle-text">获取本地绝对路径图片信息</text>
      </view>
      <image class="image" :src="absoluteImagePath" mode="aspectFit"></image>
      <text class="margin-top-10">{{relativeImageInfo}}</text>
      <view class="uni-btn-v">
        <button type="primary" @click="chooseImage">拍摄照片或从相册中选择照片</button>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: "getImageInfo",
        relativeImagePath: "/static/test-image/logo.png",
        relativeImageInfo: "",
        absoluteImagePath: "",
        absoluteImageInfo: "",
        remoteImagePath: "https://qiniu-web-assets.dcloud.net.cn/uni-app-x/static/img/building.jpg",
        remoteImageInfo: "",
42 43
        // 自动化测试
        imageInfoForTest: null,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
44 45 46 47 48 49
      }
    },
    methods: {
      chooseImage() {
        uni.chooseImage({
          count: 1,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
50 51
          success: (res) => {
            this.absoluteImagePath = res.tempFilePaths[0];
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
52
            uni.getImageInfo({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
53 54 55 56 57 58 59 60 61 62 63
              src: res.tempFilePaths[0],
              success: (_res) => {
                console.log("getImageInfo success", JSON.stringify(res));
                this.relativeImageInfo = `图片宽度: ${_res.width}\n图片高度: ${_res.height}\n图片路径: ${_res.path}\n图片方向: ${_res.orientation}\n图片格式: ${_res.type}`;
              },
              fail: (err) => {
                uni.showModal({
                  title: "获取图片信息失败",
                  content: JSON.stringify(err),
                  showCancel: false
                });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
64 65 66 67 68 69 70 71 72 73
              }
            });
          }
        });
      }
    },
    onReady() {
      uni.getImageInfo({
        src: this.relativeImagePath,
        success: (res) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
74
          console.log("getImageInfo success", JSON.stringify(res));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
75
          this.absoluteImageInfo = `图片宽度: ${res.width}\n图片高度: ${res.height}\n图片路径: ${res.path}\n图片方向: ${res.orientation}\n图片格式: ${res.type}`;
76 77 78
          this.imageInfoForTest = {
            "width": res.width,
            "height": res.height,
79
            "path": res.path.slice(res.path.indexOf('/static')),
80 81 82
            "orientation": res.orientation,
            "type": res.type
          };
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
83 84 85 86 87 88 89
        },
        fail: (err) => {
          uni.showModal({
            title: "获取图片信息失败",
            content: JSON.stringify(err),
            showCancel: false
          });
90
          this.imageInfoForTest = null;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
91 92 93 94 95
        }
      });
      uni.getImageInfo({
        src: this.remoteImagePath,
        success: (res) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
96
          console.log("getImageInfo success", JSON.stringify(res));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
97
          this.remoteImageInfo = `图片宽度: ${res.width}\n图片高度: ${res.height}\n图片路径: ${res.path}\n图片方向: ${res.orientation}\n图片格式: ${res.type}`;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
98 99 100 101 102 103 104
        },
        fail: (err) => {
          uni.showModal({
            title: "获取图片信息失败",
            content: JSON.stringify(err),
            showCancel: false
          });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        }
      });
    }
  }
</script>

<style>
  .image {
    align-self: center;
  }

  .margin-top-10 {
    margin-top: 10px;
  }
</style>