微信小程序拍照页面自定义demo

api文档

cpp 复制代码
<template>
  <div>
    <image
      mode="widthFix"
      style="width: 100%; height: 300px"
      :src="imageSrc"
      v-if="imageSrc"
    ></image>
    <camera
      v-else
      :device-position="devicePosition"
      :flash="flash"
      @error="cameraError"
      style="width: 100%; height: 300px"
    ></camera>
    <div class="buttons flex justify-between" v-if="!imageSrc">
      <div class="button" @click="openFlash">
        {{ flash === "off" ? "闪光关闭" : "闪光打开" }}
      </div>
      <div class="button cameral" @click="takePhoto"></div>
      <div class="button" @click="frontAndAfter">
        {{ devicePosition === "back" ? "后置" : "前置" }}
      </div>
    </div>
    <div class="buttons flex justify-between" v-else>
      <div class="resetPhoto" @click="resetPhoto">重新拍照</div>
      <div class="confirmPhoto" @click="confirmPhoto">确认</div>
    </div>
  </div>
</template>
<script>
import meServiceApi from "@/service/me.js";

export default {
  data() {
    return {
      imageSrc: "",
      flash: "off",
      devicePosition: "back",
    };
  },
  methods: {
    takePhoto() {
      const ctx = wx.createCameraContext();
      const that = this;
      ctx.takePhoto({
        quality: "high",
        success: (res) => {
          that.imageSrc = res.tempImagePath;
          console.log("拍照的路径", res.tempImagePath);
        },
      });
    },
    cameraError(e) {
      console.log(e.detail);
    },
    openFlash() {
      switch (this.flash) {
        case "off":
          this.flash = "on";
          break;

        default:
          this.flash = "off";
          break;
      }
    },
    frontAndAfter() {
      switch (this.devicePosition) {
        case "back":
          this.devicePosition = "front";
          break;

        default:
          this.devicePosition = "back";
          break;
      }
    },
    async confirmPhoto() {
      // 上传到服务器
      console.log(this.imageSrc, "this.imageSrc");
      const res = await meServiceApi.upFileImg(this.imageSrc);
      console.log("上传的图片", res);
    },
    resetPhoto() {
      this.imageSrc = "";
    },
  },
  mounted() {},
};
</script>
<style scoped lang='scss'>
.buttons {
  position: fixed;
  bottom: 20rpx;
  left: 0;
  width: 100%;
  height: 144rpx;
  padding: 20rpx 0;
  color: #fff;
  background-color: #000;
  .cameral {
    position: absolute;
    top: 10rpx;
    left: 40%;
    width: 144rpx;
    height: 140rpx;
    border-radius: 50%;
    border: 3px solid #f5222d;
  }
}
</style>
相关推荐
WKK_2 小时前
uniapp自定义封装tabbar
前端·javascript·小程序·uni-app
老李不敲代码1 天前
榕壹云预约咨询系统:基于ThinkPHP+MySQL+UniApp打造的灵活预约小程序解决方案
mysql·微信小程序·小程序·uni-app·php
fakaifa1 天前
【最新版】西陆健身系统源码全开源+uniapp前端
前端·小程序·uni-app·开源·php·约课小程序·健身小程序
二J1 天前
管理100个小程序-很难吗
android·小程序
qq_357389631 天前
陪诊陪检系统源码,陪诊小程序,陪诊APP,陪诊服务,家政上门系统,居家护理陪护源码,医护小程序
小程序
橘猫云计算机设计2 天前
springboot-基于Web企业短信息发送系统(源码+lw+部署文档+讲解),源码可白嫖!
java·前端·数据库·spring boot·后端·小程序·毕业设计
跨时代科技2 天前
洗车小程序系统前端uniapp 后台thinkphp
小程序
依辰2 天前
小程序SAAS产品定制化需求解决方案
前端·javascript·微信小程序
fakaifa2 天前
【最新版】沃德代驾源码全开源+前端uniapp
前端·小程序·uni-app·开源·php·沃德代驾·代驾小程序
18538162800余。2 天前
NFC 碰一碰实现视频源码,网页与小程序协同
小程序