ImageReader 图片选择器

Scan me!

用于相册照片读取或拉起拍照

引入

  1. import { ImageReader } from 'mand-mobile'
  2. import imageProcessor from 'mand-mobile/lib/image-reader/image-processor' // 图片处理插件,用法参考#imageProcessor
  3. Vue.component(ImageReader.name, ImageReader)

代码演示

图片选择

  1. <template>
  2. <div class="md-example-child md-example-child-reader md-example-child-reader-0">
  3. <ul class="image-reader-list">
  4. <li
  5. class="image-reader-item"
  6. v-for="(img, index) in imageList['reader0']"
  7. :key="index"
  8. :style="{
  9. 'backgroundImage': `url(${img})`,
  10. 'backgroundPosition': 'center center',
  11. 'backgroundRepeat': 'no-repeat',
  12. 'backgroundSize': 'cover'
  13. }">
  14. <md-icon
  15. class="image-reader-item-del"
  16. name="circle-cross"
  17. color="#666"
  18. @click.native="onDeleteImage('reader0', index)">
  19. </md-icon>
  20. </li>
  21. <li class="image-reader-item add">
  22. <md-image-reader
  23. name="reader0"
  24. @select="onReaderSelect"
  25. @complete="onReaderComplete"
  26. @error="onReaderError"
  27. is-multiple
  28. ></md-image-reader>
  29. <md-icon name="hollow-plus" size="md" color="#CCC"></md-icon>
  30. <p>上传照片</p>
  31. </li>
  32. </ul>
  33. </div>
  34. </template>
  35. <script>
  36. import {Icon, ImageReader, Toast} from 'mand-mobile'
  37. export default {
  38. name: 'image-reader-demo',
  39. components: {
  40. [Icon.name]: Icon,
  41. [ImageReader.name]: ImageReader,
  42. },
  43. data() {
  44. return {
  45. imageList: {
  46. reader0: [
  47. '//img-hxy021.didistatic.com/static/strategymis/insurancePlatform_spu/uploads/27fb7f097ca218d743f816836bc7ea4a',
  48. '//manhattan.didistatic.com/static/manhattan/insurancePlatform_spu/uploads/c2912793a222eb24b606a582fd849ab7',
  49. ],
  50. reader1: [],
  51. },
  52. }
  53. },
  54. methods: {
  55. onReaderSelect(name, {files}) {
  56. files.forEach(file => {
  57. console.log('[Mand Mobile] ImageReader Selected:', 'File Name ' + file.name)
  58. })
  59. Toast.loading('图片读取中...')
  60. },
  61. onReaderComplete(name, {dataUrl, file}) {
  62. const demoImageList = this.imageList[name] || []
  63. console.log('[Mand Mobile] ImageReader Complete:', 'File Name ' + file.name)
  64. demoImageList.push(dataUrl)
  65. this.$set(this.imageList, name, demoImageList)
  66. Toast.hide()
  67. },
  68. onReaderError(name, {msg}) {
  69. Toast.failed(msg)
  70. },
  71. onDeleteImage(name, index) {
  72. const demoImageList = this.imageList[name] || []
  73. demoImageList.splice(index, 1)
  74. this.$set(this.imageList, name, demoImageList)
  75. },
  76. },
  77. }
  78. </script>
  79. <style lang="stylus" scoped>
  80. .md-example-child-reader
  81. .image-reader-list
  82. float left
  83. width 100%
  84. .image-reader-item
  85. position relative
  86. float left
  87. width 23.5%
  88. padding-bottom 23.5%
  89. margin-bottom 2%
  90. margin-right 2%
  91. background color-bg-base
  92. box-sizing border-box
  93. list-style none
  94. border-radius radius-normal
  95. hairline(all, color-border-base)
  96. background-size cover
  97. &:nth-of-type(4n)
  98. margin-right 0
  99. &.add
  100. .md-icon
  101. position absolute
  102. top 40%
  103. left 50%
  104. transform translate(-50%, -50%)
  105. opacity .5
  106. p
  107. position absolute
  108. top 50%
  109. left 0
  110. width 100%
  111. margin-top 15px
  112. font-size font-minor-normal
  113. color color-text-disabled
  114. text-align center
  115. .image-reader-item-del
  116. position absolute
  117. top 5px
  118. right 5px
  119. z-index 3
  120. background #EEE
  121. border-radius radius-circle
  122. </style>

图片选择并轴向修正,压缩处理

width: 200 height: 200 quality: 0.1
  1. <template>
  2. <div class="md-example-child md-example-child-reader md-example-child-reader-1">
  3. <ul class="image-reader-list">
  4. <li
  5. class="image-reader-item"
  6. v-for="(img, index) in imageList['reader1']"
  7. :key="index"
  8. :style="{
  9. 'backgroundImage': `url(${img})`,
  10. 'backgroundPosition': 'center center',
  11. 'backgroundRepeat': 'no-repeat',
  12. 'backgroundSize': 'cover'
  13. }">
  14. <md-icon
  15. class="image-reader-item-del"
  16. name="circle-cross"
  17. color="#666"
  18. @click.native="onDeleteImage('reader1', index)">
  19. </md-icon>
  20. </li>
  21. <li class="image-reader-item add">
  22. <md-image-reader
  23. name="reader1"
  24. @select="onReaderSelect"
  25. @complete="onReaderComplete"
  26. @error="onReaderError"
  27. is-multiple
  28. ></md-image-reader>
  29. <md-icon name="hollow-plus" size="md" color="#CCC"></md-icon>
  30. <p>上传照片</p>
  31. </li>
  32. </ul>
  33. </div>
  34. </template>
  35. <script>
  36. import {Icon, ImageReader, Toast} from 'mand-mobile'
  37. import imageProcessor from 'mand-mobile/components/image-reader/image-processor'
  38. export default {
  39. name: 'image-reader-demo',
  40. components: {
  41. [Icon.name]: Icon,
  42. [ImageReader.name]: ImageReader,
  43. },
  44. data() {
  45. return {
  46. imageList: {
  47. reader0: [
  48. '//img-hxy021.didistatic.com/static/strategymis/insurancePlatform_spu/uploads/27fb7f097ca218d743f816836bc7ea4a',
  49. '//manhattan.didistatic.com/static/manhattan/insurancePlatform_spu/uploads/c2912793a222eb24b606a582fd849ab7',
  50. ],
  51. reader1: [],
  52. },
  53. }
  54. },
  55. methods: {
  56. onReaderSelect() {
  57. Toast.loading('图片读取中...')
  58. },
  59. onReaderComplete(name, {dataUrl}) {
  60. const demoImageList = this.imageList[name] || []
  61. imageProcessor({
  62. dataUrl,
  63. width: 200,
  64. height: 200,
  65. quality: 0.1,
  66. }).then(({dataUrl}) => {
  67. dataUrl && demoImageList.push(dataUrl)
  68. })
  69. this.$set(this.imageList, name, demoImageList)
  70. Toast.hide()
  71. },
  72. onReaderError(name, {msg}) {
  73. Toast.failed(msg)
  74. },
  75. onDeleteImage(name, index) {
  76. const demoImageList = this.imageList[name] || []
  77. demoImageList.splice(index, 1)
  78. this.$set(this.imageList, name, demoImageList)
  79. },
  80. },
  81. }
  82. </script>
  83. <style lang="stylus" scoped>
  84. .md-example-child-reader
  85. .image-reader-list
  86. float left
  87. width 100%
  88. .image-reader-item
  89. position relative
  90. float left
  91. width 23.5%
  92. padding-bottom 23.5%
  93. margin-bottom 2%
  94. margin-right 2%
  95. background color-bg-base
  96. box-sizing border-box
  97. list-style none
  98. border-radius radius-normal
  99. hairline(all, color-border-base)
  100. background-size cover
  101. &:nth-of-type(4n)
  102. margin-right 0
  103. &.add
  104. .md-icon
  105. position absolute
  106. top 40%
  107. left 50%
  108. transform translate(-50%, -50%)
  109. opacity .5
  110. p
  111. position absolute
  112. top 50%
  113. left 0
  114. width 100%
  115. margin-top 15px
  116. font-size font-minor-normal
  117. color color-text-disabled
  118. text-align center
  119. .image-reader-item-del
  120. position absolute
  121. top 5px
  122. right 5px
  123. z-index 3
  124. background #EEE
  125. border-radius radius-circle
  126. </style>

API

ImageReader Props

属性说明类型默认值备注
name标识String-可用于区分多个选择器
size图片尺寸限制String/Number-单位kb
mime支持图片类型Array*['jpeg','png']
is-camera-only是否只支持拍照Booleanfalse-
is-multiple是否支持选择多张Booleanfalse-
amount选择多张Number-只在is-multipletrue时有效

ImageReader Events

@select(name, { files })

图片选择完成事件,还未开始读取

属性说明类型备注
name选择器标识String-
files1.3.1+图片对象集合Array-
@complete(name, { dataUrl, blob, file })

图片选择读取完成事件

属性说明类型备注
name选择器标识String-
dataUrl图片Base64String-
blob图片Blob对象,可用于formDataBlob-
file1.3.1+图片对象File-
@error(name, { code, msg })

图片选择读取失败事件

属性说明类型备注
name选择器标识String-
code错误标识,见附录String-
msg错误信息,见附录String-

imageProcessor

用于图片轴向修正,图片质量压缩,宽高控制

引入

  1. import imageProcessor from 'mand-mobile/lib/image-reader/image-processor'
  2. /**
  3. * options 图片处理配置
  4. * fn(dataUrl, blob) 处理完成回调
  5. * @return Promise({dataUrl, blob})
  6. */
  7. imageProcessor(options[, fn])

options

属性说明类型备注
dataUrl图片Base64String-
width图片宽度Number单位px, 宽度超出时等比缩放
height图片高度Number单位px, 高度超出时等比缩放
quality图片质量Number取值范围0-1

附录

图片读取失败错误码和错误信息

  1. '100': 'browser does not support'
  2. '101': 'picture size is beyond the preset'
  3. '102': 'picture read failure'
  4. '103': 'the number of pictures exceeds the limit'