微信小程序拍照页面自定义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>
相关推荐
不吃辣49012 小时前
vibe coding | 如何做一个AI制图小程序?
人工智能·小程序·ai编程
laboratory agent开发12 小时前
2026小程序服务商推荐:全域私域数字化工具选型深度解析
小程序
小码哥06819 小时前
智慧物业管理系统开发详解:核心功能与源码开发优势
小程序·物业管理·物业系统
汤姆yu19 小时前
基于微信小程序的方言转文字小程序的设计与实现
java·小程序·毕业设计·方言翻译
不吃辣49019 小时前
vibe coding | 如何做一个 AI 闹钟小程序?
人工智能·小程序·ai编程
万岳科技程序员小金20 小时前
在线教育系统源码开发全解析:教育培训APP、小程序及平台搭建完整方案
小程序·在线教育系统源码·教育app开发·教育小程序开发
2501_915921431 天前
appuploader-cli 命令行上传 IPA 到 App Store Connect upload CI 集成
android·ci/cd·小程序·https·uni-app·iphone·webview
梦想的旅途22 天前
企业微信销售自动化:线索分配与超时预警
小程序·自动化·企业微信
单词记忆方法研究2 天前
探索上海背单词小程序品牌:高效学习工具的优化之路
大数据·学习·小程序
码上成长2 天前
小程序请求层怎么封装?
前端·小程序