微信小程序自定义相机(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);
          },
        })
      }
    })
  });
}
相关推荐
2601_949593651 小时前
基础入门 React Native 鸿蒙跨平台开发:卡片组件
react native·react.js·harmonyos
qq_177767372 小时前
React Native鸿蒙跨平台剧集管理应用实现,包含主应用组件、剧集列表、分类筛选、搜索排序等功能模块
javascript·react native·react.js·交互·harmonyos
qq_177767372 小时前
React Native鸿蒙跨平台自定义复选框组件,通过样式数组实现选中/未选中状态的样式切换,使用链式调用替代样式数组,实现状态驱动的样式变化
javascript·react native·react.js·架构·ecmascript·harmonyos·媒体
烬头88213 小时前
React Native鸿蒙跨平台采用了函数式组件的形式,通过 props 接收分类数据,使用 TouchableOpacity实现了点击交互效果
javascript·react native·react.js·ecmascript·交互·harmonyos
qq_177767373 小时前
React Native鸿蒙跨平台通过Animated.Value.interpolate实现滚动距离到动画属性的映射
javascript·react native·react.js·harmonyos
●VON4 小时前
React Native for OpenHarmony:项目目录结构与跨平台构建流程详解
javascript·学习·react native·react.js·架构·跨平台·von
qq_177767375 小时前
React Native鸿蒙跨平台实现消息列表用于存储所有消息数据,筛选状态用于控制消息筛选结果
javascript·react native·react.js·ecmascript·harmonyos
摘星编程5 小时前
React Native + OpenHarmony:自定义useEllipsis省略号处理
javascript·react native·react.js
2601_949593656 小时前
基础入门 React Native 鸿蒙跨平台开发:Animated 动画按钮组件 鸿蒙实战
react native·react.js·harmonyos
暮志未晚Webgl6 小时前
UE5使用CameraShake相机震动提升游戏体验
数码相机·游戏·ue5