| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import {
- ICameraService,
- MediaStreamConstraints,
- } from './CameraService';
- /**
- * Cordova 环境下的摄像头服务实现
- * 使用 Cordova Camera 插件
- */
- export class CordovaCameraService implements ICameraService {
- /**
- * 请求摄像头权限
- * Cordova 插件通常在第一次使用时自动请求权限
- * @returns 总是返回 true
- */
- async requestPermission(): Promise<boolean> {
- // Cordova 插件会在实际使用时请求权限
- return true;
- }
- /**
- * 获取媒体流
- * Cordova 不使用 MediaStream,此方法会抛出错误
- * @param constraints 媒体约束条件(未使用)
- * @returns 抛出错误
- */
- async getMediaStream(
- constraints?: MediaStreamConstraints
- ): Promise<MediaStream> {
- throw new Error(
- 'Cordova 环境不支持 MediaStream,请直接使用 capturePhoto 方法'
- );
- }
- /**
- * 捕获照片
- * 使用 Cordova Camera 插件直接拍照
- * @param stream 媒体流(在 Cordova 中未使用)
- * @returns base64 格式的图片数据
- */
- async capturePhoto(stream?: MediaStream): Promise<string> {
- return new Promise((resolve, reject) => {
- // 检查 Cordova Camera 插件是否可用
- if (!navigator.camera) {
- reject(new Error('Cordova Camera 插件未安装或不可用'));
- return;
- }
- // 检查 Camera 常量是否可用
- if (typeof Camera === 'undefined') {
- reject(new Error('Camera 常量不可用,请检查 Cordova 插件配置'));
- return;
- }
- // 配置 Camera 选项
- const options = {
- quality: 80,
- destinationType: Camera.DestinationType.DATA_URL,
- sourceType: Camera.PictureSourceType.CAMERA,
- encodingType: Camera.EncodingType.JPEG,
- mediaType: Camera.MediaType.PICTURE,
- correctOrientation: true,
- targetWidth: 1280,
- targetHeight: 720,
- saveToPhotoAlbum: false,
- };
- // 调用 Camera 插件
- navigator.camera.getPicture(
- (imageData) => {
- // 添加 base64 前缀
- const base64WithPrefix = `data:image/jpeg;base64,${imageData}`;
- resolve(base64WithPrefix);
- },
- (error) => {
- reject(new Error(`拍照失败: ${error}`));
- },
- options
- );
- });
- }
- /**
- * 停止媒体流
- * Cordova 不需要停止流
- * @param stream 媒体流(未使用)
- */
- stopStream(stream: MediaStream): void {
- // Cordova 不需要停止流
- // 什么也不做
- }
- }
|