微信小程序拍照页面自定义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>
相关推荐
hyswl6667 小时前
郑州小程序开发公司推荐:详解火焰山科技
科技·小程序
2501_916008898 小时前
iOS 性能测试的深度实战方法 构建从底层指标到真实场景回放的多工具测试体系
android·ios·小程序·https·uni-app·iphone·webview
说私域8 小时前
基于开源AI智能客服、AI智能名片与S2B2C商城小程序的新社群用户进化策略研究
人工智能·小程序
项目題供诗9 小时前
微信小程序黑马优购(项目)(二)
微信小程序·小程序
小小王app小程序开发10 小时前
陪诊陪护小程序一番赏衍生玩法分析:场景融合与用户激励的双重创新
小程序
阿里花盘14 小时前
线上门店经营方案:连锁美容院线上小程序店铺怎么做?
小程序
计算机徐师兄15 小时前
Java基于SpringBoot的足球队管理平台小程序【附源码、文档说明】
微信小程序·小程序·足球队管理平台小程序·java足球队管理平台小程序·足球队管理微信小程序·足球队管理平台·jav足球队管理小程序
千寻技术帮17 小时前
50040_基于微信小程序的项目管理系统
小程序·源码·讲解·文档·ppt
笨笨狗吞噬者20 小时前
【uniapp】小程序实现自由控制组件JSON文件配置
vue.js·微信小程序·vite
2501_9159090620 小时前
iOS APP 抓包全流程解析,HTTPS 调试、网络协议分析与多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview