uniapp微信小程序的拍照功能

1.实现的效果

2.实现代码

(1)pages/index/index中

<template>
	<view class="content">
		<image class="logo" src="/static/header.jpg"></image>
		<view class="text-area">
			<button class="title" @click="goPhoto">拍照</button>
		</view>
	</view>
</template>
 
<script>
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {
 
		},
		methods: {
			goPhoto(){
				uni.navigateTo({
					url: '/pages/takephoto/takephoto'
				})
			}
		}
	}
</script>
 
<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
 
	.logo {
		height: 300rpx;
		width: 300rpx;
		border-radius: 150rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}
 
	.text-area {
		display: flex;
		justify-content: center;
	}
 
	button{
		width: 120px;
		line-height: 62px;
		font-size: 58rpx;
		color: white;
		background-color: green;
	}
</style>

(2)pages/takephoto/takephoto中

<template>
	<view>
		<camera device-position="back" flash="off" :style="'width:100%;height:' + height+'px'">
		</camera>
		<view class="btn-box">
			<button class="photo-btn" type="primary" @click="takePhoto">拍照</button>
			<view class="perview-btn">
				<image :src="src" style="width: 46px;height: 46px"></image>
			</view>
		</view>
	</view>
</template>
 
<script>
	export default {
		data() {
			return {
				src: "",
				height: undefined,
			}
		},
		onLoad(){
			this.getSystemInfo()
		},
		methods: {	
			takePhoto() {
				let that= this
				const ctx = wx.createCameraContext()
				console.log("ctx", ctx);
				ctx.takePhoto({
				  quality: 'high',
				  success: (res) => {
					  console.log("res", res);
				      that.src= res.tempImagePath
				  }
				})
			}
        }
			
</script>
 
<style>
.photo-btn{
	width: 46px;
	height: 46px;
	border-radius: 46px;
	margin-top: 10px;
}
.btn-box{
	height: 66px;
	display: flex;
	position: relative;
}
.perview-btn{
	width: 46px;
	height: 46px;
	border: 1px solid #000000;
	opacity: 0.2;
	position: absolute;
	left: 10px;
	top: 10px;
}
</style>

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值