微信小程序自定义相机(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);
          },
        })
      }
    })
  });
}
相关推荐
格林威16 分钟前
工业相机如何通过光度立体成像技术实现高效精准的2.5D缺陷检测
人工智能·深度学习·数码相机·yolo·计算机视觉
点灯小铭1 小时前
基于MATLAB的车牌识别系统
开发语言·单片机·数码相机·matlab·毕业设计·课程设计
Python大数据分析2 小时前
uniapp微信小程序商品列表数据分页+本地缓存+下拉刷新+图片懒加载
缓存·微信小程序·uni-app
小白_ysf2 小时前
uniapp和vue3项目中引入echarts 、lime-echart(微信小程序、H5等)
微信小程序·uni-app·echarts·h5·lime-echart
imHere·3 小时前
UniApp 分包异步化配置及组件引用解决方案
微信小程序·uni-app·分包
Cyann4 小时前
Day1- React基础组件使用
前端·react.js
canglingyue5 小时前
微信小程序加速计开发指南
微信小程序·小程序
SY_FC5 小时前
uniapp发布成 微信小程序 主包内 main.wxss 体积太大
微信小程序·小程序·uni-app
霸气小男5 小时前
解决React中通过外部引入的css/scss/less文件更改antDesign中Modal组件内部的样式不生效问题
css·react.js
Developer-YC6 小时前
像素图生成小程序开发全解析:从图片上传到Excel图纸
java·javascript·图像处理·微信小程序·excel