H5使用Input拍照

H5 input 拍照

页面开始显示一个拍照图片,点击拍照之后,返回图片并替换拍照图片

一、页面

页面设计,点击两个图片,进行拍照。拍照之后使用拍照后的图片替换原图

二、代码

1.HTML

<template>
  <div id="identityCheck" style="background-color: #f7f7f7;font-size: 12px;">
      <div style="width: 100%;text-align: center;height: 113px;margin-top: 40px">
        <div ref="face" class="identityImage face" @click="takePhotoFace"></div>
        <div style="width: 20px;display: inline-block;"></div>
        <div ref="back" class="identityImage back" @click="takePhotoBack"></div>
      </div>
    </div>
    <input style="display: none;" ref="faceInput" type="file" accept="image/*" capture="camera" @change="backImag" />
    <input style="display: none;" ref="backInput" type="file" accept="image/*" capture="camera" @change="backImag2" />
  </div>
</template>

2.js

<script>
import { Vue, Component } from "vue-property-decorator";
import { NativeUI } from "../../utils/NativeUI";
@Component({
  name: "IndentityCheck"
})
export default class IndentityCheck extends Vue {

  //拍身份证证明照
  takePhotoFace() {
    this.$refs.faceInput.click();
  }
  //拍身份证背面照
  takePhotoBack() {
    this.$refs.backInput.click();
  }

  backImag(e) {
    const file = e.target.files[0];
    console.log(file.size / 1024);
    const fileName = this.getObjectURL(file);
    this.$refs.face.style.backgroundImage = `url(${fileName})`;
    NativeUI.showWaiting("正在上传证件照图片,请稍后. . .");
    const that = this;
    this.fileToBase64(file)
      .then(function(result) {
        //打印base64的值
        console.log(result);
        NativeUI.closeWaiting();
      })
      .catch(function(error) {
        console.log(error);
        NativeUI.closeWaiting();
      });
  }
  backImag2(e) {
    const file = e.target.files[0];
    console.log(file.size / 1024);
    const fileName = this.getObjectURL(file);
    this.$refs.back.style.backgroundImage = `url(${fileName})`;
    NativeUI.showWaiting("正在上传证件照图片,请稍后. . .");
    const that = this;
    this.fileToBase64(file)
      .then(function(result) {
        console.log(result);
        NativeUI.closeWaiting();
      })
      .catch(function(error) {
        console.log(error);
        NativeUI.closeWaiting();
      });
  }
  // 文件转 base64
  fileToBase64(file) {
    return new Promise(function(resolve, reject) {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = function(ev) {
        resolve(ev.target.result);
      };
    });
  }
  //获取文件路径
  getObjectURL(file) {
    let url = null;
    if (window.createObjectURL != undefined) {
      // basic
      url = window.createObjectURL(file);
    } else if (window.URL != undefined) {
      // mozilla(firefox)
      url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) {
      // webkit or chrome
      url = window.webkitURL.createObjectURL(file);
    }
    return url;
  }

}

使用两个不展示的 input标签,当图片点击的时候,分别触发各自的input标签,使用input标签拍照。在 input 的 change事件里面拿到返回的文件,读取文件地址 并把文件转化为 base64 。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值