• 引入
  • 代码演示
    • 图片选择
    • 图片选择并轴向修正,压缩处理
      • width: 200 height: 200 quality: 0.1
  • API
    • ImageReader Props
    • ImageReader Events
      • @select(name, { files })
      • @complete(name, { dataUrl, blob, file })
      • @error(name, { code, msg })
  • imageProcessor
    • 引入
    • options
  • 附录

    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)

    代码演示

    图片选择

    ImageReader 图片选择器 - 图2

    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-tag
    15. class="image-reader-item-del"
    16. size="small"
    17. shape="quarter"
    18. fill-color="#111A34"
    19. type="fill"
    20. font-color="#fff"
    21. @click.native="onDeleteImage('reader0', index)"
    22. >
    23. <md-icon name="close"></md-icon>
    24. </md-tag>
    25. </li>
    26. <li class="image-reader-item add">
    27. <md-image-reader
    28. name="reader0"
    29. @select="onReaderSelect"
    30. @complete="onReaderComplete"
    31. @error="onReaderError"
    32. is-multiple
    33. ></md-image-reader>
    34. <md-icon name="camera" size="md" color="#CCC"></md-icon>
    35. <p>添加图片</p>
    36. </li>
    37. </ul>
    38. </div>
    39. </template>
    40. <script>
    41. import {Icon, ImageReader, Tag, Toast} from 'mand-mobile'
    42. export default {
    43. name: 'image-reader-demo',
    44. components: {
    45. [Icon.name]: Icon,
    46. [ImageReader.name]: ImageReader,
    47. [Tag.name]: Tag,
    48. },
    49. data() {
    50. return {
    51. imageList: {
    52. reader0: [
    53. '//img-hxy021.didistatic.com/static/strategymis/insurancePlatform_spu/uploads/27fb7f097ca218d743f816836bc7ea4a',
    54. '//manhattan.didistatic.com/static/manhattan/insurancePlatform_spu/uploads/c2912793a222eb24b606a582fd849ab7',
    55. ],
    56. reader1: [],
    57. },
    58. }
    59. },
    60. methods: {
    61. onReaderSelect(name, {files}) {
    62. files.forEach(file => {
    63. console.log('[Mand Mobile] ImageReader Selected:', 'File Name ' + file.name)
    64. })
    65. Toast.loading('图片读取中...')
    66. },
    67. onReaderComplete(name, {dataUrl, file}) {
    68. Toast.hide()
    69. console.log('[Mand Mobile] ImageReader Complete:', 'File Name ' + file.name)
    70. setTimeout(() => {
    71. const demoImageList = this.imageList[name] || []
    72. demoImageList.push(dataUrl)
    73. this.$set(this.imageList, name, demoImageList)
    74. }, 100)
    75. },
    76. onReaderError(name, {msg}) {
    77. Toast.failed(msg)
    78. },
    79. onDeleteImage(name, index) {
    80. const demoImageList = this.imageList[name] || []
    81. demoImageList.splice(index, 1)
    82. this.$set(this.imageList, name, demoImageList)
    83. },
    84. },
    85. }
    86. </script>
    87. <style lang="stylus" scoped>
    88. .md-example-child-reader
    89. .image-reader-list
    90. float left
    91. width 100%
    92. .image-reader-item
    93. position relative
    94. float left
    95. width 23.5%
    96. padding-bottom 23.5%
    97. margin-bottom 2%
    98. margin-right 2%
    99. background #FFF
    100. box-shadow 0 5px 20px rgba(197, 202, 213, .25)
    101. box-sizing border-box
    102. list-style none
    103. border-radius 4px
    104. background-size cover
    105. overflow hidden
    106. &:nth-of-type(4n)
    107. margin-right 0
    108. &.add
    109. .md-icon
    110. position absolute
    111. top 40%
    112. left 50%
    113. transform translate(-50%, -50%)
    114. opacity .5
    115. p
    116. position absolute
    117. top 50%
    118. left 0
    119. width 100%
    120. margin-top 15px
    121. font-size 22px
    122. color #CCC
    123. text-align center
    124. .image-reader-item-del
    125. position absolute
    126. top 0
    127. right 0
    128. z-index 3
    129. opacity .8
    130. .md-icon-close
    131. font-size 24px
    132. </style>

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

    width: 200 height: 200 quality: 0.1

    ImageReader 图片选择器 - 图3

            <template>
      <div class="md-example-child md-example-child-reader md-example-child-reader-1">
        <ul class="image-reader-list">
          <li
            class="image-reader-item"
            v-for="(img, index) in imageList['reader1']"
            :key="index"
            :style="{
              'backgroundImage': `url(${img})`,
              'backgroundPosition': 'center center',
              'backgroundRepeat': 'no-repeat',
              'backgroundSize': 'cover'
            }">
            <md-tag
              class="image-reader-item-del"
              size="small"
              shape="quarter"
              fill-color="#111A34"
              type="fill"
              font-color="#fff"
              @click.native="onDeleteImage('reader1', index)"
            >
              <md-icon name="close"></md-icon>
            </md-tag>
          </li>
          <li class="image-reader-item add">
            <md-image-reader
              name="reader1"
              @select="onReaderSelect"
              @complete="onReaderComplete"
              @error="onReaderError"
              is-multiple
            ></md-image-reader>
            <md-icon name="camera" size="md" color="#CCC"></md-icon>
            <p>添加图片</p>
          </li>
        </ul>
      </div>
    </template>
    
    <script>
    import {Icon, ImageReader, Tag, Toast} from 'mand-mobile'
    import imageProcessor from 'mand-mobile/components/image-reader/image-processor'
    
    export default {
      name: 'image-reader-demo',
      components: {
        [Icon.name]: Icon,
        [ImageReader.name]: ImageReader,
        [Tag.name]: Tag,
      },
      data() {
        return {
          imageList: {
            reader0: [
              '//img-hxy021.didistatic.com/static/strategymis/insurancePlatform_spu/uploads/27fb7f097ca218d743f816836bc7ea4a',
              '//manhattan.didistatic.com/static/manhattan/insurancePlatform_spu/uploads/c2912793a222eb24b606a582fd849ab7',
            ],
            reader1: [],
          },
        }
      },
      methods: {
        onReaderSelect() {
          Toast.loading('图片读取中...')
        },
        onReaderComplete(name, {dataUrl}) {
          const demoImageList = this.imageList[name] || []
    
          imageProcessor({
            dataUrl,
            width: 200,
            height: 200,
            quality: 0.1,
          }).then(({dataUrl}) => {
            dataUrl && demoImageList.push(dataUrl)
          })
          this.$set(this.imageList, name, demoImageList)
    
          Toast.hide()
        },
        onReaderError(name, {msg}) {
          Toast.failed(msg)
        },
        onDeleteImage(name, index) {
          const demoImageList = this.imageList[name] || []
          demoImageList.splice(index, 1)
          this.$set(this.imageList, name, demoImageList)
        },
      },
    }
    
    </script>
    
    <style lang="stylus" scoped>
    .md-example-child-reader
      .image-reader-list
        float left
        width 100%
        .image-reader-item
          position relative
          float left
          width 23.5%
          padding-bottom 23.5%
          margin-bottom 2%
          margin-right 2%
          background #FFF
          box-shadow 0 5px 20px rgba(197, 202, 213, .25)
          box-sizing border-box
          list-style none
          border-radius 4px
          background-size cover
          &:nth-of-type(4n)
            margin-right 0
          &.add
            .md-icon
              position absolute
              top 40%
              left 50%
              transform translate(-50%, -50%)
              opacity .5
            p
              position absolute
              top 50%
              left 0
              width 100%
              margin-top 15px
              font-size 22px
              color #CCC
              text-align center
          .image-reader-item-del
            position absolute
            top 0
            right 0
            z-index 3
            opacity .8
            .md-icon-close
              font-size 24px
    </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-
    files图片对象集合Array-
    @complete(name, { dataUrl, blob, file })

    图片选择读取完成事件

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

    图片选择读取失败事件

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

    imageProcessor

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

    引入

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

    options

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

    附录

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

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