微信小程序拍照页面自定义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>
相关推荐
tuanyuan99o8 小时前
2026商城小程序的安全怎么保障?防止黑客攻击和数据泄露
安全·小程序
facetarzan13 小时前
微信小程序文件下载
小程序·文件预览·文件下载
aiguangyuan13 小时前
微信小程序服务商
微信小程序·前端开发
一支帆13 小时前
微信小程序-情侣点餐
java·微信小程序·情侣点餐
Slow菜鸟13 小时前
AI开发-微信小程序(全流程提示词)
人工智能·微信小程序
橘子海全栈攻城狮13 小时前
【最新源码】鸟博士微信小程序 023
spring boot·后端·web安全·微信小程序·小程序
Yuujs13 小时前
微信小程序反编译保姆级教程
微信小程序·小程序
m0_6470579613 小时前
微信小程序同声传译(WechatSI)通用接入教程
微信小程序·小程序·notepad++
kkk哥13 小时前
weixin121作品集展示微信小程序
java·mysql·微信小程序·ssm·b/s架构·作品集展示微信小程序
QQ_51100828513 小时前
uniapp微信小程序网上饰品商城售卖系统php python物流
微信小程序·uni-app·php