微信小程序自定义相机(React+Taro)

1、index.tsx

javascript 复制代码
import { useState } from 'react'
import { View, Image, Camera } from "@tarojs/components";
import { observer } from "mobx-react";
import { AtButton } from 'taro-ui'

import "./index.scss";

import { takePhote } from '@/common/bluetoolth/tool'
const Home = observer(() => {
  const [showImage,setShowImage] = useState<boolean>(false)
  const [devicePosition,setDevicePosition] = useState<any>('front')
  const [path,setPath] = useState<string>('')
  const [showBg,setShowBg] = useState<boolean>(true)
  const openMedia = async ()=>{
    console.log('检测-拍照')
    setShowImage(true)
  }
  const takePhoto = async ()=>{
    console.log('相机拍照时触发')
    const res = await takePhote()
    console.log('照片path',res.path)
    setPath(res.path)
    setShowBg(false)
  }
  const resetPhoto = ()=>{
    setPath('')
    setShowBg(true)
  }
  return (
    <View className="home-wrap">
      {showImage?
      <View  className='camera-box'>
        <Camera className='camera-photo' mode='normal' devicePosition={devicePosition}>
          <View className='camera-path-box'>
            {showBg?<View className='bg'></View>:<Image className='camera-path' src={path}></Image>}
          </View>
        </Camera>
        <View className='camera-button'>
          <View onClick={()=>{ setShowImage(false) }}>返回</View>
          {showBg?<View onClick={takePhoto}>拍照</View>:<View onClick={resetPhoto}>重拍</View>}
          <View onClick={()=>{ setDevicePosition(devicePosition=='front'?'back':'front') }}>切换前后置</View>
        </View>
      </View>
      :<AtButton type='primary' size='normal' onClick={openMedia}>调用相机</AtButton>
      }
    </View>
  );
});

export default Home;

2、scss

css 复制代码
.home-wrap {
  padding-top: 100px;
  .camera-box {
    width: 100%;
    height: calc(100vh - 100px);
    
    .camera-photo {
      height: calc(100% - 200px);

      .camera-path-box {
        width: 100%;
        height: 100%;
        .camera-path {
          width: 100%;
          height: 100%;
          
        }

        .bg {
          background: url($IMG_URL + "/home/home-top-bg.png") 0 0 no-repeat;
          background-size: contain;
          width: 100%;
          height: 100%;
        }
      }

    }

    .camera-button {
      height: 200px;
      display: flex;
      justify-content: space-between;
      background-color: #ffffff;
      align-items: center;
    }
  }
}

3、tool.js

javascript 复制代码
export function takePhote() {
  const ctx = wx.createCameraContext()
  const that = this
  return new Promise((resolve, reject) => {
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        wx.getImageInfo({
          src: res.tempImagePath,
          success:res=>{
            console.log(res)
            resolve(res);
          },
          fail(res) {
            reject(res);
          },
        })
      }
    })
  });
}
相关推荐
xiaoxue..11 分钟前
react:浅聊 vdom 与 diff 算法
前端·javascript·react.js·面试
nashane1 小时前
HarmonyOS 6学习:相机预览画面拉伸全解析——告别变形,打造完美相机预览体验
数码相机·harmonyos 5
Ruihong16 小时前
Vue v-bind 转 React:VuReact 怎么处理?
vue.js·react.js·面试
Ruihong18 小时前
Vue v-on 在 React 中 VuReact 会如何实现?
vue.js·react.js·面试
QQ227923910219 小时前
Java springboot基于微信小程序的智慧旅游导游系统景点门票酒店预订(源码+文档+运行视频+讲解视频)
java·spring boot·微信小程序·maven·vuejs
格林威20 小时前
AI视觉检测:Jetson Orin vs RTX A2000 推理速度对比
人工智能·数码相机·机器学习·计算机视觉·视觉检测·机器视觉·工业相机
M ? A21 小时前
你的 Vue v-memo 与 v-once,VuReact 会编译成什么样的 React 代码?
前端·javascript·vue.js·经验分享·react.js·面试·vureact
M ? A1 天前
你的 Vue v-for,VuReact 会编译成什么样的 React 代码?
前端·javascript·vue.js·经验分享·react.js·面试·vureact
qq_12084093712 天前
Three.js 大场景分块加载实战:从全量渲染到可视集调度
开发语言·javascript·数码相机
光影少年2 天前
前端工程化升级
前端·javascript·react.js·前端框架