buguge - Keep it simple,stupid

知识就是力量,但更重要的,是运用知识的能力why buguge?

导航

vue利用canvas将图片上传到服务器

 

前端.vue页面:

    <div class="panel">
      <div class="panel_hd">身份证</div>
      <div class="panel_bd flex">
        <div class="id-card">
          <div>身份证正面</div>
          <div class="submit-img">
            <input type="file" accept="image/*" @change="chooseImg($event,'fileFront')"/>
            <img :src="fileFront" alt="">
          </div>
        </div>
        <div class="id-card">
          <div>身份证反面</div>
          <div class="submit-img">
            <input type="file" accept="image/*" @change="chooseImg($event,'fileBack')"/>
            <img :src="fileBack" alt="">
          </div>
        </div>
      </div>
    </div>

 

js:

canvas绘图后用 canvas.toDataURL()可以得到图片的base64 dataURI——格式:data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/7QjCUGhvdG9zaG9wIDMuMAA/9k=

<script type="text/ecmascript-6">
    import ScrollLock from '../mixins/scrollLock'
    import {mapState, mapMutations} from 'vuex'

    export default {
        name: "Promotion",
        mixins: [ScrollLock],
        data() {
            return {
            }
        },
        computed: {
        },
        methods: {
            chooseImg(e, type) {
                let file = e.target.files[0];
                if (!file) {
                    return;
                }
                this.transformToBase64(file, type);
            },
            transformToBase64(file, type) {
                if (!window.FileReader) {
                    console.log('浏览器对FileReader方法不兼容');
                } else {
                    let reader = new FileReader();
                    reader.readAsDataURL(file);//读出 base64
                    reader.onloadend = () => {
                        this.imgCompress(reader, type);
                    };
                }
            },
            imgCompress(reader, type) {//图片超过尺寸压缩
                let img = new Image();
                img.src = reader.result;
                img.onload = () => {
                    let w = this.naturalWidth, h = this.naturalHeight, resizeW = 0, resizeH = 0;
                    let maxSize = {
                        width: 1000,
                        height: 1000,
                        level: 0.2
                    };
                    if (w > maxSize.width || h > maxSize.height) {
                        let multiple = Math.max(w / maxSize.width, h / maxSize.height);
                        resizeW = w / multiple;
                        resizeH = h / multiple;
                        let canvas = document.createElement('canvas'),
                            ctx = canvas.getContext('2d');
                        canvas.width = resizeW;
                        canvas.height = resizeH;
                        ctx.drawImage(img, 0, 0, resizeW, resizeH);
                        let base64 = canvas.toDataURL('image/jpeg', maxSize.level);
                        this.uploadImg(base64, type);
                    } else {
                        // 如果图片尺寸小于最大限制,则不压缩直接上传
                        this.uploadImg(reader.result, type);
                    }
                }
            },
            uploadImg(base64, type) {
                // this[type] = base64;
                this.$axios.post('driver/uploadImage', {
                    picString: base64,
                    picType: type
                }).then(response => {
                    this[type] = base64;
                }, () => {
                    this.$error('图片上传失败');
                });
            },
        },
        created() {
        },
        mounted() {
        }
    }
</script>

 

服务端api:

程序将接收到base64串,通过fastdfs的api上传到文件服务器。fastdfs工具点击这里

@Slf4j
@RequestMapping("/api/driver")
@RestController
public class DriverInfoController {

    @Autowired
    private FastdfsClientUtil fastdfsClientUtil;

    @PostMapping("/uploadImage")
    @ResponseBody
    public Result uploadImage(@RequestBody JSONObject requestBody){
        String picString = requestBody.getString("picString");
        
        String imgPath = fastdfsClientUtil.uploadImageAndCrtThumbImageByBase64(picString);
        log.info("[上传返回的地址:{}]",imgPath);
        return Result.ok();
    }
}

 

posted on 2020-07-30 21:20  buguge  阅读(1511)  评论(0编辑  收藏  举报