CameraContext.takePhoto

解释:拍照,可指定质量,成功则返回图片。

方法参数

Object object

object参数说明

参数名类型必填默认值说明
qualityStringnormal成像质量,有效值为high, normal, low。
successFunction接口调用成功的回调函数,res = { tempImagePath }。
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

示例

在开发者工具中预览效果

扫码体验

CameraContext.takePhoto - 图1请使用百度APP扫码

图片示例

CameraContext.takePhoto - 图2

CameraContext.takePhoto - 图3

CameraContext.takePhoto - 图4

代码示例

  • 在 swan 文件中
  1. <div class="camera">
  2. <camera device-position="{{device}}" flash="off" binderror="error" style="width: 100%; height: 500rpx;"></camera>
  3. <button type="primary" bind:tap="takePhoto">拍照</button>
  4. <image class="img" mode="widthFix" src="{{src}}"></image>
  5. </div>
  • 在 js 文件中
Page({
    data: {
        src: ''
    },
    onShow() {
        const cameraContext = swan.createCameraContext();
        this.cameraContext = cameraContext;
    },
    takePhoto() {
        this.cameraContext.takePhoto({
            quality: 'high',
            success: res => {
                this.setData({
                    src: res.tempImagePath
                });
            }
        });
    }
});