UNPKG

7.27 kBTypeScriptView Raw
1import Taro from '../../index'
2
3declare module '../../index' {
4 /**
5 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/CameraContext.html
6 */
7 interface CameraContext {
8 /** 获取 Camera 实时帧数据
9 *
10 * ****
11 *
12 * 注: 使用该接口需同时在 [camera](/docs/components/media/camera) 组件属性中指定 frame-size。
13 * @supported weapp
14 * @example
15 * ```tsx
16 * const context = wx.createCameraContext()
17 * const listener = context.onCameraFrame((frame) => {
18 * console.log(frame.data instanceof ArrayBuffer, frame.width, frame.height)
19 * })
20 * listener.start()
21 * ```
22 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/CameraContext.onCameraFrame.html
23 */
24 onCameraFrame(
25 /** 回调函数 */
26 callback: CameraContext.OnCameraFrameCallback,
27 ): CameraFrameListener
28 /** 设置缩放级别
29 * @supported weapp
30 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/CameraContext.setZoom.html
31 */
32 setZoom(option: CameraContext.SetZoomOption): void
33 /** 开始录像
34 * @supported weapp, rn
35 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/CameraContext.startRecord.html
36 */
37 startRecord(option: CameraContext.StartRecordOption): void
38 /** 结束录像
39 * @supported weapp, rn
40 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/CameraContext.stopRecord.html
41 */
42 stopRecord(option?: CameraContext.StopRecordOption): void
43 /** 拍摄照片
44 * @supported weapp, rn
45 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/CameraContext.takePhoto.html
46 */
47 takePhoto(option: CameraContext.TakePhotoOption): void
48 }
49
50 namespace CameraContext {
51 interface SetZoomOption {
52 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
53 complete?: (res: TaroGeneral.CallbackResult) => void
54 /** 接口调用失败的回调函数 */
55 fail?: (res: TaroGeneral.CallbackResult) => void
56 /** 接口调用成功的回调函数 */
57 success?: (res: StartRecordSuccessCallbackResult) => void
58 /** 缩放级别,范围[1, maxZoom]。zoom 可取小数,精确到小数后一位。maxZoom 可在 bindinitdone 返回值中获取。 */
59 zoom: number
60 }
61 interface StartRecordSuccessCallbackResult extends TaroGeneral.CallbackResult {
62 /** 实际设置的缩放级别。由于系统限制,某些机型可能无法设置成指定值,会改用最接近的可设值。 */
63 zoom: number
64 }
65 interface StartRecordOption {
66 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
67 complete?: (res: TaroGeneral.CallbackResult) => void
68 /** 接口调用失败的回调函数 */
69 fail?: (res: TaroGeneral.CallbackResult) => void
70 /** 接口调用成功的回调函数 */
71 success?: (res: TaroGeneral.CallbackResult) => void
72 /** 超过30s或页面 `onHide` 时会结束录像 */
73 timeoutCallback?: StartRecordTimeoutCallback
74 }
75 /** 超过30s或页面 `onHide` 时会结束录像 */
76 type StartRecordTimeoutCallback = (
77 result: StartRecordTimeoutCallbackResult,
78 ) => void
79 interface StartRecordTimeoutCallbackResult {
80 /** 封面图片文件的临时路径 */
81 tempThumbPath: string
82 /** 视频的文件的临时路径 */
83 tempVideoPath: string
84 }
85 interface StopRecordOption {
86 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
87 complete?: (res: TaroGeneral.CallbackResult) => void
88 /** 接口调用失败的回调函数 */
89 fail?: (res: TaroGeneral.CallbackResult) => void
90 /** 接口调用成功的回调函数 */
91 success?: (result: StopRecordSuccessCallbackResult) => void
92 }
93 interface StopRecordSuccessCallbackResult extends TaroGeneral.CallbackResult {
94 /** 封面图片文件的临时路径 */
95 tempThumbPath: string
96 /** 视频的文件的临时路径 */
97 tempVideoPath: string
98 /** 调用结果 */
99 errMsg: string
100 }
101 interface TakePhotoOption {
102 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
103 complete?: (res: TaroGeneral.CallbackResult) => void
104 /** 接口调用失败的回调函数 */
105 fail?: (res: TaroGeneral.CallbackResult) => void
106 /** 成像质量 */
107 quality?: keyof Quality
108 /** 接口调用成功的回调函数 */
109 success?: (result: TakePhotoSuccessCallbackResult) => void
110 }
111 interface TakePhotoSuccessCallbackResult extends TaroGeneral.CallbackResult {
112 /** 照片文件的临时路径,安卓是jpg图片格式,ios是png */
113 tempImagePath: string
114 /** 调用结果 */
115 errMsg: string
116 }
117 /** 回调函数 */
118 type OnCameraFrameCallback = (result: OnCameraFrameCallbackResult) => void
119 interface OnCameraFrameCallbackResult {
120 /** 图像像素点数据,一维数组,每四项表示一个像素点的 rgba */
121 data: ArrayBuffer
122 /** 图像数据矩形的高度 */
123 height: number
124 /** 图像数据矩形的宽度 */
125 width: number
126 }
127 interface Quality {
128 /** 高质量 */
129 high
130 /** 普通质量 */
131 normal
132 /** 低质量 */
133 low
134 }
135 }
136
137 /** CameraContext.onCameraFrame() 返回的监听器。
138 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/CameraFrameListener.html
139 */
140 interface CameraFrameListener {
141 /** 开始监听帧数据
142 * @supported weapp
143 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/CameraFrameListener.start.html
144 */
145 start(option?: CameraFrameListener.StartOption): void
146 /** 停止监听帧数据
147 * @supported weapp
148 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/CameraFrameListener.stop.html
149 */
150 stop(option?: CameraFrameListener.StopOption): void
151 }
152
153 namespace CameraFrameListener {
154 interface StartOption {
155 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
156 complete?: (res: TaroGeneral.CallbackResult) => void
157 /** 接口调用失败的回调函数 */
158 fail?: (res: TaroGeneral.CallbackResult) => void
159 /** 接口调用成功的回调函数 */
160 success?: (res: TaroGeneral.CallbackResult) => void
161 }
162 interface StopOption {
163 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
164 complete?: (res: TaroGeneral.CallbackResult) => void
165 /** 接口调用失败的回调函数 */
166 fail?: (res: TaroGeneral.CallbackResult) => void
167 /** 接口调用成功的回调函数 */
168 success?: (res: TaroGeneral.CallbackResult) => void
169 }
170 }
171
172 interface TaroStatic {
173 /** 创建 camera 上下文 CameraContext 对象。
174 * @supported weapp, rn
175 * @example
176 * ```tsx
177 * const cameraContext = Taro.createCameraContext()
178 * ```
179 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/camera/wx.createCameraContext.html
180 */
181 createCameraContext(): CameraContext
182 }
183}