图片选取器:包含拍照,录制视频,相册选图,图片和视频处理、获取图片和视频信息、图片涂鸦、文本、剪裁,水印相机等功能。
error
回调中,errCode
通用释义errorCode | 说明 |
---|---|
-1 | 普通错误,错误具体内容参考errMsg
|
999 | 取消操作 |
1001 | 调用插件接口传参错误, (参数不合法,参数不匹配,参数校验失败等) |
1100 | 用户拒绝权限 |
1101 | 用户取消权限,仅支持Android
|
iOS 注意事项
cordova.file.externalDataDirectory
路径仅适用于Android,更多关于Android与iOS本地存储目录请参考这里
Android 注意事项
图片处理
和视频处理
中saveToAlbum
如果为true
,那么图片和视频path
必须填写非应用路径,如填写cordova.file.externalRootDirectory + '目录\文件名'
,才能将资源更新到相册。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 | 用来区分拍照 和录制视频 ,分别对应传参photo 和video ,不传或传其他值时默认为photo
|
photoConfig | json | 当sourceType 为photo 时的可选参数,photoConfig释义如下 |
videoConfig | json | 当sourceType 为video 时的可选参数,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参数 |
1.3.4版本开始生效
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规则说明
图片
文件拓展名传jpg
,选视频
文件拓展名传mp4
。传递非jpg
,mp4
的文件拓展名(包含没有拓展名),将会直接进入错误回调jpg
、mp4
的文件选择是会提示不支持的文件类型X
为路径中传递的图片/视频数量调用示例
// 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 | 图片水印,详细配置如下 |
注意:width
和height
如果小于待压缩图片尺寸,那么图片宽高不做处理
字段名称 | 类型 | 说明 |
---|---|---|
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": "张三(更新)"
}
]
]
});
2. 预览页面
图形编辑说明:
2. 涂鸦
3. 添加文字
4. 图片裁剪
图片裁剪说明:
5. 马赛克