React Native开发之expo中camera的基本使用
之前做RN项目没调用过本地摄像头,今天下班早,做了一个简单的小demo:主要实现的功能:点击拍照按钮进入拍照界面,点击flip进行前后摄像头转换,点击开始拍照实现拍照功能(没写保存到本地的功能,大家可以自主开发),代码是参照expo官网的Camera写的一个小demo,大家可以结合的expo官网来看,该加注释的地方都在代码中加了,希望能对你有所帮助。
import React from 'react'
import {
View,
Text,
TouchableOpacity,
Button,
Image
} from 'react-native'
import { Camera, Permissions } from 'expo';
interface Props{
}
//定义Camera的两个属性
interface State{
hasCameraPermission?:any,
type?:any,
isShowCamera: Boolean,
uri:string
}
export default class componentName extends React.Component<Props,State> {
public camera:any //定义一个camera来拿到Camera节点
constructor(props:Props) {
super(props)
this.state = {
hasCameraPermission: null, //照相机权限
type: Camera.Constants.Type.back, //照相机类型
isShowCamera: false, //是否开启照相机
uri: ''
}
}
async componentWillMount() {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({ hasCameraPermission: status === 'granted' });
}
//把官网里面的render粘过来
render() {
const { hasCameraPermission } = this.state;
if (hasCameraPermission === null) {
return <View />;
} else if (hasCameraPermission === false) {
return <Text>没有权限打开照相机</Text>;
} else {
return (
<View style={{ flex: 1, paddingTop: 20 }}>
{
!this.state.isShowCamera ?
<View>
<View>
<Image source={{uri:this.state.uri}} style={{width: 200, height: 200}}></Image>
</View>
<Button
onPress={this.takePicture.bind(this)}
title='拍照'
></Button>
</View>:
<Camera
style={{ flex: 1 }}
type={this.state.type}
ref={(el:any)=>this.camera=el} //参照官网的Methods
>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row',
}}>
<TouchableOpacity
style={{
flex: 1,
alignSelf: 'flex-end',
alignItems: 'center',
}}
onPress={() => {
this.setState({
type: this.state.type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back,
});
}}>
<Text
style={{ fontSize: 18, marginBottom: 10, color: 'white' }}>
{' '}Flip{' '}
</Text>
</TouchableOpacity>
{/* 复制一个开始拍照的点击按钮 */}
<TouchableOpacity
style={{
flex: 1, //flex为0.1改成flex为1
alignSelf: 'flex-end',
alignItems: 'center',
}}
//参照官网的Methods
onPress={async () => {
if (this.camera) {
let photo = await this.camera.takePictureAsync();
console.log(photo)
this.setState({
isShowCamera: false,
uri: photo.uri
})
}
}}>
<Text
style={{ fontSize: 18, marginBottom: 10, color: 'white' }}>
{' '}开始拍照{' '}
</Text>
</TouchableOpacity>
</View>
</Camera>
}
</View>
);
}
}
takePicture(){
this.setState({
isShowCamera: true
})
}
}
控制台打印的photo结果:

React Native开发之expo中camera的基本使用的更多相关文章
- React Native开发之npm start加速
在Windows下好不容易安装好React Native环境之后,运行npm start,结果就是无限被等待,快的话160秒(将近3分钟啊....) 而Mac下因为有watchman所以是飞一样的速度 ...
- 转 : React Native 开发之 IDE 选型和配置
转:https://mp.weixin.qq.com/s?__biz=MzA3ODg4MDk0Ng==&mid=2651112392&idx=1&sn=135e29ddde30 ...
- React Native开发之IDE(Atom+Nuclide)安装,运行,调试
版权声明:本文为博主原创文章,如需转载请注明出处 目录(?)[-] 前言 MacWindowsLinux 准备工作 安装Atom 安装Nuclide 新建一个工程 自动补全 类型标注 语法检查 跳 ...
- React—Native开发之 Could not connect to development server(Android)解决方法
作为初学者昨天还好好能跑的项目今天就会遇到突然爆红出错是经常的事,让我们来看下是什么错吧 先来翻译: 连接不到开发的服务器. 请按照以下的步骤来修复此问题: 确保包服务器在运行确保你的设备或者模拟器连 ...
- Android安全开发之WebView中的地雷
Android安全开发之WebView中的地雷 0X01 About WebView 在Android开发中,经常会使用WebView来实现WEB页面的展示,在Activiry中启动自己的浏览器,或者 ...
- JavaEE开发之Spring中Bean的作用域、Init和Destroy方法以及Spring-EL表达式
上篇博客我们聊了<JavaEE开发之Spring中的依赖注入以及AOP>,本篇博客我们就来聊一下Spring框架中的Bean的作用域以及Bean的Init和Destroy方法,然后在聊一下 ...
- JavaEE开发之Spring中的多线程编程以及任务定时器详解
上篇博客我们详细的聊了Spring中的事件的发送和监听,也就是常说的广播或者通知一类的东西,详情请移步于<JavaEE开发之Spring中的事件发送与监听以及使用@Profile进行环境切换&g ...
- JavaEE开发之Spring中的条件注解组合注解与元注解
上篇博客我们详细的聊了<JavaEE开发之Spring中的多线程编程以及任务定时器详解>,本篇博客我们就来聊聊条件注解@Conditional以及组合条件.条件注解说简单点就是根据特定的条 ...
- JavaEE开发之SpringMVC中的自定义拦截器及异常处理
上篇博客我们聊了<JavaEE开发之SpringMVC中的路由配置及参数传递详解>,本篇博客我们就聊一下自定义拦截器的实现.以及使用ModelAndView对象将Controller的值加 ...
随机推荐
- scanf和scanf_s在VS2013中的使用
转载:https://www.cnblogs.com/liuchaojiayou/p/4418215.html 在VS2013中,每次使用scanf都会报错:This function or vari ...
- [转] RISC-V架构介绍
1. RISC-V和其他开放架构有何不同 如果仅从"免费"或"开放"这两点来评判,RISC-V架构并不是第一个做到免费或开放的处理器架构. 在开始之前,我们先通 ...
- 第四次工业革命:人工智能(AI)入门
转载自 http://www.infoq.com/cn/articles/the-fourth-industrial-revolution-an-introduction-to-ai "过去 ...
- Azkaban调度器
Azkaban介绍 Azkaban 是由 Linkedin 公司推出的一个批量工作流任务调度器,用于在一个工作流内以一个特定的顺序运行一组工作和流程.Azkaban 使用 job 配置文件建立任务之间 ...
- Can't create new folder in windows7
First, please use System File Checker tool to troubleshoot(诊断) this issue. If the issue persists, im ...
- cmd:相关命令和笔记
(1)查看git版本:git --version (2)
- maven仓库使用HTTP代理,maven仓库使用本地jar
setting.xml <proxies> <proxy> <id>proxy</id> <active>true</active&g ...
- January 18 2017 Week 3 Wednesday
True liberty is to have power over oneself in all things. 真正的自由是在所有的事情上都能控制住自己. Liberty isn't meanin ...
- jquery获取前一个月日期
一) 重构Date对象: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年 ...
- BZOJ 3680: 吊打XXX (模拟退火)
//yy:今天简单入门学了下ORZ 爬山算法:兔子朝着比现在高的地方跳去.它找到了不远处的最高山峰.但是这座山不一定是珠穆朗玛峰.这就是爬山算法,它不能保证局部最优值就是全局最优值. 模拟退火:兔子喝 ...