get-image-info.uvue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex:1">
  4. <!-- #endif -->
  5. <page-head :title="title"></page-head>
  6. <view class="uni-padding-wrap">
  7. <view class="uni-title">
  8. <text class="uni-subtitle-text">获取本地相对路径图片信息</text>
  9. </view>
  10. <image class="image" :src="relativeImagePath" mode="aspectFit"></image>
  11. <text class="margin-top-10">{{absoluteImageInfo}}</text>
  12. <view class="uni-title">
  13. <text class="uni-subtitle-text">获取网络路径图片信息</text>
  14. </view>
  15. <image class="image" :src="remoteImagePath" mode="aspectFit"></image>
  16. <text class="margin-top-10">{{remoteImageInfo}}</text>
  17. <view class="uni-title">
  18. <text class="uni-subtitle-text">获取本地绝对路径图片信息</text>
  19. </view>
  20. <image class="image" :src="absoluteImagePath" mode="aspectFit"></image>
  21. <text class="margin-top-10">{{relativeImageInfo}}</text>
  22. <view class="uni-btn-v">
  23. <button type="primary" @click="chooseImage">拍摄照片或从相册中选择照片</button>
  24. </view>
  25. </view>
  26. <!-- #ifdef APP -->
  27. </scroll-view>
  28. <!-- #endif -->
  29. </template>
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. title: "getImageInfo",
  35. relativeImagePath: "/static/test-image/logo.png",
  36. relativeImageInfo: "",
  37. absoluteImagePath: "",
  38. absoluteImageInfo: "",
  39. remoteImagePath: "https://qiniu-web-assets.dcloud.net.cn/uni-app-x/static/img/building.jpg",
  40. remoteImageInfo: "",
  41. // 自动化测试
  42. imageInfoForTest: null as UTSJSONObject | null,
  43. }
  44. },
  45. methods: {
  46. chooseImage() {
  47. uni.chooseImage({
  48. count: 1,
  49. success: (res) => {
  50. this.absoluteImagePath = res.tempFilePaths[0];
  51. uni.getImageInfo({
  52. src: res.tempFilePaths[0],
  53. success: (_res) => {
  54. console.log("getImageInfo success", JSON.stringify(_res));
  55. this.relativeImageInfo = `图片宽度: ${_res.width}\n图片高度: ${_res.height}\n图片路径: ${_res.path}\n图片方向: ${_res.orientation}\n图片格式: ${_res.type}`;
  56. },
  57. fail: (err) => {
  58. uni.showModal({
  59. title: "获取图片信息失败",
  60. content: JSON.stringify(err),
  61. showCancel: false
  62. });
  63. }
  64. });
  65. }
  66. });
  67. }
  68. },
  69. onReady() {
  70. uni.getImageInfo({
  71. src: this.relativeImagePath,
  72. success: (res) => {
  73. console.log("getImageInfo success", JSON.stringify(res));
  74. this.absoluteImageInfo = `图片宽度: ${res.width}\n图片高度: ${res.height}\n图片路径: ${res.path}\n图片方向: ${res.orientation}\n图片格式: ${res.type}`;
  75. this.imageInfoForTest = {
  76. "width": res.width,
  77. "height": res.height,
  78. "path": res.path.slice(res.path.indexOf('static/') + 'static/'.length),
  79. "orientation": res.orientation,
  80. "type": res.type
  81. };
  82. },
  83. fail: (err) => {
  84. uni.showModal({
  85. title: "获取图片信息失败",
  86. content: JSON.stringify(err),
  87. showCancel: false
  88. });
  89. this.imageInfoForTest = null;
  90. }
  91. });
  92. uni.getImageInfo({
  93. src: this.remoteImagePath,
  94. success: (res) => {
  95. console.log("getImageInfo success", JSON.stringify(res));
  96. this.remoteImageInfo = `图片宽度: ${res.width}\n图片高度: ${res.height}\n图片路径: ${res.path}\n图片方向: ${res.orientation}\n图片格式: ${res.type}`;
  97. },
  98. fail: (err) => {
  99. uni.showModal({
  100. title: "获取图片信息失败",
  101. content: JSON.stringify(err),
  102. showCancel: false
  103. });
  104. }
  105. });
  106. }
  107. }
  108. </script>
  109. <style>
  110. .image {
  111. align-self: center;
  112. }
  113. .margin-top-10 {
  114. margin-top: 10px;
  115. }
  116. </style>