微信小程序自定义相机(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);
          },
        })
      }
    })
  });
}
相关推荐
didiplus9 小时前
我在GitHub刷到一个诗词API,顺手写了款小程序
微信小程序
wuhanzhanhui11 小时前
从 3D 建模到高光谱2026 武汉机器视觉展会|工业相机展会洞见感知力量新高度
数码相机·3d
光影少年15 小时前
RN 样式特点:Flex 布局默认、没有像素单位、样式不能级联
react native·react.js·掘金·金石计划
3DVisionary15 小时前
单目高速DIC如何啃下精密件微米级高频振动这块“硬骨头“
数码相机·航空发动机微型叶片·mems器件·pcb与电子封装·精密钟表/齿轮·微纳加工件·全尺寸3d检测
_helen_52016 小时前
【第3篇】自定义飞机关键点标注数据获取
数码相机
中达瑞和-高光谱·多光谱17 小时前
跨越波段盲区,获取多维信息:全波段光谱相机能解决什么?
数码相机
小蚂蚁i18 小时前
React Hooks 原理深度解析:从会用到真正懂
前端·react.js
3DVisionary19 小时前
单目高速DIC、双目DIC与传统传感器,谁更适合精密件?
网络·数码相机·3d·性能优化·注塑件全尺寸检测·高频振动测量·悬臂梁面内振动
索西引擎19 小时前
【React】虚拟 DOM:原理、机制与核心价值分析
前端·react.js·前端框架
蜕变的土豆20 小时前
Dalsa网口线扫相机配置以及排查问题全流程
数码相机