返回插件列表

MImagePicker

图片选取器:包含拍照,录制视频,相册选图,图片和视频处理、获取图片和视频信息、图片涂鸦、文本、剪裁,水印相机等功能。

注意事项

errorCode 说明
-1 普通错误,错误具体内容参考errMsg
999 取消操作
1001 调用插件接口传参错误, (参数不合法,参数不匹配,参数校验失败等)
1100 用户拒绝权限
1101 用户取消权限,仅支持Android

iOS 注意事项

  1. iOS端仅支持iOS10.0(含)以上的系统。
  2. 接口示例代码中cordova.file.externalDataDirectory路径仅适用于Android,更多关于Android与iOS本地存储目录请参考这里

Android 注意事项

  1. 图片处理视频处理saveToAlbum如果为true,那么图片和视频path必须填写非应用路径,如填写cordova.file.externalRootDirectory + '目录\文件名',才能将资源更新到相册。
  2. 使用过程中,若修改系统UI参数,如系统按键或屏幕默认分辨率,会导致当前页面的自定义UI失效,需要再次打开水印相机才能恢复正常
  3. 水印中文字的字号随水印尺寸变化,若文字较小或模糊,可通过放大水印尺寸优化;水印尺寸size设置越大,字体呈现效果越好

目录

  1. 打开相机拍照或者拍摄视频
  2. 从相册选择图片
  3. 从相册选择视频和图片
  4. 图片处理
  5. 视频处理
  6. 图片编辑
  7. 获取图片信息
  8. 获取视频信息
  9. 将图片、视频存入系统相册或指定相册
  10. 打开水印相机
  11. 关闭水印相机
  12. 更新水印相机同步状态

API

打开相机拍照或者拍摄视频

  1. 自定义相机效果
  2. 图片编辑效果图
  3. 自定义相机不带系统调校(如广角、光学防抖、微距等),无法设置参数,对成像要求较高的建议使用系统相机

MImagePicker.openCamera(path, success, error, options);
参数 类型 必填 作用
path string 图片存储路径
success(path) function 成功回调,path为操作完成后保存的图片路径(即接口传过来的path)
error(err) function 失败回调,err为详细的错误信息,是一个json对象,例如:{"errCode": -1, "errMsg":"some explain of error."}
options json 可选参数。详情请参照下方options结构说明

options结构说明

字段名称 类型 说明
sourceType string 用来区分拍照录制视频,分别对应传参photovideo,不传或传其他值时默认为photo
photoConfig json sourceTypephoto时的可选参数,photoConfig释义如下
videoConfig json sourceTypevideo时的可选参数,videoConfig释义如下

photoConfig结构说明

字段名称 类型 说明
edit boolean 是否启用图片编辑功能,默认false不启用
customCamera boolean 进入自定义拍照页面开关。true:进入自定义拍照页面;false:进入系统拍照页面;默认值false
editConfig json 编辑图片功能配置,editConfig释义如下

editConfig 结构说明

字段名称 类型 说明
cropAspectRatio string 裁剪框宽高比,格式:整形数字:整形数字,如"1:1","4:3","16:9";格式不合法或不传该字段时用户可自由调整宽高比。1.2.5版本开始生效
limits array 图片编辑功能配置项,仅在edit=true时生效。
必须开启一个功能,否则忽略limits参数,开启所有功能
可传入参数释义如下:limit参数
autoSelected string 自动选中激活编辑功能
默认所有编辑功能都不主动激活
可传入参数释义如下:limit参数
limit 说明
shape 图形,如果没有传入,隐藏此功能
doodle 涂鸦,如果没有传入,隐藏此功能
text 添加文字,如果没有传入,隐藏此功能
crop 裁剪,如果没有传入,隐藏此功能
mosaic 马赛克,如果没有传入,隐藏此功能

videoConfig结构说明

字段名称 类型 说明
recordDuration int 视频录制时长,单位,默认10秒,Android端因厂商ROM定制,部分设备该参数设置后无效
videoQuality int 视频质量参数,仅iOS端生效。默认参数为1中等质量
可选参数为0-5:
0:
1:
2:
3:640x480
4:1280x720
5:960x540
超出可选参数范围,则使用默认质量,使用时请注意质量与序号的对应

调用示例

// 拍照

MImagePicker.openCamera(
  // cordova.file.externalDataDirectory仅适用于Android,iOS本地路径参数请查阅本文档注意事项部分说明
  cordova.file.externalDataDirectory + 'test.jpg', 
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  },
  {
    sourceType: 'photo',
    photoConfig: {
      edit: true,
      customCamera: true,
      editConfig:
      {
        limits: ['shape', 'doodle','text','crop','mosaic'],
        cropAspectRatio: "1:1"
      }
    }
  }
);

// 录制视频
MImagePicker.openCamera(
  // cordova.file.externalDataDirectory仅适用于Android,iOS本地路径参数请查阅本文档注意事项部分说明
  cordova.file.externalDataDirectory + 'test.mp4',
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  },
  {
    sourceType: 'video',
    videoConfig: {
      recordDuration: 10,
      videoQuality:1
    }
  }
);

从相册选择图片

MImagePicker.openAlbum(paths, success, error, options);
参数 类型 必填 作用
paths array 图片存储路径列表,paths内容为存放图片的绝对路径(须含文件名)。
Tips:
1、系统相册仅支持图片单选,若paths中有多个内容,则取paths[0]
2、自定义相册支持图片单选多选,当paths.length > 1时启用多选功能
success(result) function 成功回调,result为结果数组(result.length ≤ paths.length)
result内容:
[{
 "code":0, //code为0是图片选取成功,非0时均为失败;
 "result":"path", //图片存储路径,从前端传入paths遍历后得到
 "errMsg":"失败错误具体原因",
 "size":文件size,单位byte
}]
error(err) function 失败回调,err为详细的错误信息,是一个json对象,例如:{"errCode": -1, "errMsg":"some explain of error."}
options json 可选参数。详情请参照下方options结构说明

options结构说明

字段名称 类型 说明
customMode boolean 是否启用自定义相册,为true时启用自定义相册,false启用系统相册,不传或传其他值时默认false
edit boolean 是否启用图片编辑功能,默认false不启用
editConfig json 编辑图片功能配置项editConfig

调用示例

// cordova.file.externalDataDirectory仅适用于Android,iOS本地路径参数请查阅本文档注意事项部分说明
MImagePicker.openAlbum(
  [
    cordova.file.externalDataDirectory + 'test1.jpg',
    cordova.file.externalDataDirectory + 'test2.jpg',
    cordova.file.externalDataDirectory + 'test3.jpg',
    cordova.file.externalDataDirectory + 'test4.jpg',
  ],
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  },
  {
    customMode: true,
    edit: true,
    editConfig:
    {
      limits: ['shape', 'doodle','text','crop','mosaic'],
      cropAspectRatio: "4:3"
    }
  }
);

从相册选择视频和图片

MImagePicker.openAlubmForVideosAndImages(paths, success, error, options);
参数 类型 必填 作用
paths array 文件存储路径列表。详情请参考下方paths规则说明
success(result) function 成功回调,result为结果数组(result.length ≤ paths.length)
result内容:
[{
 "code":0, //code为0是视频选取成功,非0时均为失败;
 "result":"path", //视频存储路径,从前端传入paths遍历后得到
 "errMsg":"失败错误具体原因",
 "size":文件size,单位byte
}]
error(err) function 失败回调,err为详细的错误信息,是一个json对象,例如:{"errCode": -1, "errMsg":"some explain of error."}
options json 可选参数。详情请参照下方options结构说明

options结构说明

字段名称 类型 说明
maxVPSize int 最大可选数量,必须小于等于paths的长度
edit boolean 是否启用图片编辑功能,只能编辑图片,视频无法编辑,默认false不启用
editConfig json 编辑图片功能配置项editConfig

paths规则说明

调用示例

// cordova.file.externalDataDirectory仅适用于Android,iOS本地路径参数请查阅本文档注意事项部分说明
MImagePicker.openAlubmForVideosAndImages(
  [
    cordova.file.externalDataDirectory + 'test1.mp4',
    cordova.file.externalDataDirectory + 'test2.mp4',
    cordova.file.externalDataDirectory + 'test3.jpg',
    cordova.file.externalDataDirectory + 'test4.jpg',
    cordova.file.externalDataDirectory + 'test5.jpg',
  ],
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  },
  {
    maxVPSize: 3,
    edit: true,
    editConfig:{
       limits: ['shape', 'doodle','text','crop','mosaic'],
       cropAspectRatio: "4:3"
    }
  }
);

图片处理

MImagePicker.imageProcess(path, success, error, options);
参数 类型 必填 作用
path string 需要处理的图片绝对路径
success(path) function 成功回调。path为处理后的图片路径
error(err) function 失败回调,err为详细的错误信息,是一个json对象,例如:{"errCode": -1, "errMsg":"some explain of error."}
options json 可选参数。详情请参照下方options结构说明

options结构说明

字段名称 类型 说明
saveToAlbum boolean 是否保存到相册,true: 存到相册;false:不存入相册;不传则默认为false。
ratio float 图片压缩比例(以减小清晰度的代价,有助于减小物理空间),范围0 ~ 1,压缩程度与ratio成反比,默认0.8
width int 图片宽度,传入值宽高与图片真实宽高比率对比,取最大值对图片进行宽高等比压缩,最终值可能与传入值有偏差;
默认值:
iOS:屏幕宽*手机缩放比率,例如:iPhone 6plus默认屏幕的3倍大小,iPhone5默认屏幕的2倍大小;
Android:1080px
height int 图片高度,传入值宽高与图片真实宽高比率对比,取最大值对图片进行宽高等比压缩,最终值可能与传入值有偏差;
默认值:
iOS:屏幕宽*手机缩放比率,例如:iPhone 6plus默认屏幕的3倍大小,iPhone5默认屏幕的2倍大小;
Android:1920px
keepOrigin boolean 是否保持原图,为true时,不做缩放,默认false
watermarkConfig json 图片水印,详细配置如下

注意:widthheight如果小于待压缩图片尺寸,那么图片宽高不做处理

watermarkConfig参数字段说明

字段名称 类型 说明
textBackground string 文字整行的背景颜色,格式为16进制,推荐值(即默认值):#40000000
textAlignment string 标识文本在水平方向的对齐方式,竖直方向居中显示;
可取值:
left:文本靠左对齐;
center:文本居中对齐;
right:文本靠右对齐;
传错或不传则默认为left。
boardPosition string 标识整个水印背景框在竖直方向的位置;
可取值:
top:水印添加在屏幕顶部;
center:水印添加在屏幕中间;
bottom:水印添加在屏幕底部;
传错或不传则默认为bottom。
textColumns array 一个json下标对象显示一行,从上往下顺序显示.文本行数随内容变化,超出部分省略号显示,
示例:
[
 {
  "iconPath":"图标路径,只支持本地",
  "text":"文本说明",
  "textColor":"文本颜色,格式为16进制,默认值:#FFFFFF"
 },
 ...
]

调用示例

MImagePicker.imageProcess(
  // cordova.file.externalDataDirectory仅适用于Android,iOS本地路径参数请查阅本文档注意事项部分说明
  cordova.file.externalDataDirectory + 'test.jpg',
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  },
  {
    saveToAlbum: true,
    ratio: 0.8,
    width: 1080,
    height: 1920,
    keepOrigin: false,
    watermarkConfig: {
      textBackground: '#40000000',
      textAlignment: 'left',
      boardPosition: 'bottom',
      textColumns: [
        {
          iconPath: cordova.file.externalDataDirectory + 'icon1.jpg',
          text: '第 ' + 1 + ' 行水印',
          textColor: '#FFFFFF'
        },
        {
          iconPath: cordova.file.externalDataDirectory + 'icon2.jpg',
          text: '第 ' + 2 + ' 行水印',
          textColor: '#FFFFFF'
        },
      ]
    }
  }
);

视频处理

MImagePicker.videoProcess(path, success, error, options);

在Android中,该接口仅支持5.0+系统

参数 类型 必填 作用
path string 待处理视频绝对路径
success(path) function 成功回调。path为处理后的视频路径
error(err) function 失败回调,err为详细的错误信息,是一个json对象,例如:{"errCode": -1, "errMsg":"some explain of error."}
options json 可选参数。详情请参照下方options结构说明

options结构说明

字段名称 类型 说明
saveToAlbum boolean 是否保存到相册,true: 存到相册;false:不存入相册;不传则默认为false。
ratio float 视频质量压缩。可选范围0 ~ 1,压缩程度与ratio成反比,默认0.8仅支持Android

调用示例

MImagePicker.videoProcess(
  // cordova.file.externalDataDirectory仅适用于Android,iOS本地路径参数请查阅本文档注意事项部分说明
  cordova.file.externalDataDirectory + 'test.mp4',
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  },
  {
    saveToAlbum: true,
    ratio: 0.8,
  }
);

图片编辑

MImagePicker.editImage(pathGroups, success, error, options);
参数 类型 必填 作用
pathGroups array 待编辑图片路径&已编辑图片路径pathGroups
success(result) function 成功回调。result参照下方说明
error(err) function 失败回调,err为详细的错误信息,是一个json对象,例如:{"errCode": -1, "errMsg":"some explain of error."}
options json 可选参数。可配置项与editConfig一致

pathGroups结构说明

[
  {
    "srcPath": "src_image_01.png",// 待编辑图片路径
    "destPath": "dest_image_02.png"// 编辑后的图片存放路径
  },
  {
    "srcPath": "src_image_01.png",
    "destPath": "dest_image_02.png"
  }
]

result结构说明

[
  {
    "code": -1,// 非0时操作失败
    "errMsg": "图片不存在",// 操作失败具体原因
    "srcPath": "src_image_01.png",// 待编辑图片路径
  },
  {
    "code": 0,// 为0时操作成功
    "srcPath": "src_image_01.png",// 待编辑图片路径
    "destPath": "dest_image_02.png"// 编辑后的图片存放路径(仅当图片编辑成功后该值才有内容返回)
  }
]

调用示例

// cordova.file.externalDataDirectory仅适用于Android,iOS本地路径参数请查阅本文档注意事项部分说明
MImagePicker.editImage(
  [
    {
      srcPath: cordova.file.externalDataDirectory + 'test1.jpg',
      destPath: cordova.file.externalDataDirectory + 'edit_test1.jpg'
    },
    {
      srcPath: cordova.file.externalDataDirectory + 'test2.jpg',
      destPath: cordova.file.externalDataDirectory + 'edit_test2.jpg'
    }
  ],
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  },
  {
    limits: ['shape', 'doodle','text','crop','mosaic'],
    cropAspectRatio: "4:3"
  }
)

获取图片信息

MImagePicker.getImageInfo(path, success, error);
参数 类型 必填 作用
path string 待获取信息的图片路径
success(result) function 成功回调。result参照下方说明
error(err) function 失败回调,err为详细的错误信息,是一个json对象,例如:{"errCode": -1, "errMsg":"some explain of error."}

result说明

{
  "width": 1080,// 照片宽度
  "height": 1920,// 照片高度
  "size": 1024,// 照片占用内存大小,单位:Byte
  "dateTime": "2019:03:07 18:05:20",// 照片拍摄时间,格式:"yyyy:mm:dd HH:mm:ss"
}

调用示例

MImagePicker.getImageInfo(
  cordova.file.externalDataDirectory + 'test.jpg',
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  }
);

获取视频信息

MImagePicker.getVideoInfo(path, success, error, options);
参数 类型 必填 作用
path string 待获信息的视频路径
success(result) function 成功回调。result参照下方说明
error(err) function 失败回调,err为详细的错误信息,是一个json对象,例如:{"errCode": -1, "errMsg":"some explain of error."}
options json 可选参数。详情请参照下方options结构说明

options结构说明

字段名称 类型 说明
firstFramePath string 预览图片(即视频首帧)存储地址,为绝对路径

result说明

{
  "width": 1080,// 帧宽度
  "height": 1920,// 帧高度
  "size": 1024,// 视频占用内存大小,单位:Byte
  "dateTime": "2019:03:07 18:05:20",// 照片拍摄时间,格式:"yyyy:mm:dd HH:mm:ss"
  "rotate": 90,// 视频旋转角度
  "duration": 10, // 视频时长,单位:毫秒
  "firstFramePath": "xxxx", // 视频首帧存放路径,绝对路径
}

调用示例


  // cordova.file.externalDataDirectory仅适用于Android,iOS本地路径参数请查阅本文档注意事项部分说明


MImagePicker.getVideoInfo(
  cordova.file.externalDataDirectory + 'test.mp4',
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  },
  {
    firstFramePath: cordova.file.externalDataDirectory + 'video_frame.jpg',
  }
);

将图片、视频存入系统相册或指定相册

MImagePicker.saveToAlbum(path, success, error, group);
参数 类型 必填 作用
path string 图片或视频的绝对路径
group string 相册名,
如果不传则存入系统相册;
如果没有这个相册,则会创建。
success function 成功回调
error(err) function 失败回调,err为详细的错误信息,是一个json对象,例如:{"errCode": -1, "errMsg":"some explain of error."}

调用示例

// cordova.file.externalDataDirectory仅适用于Android,iOS本地路径参数请查阅本文档注意事项部分说明
MImagePicker.saveToAlbum(
  cordova.file.externalDataDirectory + 'test.mp4',
  function (result) {
    console.log('success', result)
  },
  function (error) {
    console.log('error:', error)
  },
  "AppCloud"
);

打开水印相机

MImagePicker.openWatermarkCamera(cameraParams, callback);
参数 类型 必填 作用
cameraParams json 水印相机相关参数,详情请参照下方cameraParams结构说明
callback object 水印相机回调,详情请参照下方callback结构说明

cameraParams结构说明

参数 类型 必填 作用
ratio float 相机预览与图片尺寸,不传为默认铺满屏幕比例
thumbnail string 相机页面左下角缩略图路径,仅支持本地路径,不传或传错将不显示
storeDirectory string 图片存储目录,不传时默认为应用沙盒目录
syncButtonIconPath string 水印相机右下角同步按钮图标路径,不传或传错时为默认图标
hideSyncButton boolean 隐藏同步按钮,默认false不隐藏
hideWatermarkSettingButton boolean 隐藏水印设置按钮,默认false不隐藏
watermarkConfig json 不传不显示水印,水印配置相关参数,详情请参照下方 watermarkConfig结构说明

watermarkConfig结构说明

参数 类型 必填 作用
size float 水印尺寸比例,以画面预览区域宽度为基础,默认0.75,范围 0~1
opacity float 水印不透明度,默认0.5,范围 0~1
title string 项目名称
items array 项目具体内容, 示例[[{"title": "编号", "content": "123-456-789"}], [{"title": "计数", "content": "8"}]]
注意:title的value限制长度为4,字符长度超过4时,可能会出现省略号……

callback结构说明

参数 类型 必填 作用
onSuccess function 调用成功回调
onThumbnailClick function 点击左下角缩略图回调
onWatermarkSettingClick function 点击水印设置回调
onSyncClick function 同步按钮点击回调
onSyncFailClick function 同步失败文本点击回调
onTakePicture(path) function 拍照成功回调
onError(err) function 失败回调
onClose() function 关闭回调

调用示例

// 注意:`iOS端`水印相机功能,支持的最低系统版本为`iOS10.0`

MImagePicker.openWatermarkCamera(
  {
    "ratio": 1.7777777,
    "thumbnail": "",
    "storeDirectory": cordova.file.externalDataDirectory + "watermark",
    "syncButtonIconPath": cordova.file.externalDataDirectory + "sync_button_icon.png",
    "watermarkConfig": {
      "size": 0.75,
      "opacity": 0.5,
      "title": "水印测试项目",// 该title的value值无长度限制,长度超过水印范围会以省略号结尾
      "items": [
        [
          {
            "title": "编号",// 该title的value值限制长度小于等于4 超出会出现省略号 下同
            "content": "123-456-789"
          },
          {
            "title": "计数",
            "content": "8"
          }
        ],
        [
          {
            "title": "施工区域",
            "content": "广东省深圳市南山区高新南一道2号"
          },
          {
            "title": "施工内容",
            "content": "外墙清洗"
          },
          {
            "title": "责任人",
            "content": "张三"
          }
        ]
      ]
    }
  }, 
  {
    onSuccess: function () {
      console.log('onSuccess');
    },
    onThumbnailClick: function () {
      console.log('onThumbnailClick');
    },
    onWatermarkSettingClick: function () {
      console.log('onWatermarkSettingClick');
    },
    onSyncClick: function () {
      console.log('onSyncClick');
    },
    onSyncFailClick: function () {
      console.log('onSyncFailClick');
    },
    onTakePicture: function (path) {
      console.log('onTakePicture', path);
    },
    onError: function (error) {
      console.log('onError', error);
    },
    onClose: function () {
      console.log('close') ;
    }
  }
);

关闭水印相机

MImagePicker.closeWatermarkCamera();

更新水印相机同步状态

MImagePicker.updateWatermarkCameraSyncState(options);
参数 类型 必填 作用
options json 更新水印相机同步状态参数,详情请参照下方options结构说明

options结构说明

字段名称 类型 说明
syncButtonIconPath string 水印相机右下角同步按钮图标路径,不传或传错时为默认图标
syncStateText string 同步状态文本内容
syncFailText string 同步失败文本内容

调用示例

MImagePicker.updateWatermarkCameraSyncState(
  {
    // 若2个字段都传空值,底部提示条会被隐藏
    syncButtonIconPath: cordova.file.externalDataDirectory + "sync_button_icon.png",
    syncStateText: "正在同步中...",
    syncFailText: "同步失败>>"
  }
);

更新水印相机配置

MImagePicker.updateWatermarkCameraConfig(config);
参数 类型 必填 作用
config json 更新水印相机同步状态参数,详情请参照watermarkConfig结构说明

调用示例

MImagePicker.updateWatermarkCameraConfig({
  "size": 0.8,
  "opacity": 0.8,
  "title": "水印测试项目(更新)",// 该title的value值无长度限制,长度超过水印范围会以省略号结尾
  "items": [
    [{
        "title": "编号(更新)","编号",// 该title的value值限制长度小于等于4 超出会出现省略号 下同
        "content": "123-456-789(更新)"
      },
      {
        "title": "计数(更新)",
        "content": "8(更新)"
      }
    ],
    [{
        "title": "施工区域(更新)",
        "content": "广东省深圳市南山区高新南一道2号(更新)"
      },
      {
        "title": "施工内容(更新)",
        "content": "外墙清洗(更新)"
      },
      {
        "title": "责任人(更新)",
        "content": "张三(更新)"
      }
    ]
  ]
});

UI效果图

图片水印UI

Alt text

自定义相机效果

  1. 拍照页面

Alt text 2. 预览页面

Alt text

iOS自定义相册UI

Alt textAlt textAlt text

Android自定义相册UI

Alt textAlt text

图片、视频处理前后数据对比图

Alt text

图片编辑效果图

  1. 添加图形界面

Alt text 图形编辑说明:

  1. 点击图形可以进入编辑模式
  2. 选中模式下,按住线框滑动可以移动图形
  3. 选中模式下,按住图形上的点滑动可以修改图形大小

Alt textAlt textAlt text 2. 涂鸦

Alt text 3. 添加文字

Alt textAlt text 4. 图片裁剪

图片裁剪说明:

  1. 点击底部裁剪按钮进入裁剪模式
  2. 通过双指触摸进行图片缩放
  3. 单指移动图片调整裁剪区域

Alt text 5. 马赛克

Alt text