tt.getImageInfo
收藏
我的收藏

基础库 1.10.0 开始支持本方法,这是一个异步方法。

获取图片信息。

前提条件
业务背景
使用限制
注意事项
支持沙盒
相关教程

语法

tt.getImageInfo(options)

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
srcstring

图片路径,可以是相对路径、临时文件路径或存储文件路径,如果是网络图片请参考获取网络图片信息,可通过 tt.chooseImage 获取相册或相机拍摄的图片路径

1.10.0
successfunction
接口调用成功的回调函数
1.10.0
failfunction
接口调用失败的回调函数
1.10.0
completefunction
接口调用结束的回调函数(调用成功、失败都会执行)
1.10.0

回调成功

object 类型,属性如下:

属性名类型说明最低支持版本
pathstring
返回图片的本地路径,若为网络图片则该 API 首先会下载图片再返回下载到本地后的临时路径
1.10.0
typestring
图片格式
1.10.0
widthnumber
图片宽度,单位 px
1.10.0
heightnumber
图片高度,单位 px
1.10.0
orientationenum

拍照时设备方向,合法值参考 orientation 的合法值。orientation 的介绍请参考链接

2.29.0
errMsgstring
"getImageInfo:ok"
2.29.0

orientation 的合法值

说明最低支持版本
right
顺时针旋转 90 度,对应 Exif 中的 6
2.29.0
left
逆时针旋转 90 度,对应 Exif 中的 8
2.29.0
up
默认方向(手机横持拍照),对应 Exif 中的 1。或无 orientation 信息。
2.29.0
up-mirrored
同 up,但镜像翻转,对应 Exif 中的 2
2.29.0
down
旋转 180 度,对应 Exif 中的 3
2.29.0
down-mirrored
同 down,但镜像翻转,对应 Exif 中的 4
2.29.0
left-mirrored
同 left,但镜像翻转,对应 Exif 中的 5
2.29.0
right-mirrored
同 right,但镜像翻转,对应 Exif 中的 7
2.29.0

回调失败

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"getImageInfo:fail " + 详细错误信息
1.10.0

错误码

errorCodeerrMsgerrorType说明最低支持版本
110499params src is requireD
src 数组为空
1.10.0
110404sandbox is nilF
小程序框架内部错误,有需要请拉客服咨询
1.10.0
110403permission denied, read {filePath}D
传入了非法路径
1.10.0
110401no such file or directory {filePath}D
传入了不存在的路径
1.10.0
110402decode image failD
传入的路径非图片
1.10.0
110491Internal error: Failed to get image info.internal errorF
小程序框架内部错误,有需要请拉客服咨询
1.10.0
110405download image failU
图片下载失败
1.10.0

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

<!-- index.ttml -->
<image src="{{image}}" mode="widthFix" bindtap="previewImage"></image>
<button type="primary" bindtap="getImageInfo">获取上面的图片信息</button>
<view>图片信息:{{imageInfo}}</view>
// index.js
Page({
  data: {
    image: "https://s3.pstatp.com/toutiao/static/img/logo.201f80d.png",
    imageInfo: "",
  },
  getImageInfo() {
    tt.getImageInfo({
      src: this.data.image, // 也可以为本地路径,相对路径或临时文件路径
      success: (res) => {
        tt.showToast({ title: "获取成功" });
        let { type, width, height, path } = res;
        let imageInfo = `图片类型: ${type}; 图片宽度: ${width}px; 图片高度: ${height}px; 图片路径: ${path}`;
        this.setData({
          imageInfo,
        });
      },
      fail: (err) => {
        tt.showModal({
          title: "获取失败",
          content: err.errMsg,
          showCancel: false,
        });
      },
      complete() {
        console.log("获取完成");
      },
    });
  },
});

获取网络图片信息​

如果要获取网络图片的信息,需要预先在开发者平台配置 download 的域名白名单。具体步骤为:开发者平台 -> 选择目标小程序 -> 开发设置 -> 服务器域名 -> downloadFile 合法域名。​