init
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
import { CameraView } from './camera'
|
||||
import {
|
||||
TakePhotoOption,
|
||||
GeneralCallbackResult,
|
||||
CameraContextSetZoomOption,
|
||||
CameraContextStartRecordOption,
|
||||
CameraContextStopRecordOption,
|
||||
// CameraFrameListener,
|
||||
OnCameraFrameCallback,
|
||||
// OnCameraFrameCallbackResult,
|
||||
CameraFrameListenerStartOption,
|
||||
StopOption,
|
||||
TakePhoto,
|
||||
SetZoom,
|
||||
StartRecord,
|
||||
StopRecord,
|
||||
StartCameraFrame,
|
||||
StopCameraFrame,
|
||||
OnCameraFrameListenerOption,
|
||||
SwitchOption,
|
||||
SwitchCamera
|
||||
} from '../interface'
|
||||
import { CameraManager } from "./CameraManager";
|
||||
|
||||
|
||||
class CameraFrameListener {
|
||||
context : CameraContext
|
||||
constructor(context : CameraContext) {
|
||||
this.context = context
|
||||
}
|
||||
start() : void
|
||||
start(option ?: CameraFrameListenerStartOption) {
|
||||
this.context.getCamera().then((camera : CameraView) => {
|
||||
camera.cameraFrameOnStart(option)
|
||||
})
|
||||
}
|
||||
stop() : void
|
||||
stop(option ?: StopOption) {
|
||||
this.context.getCamera().then((camera : CameraView) => {
|
||||
camera.cameraFrameOnStop(option)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
class CameraContext {
|
||||
private mCameraFrameListener : CameraFrameListener | null = null
|
||||
constructor() {
|
||||
}
|
||||
getCamera() : Promise<CameraView> {
|
||||
let cameraPreview = CameraManager.getInstance().getCameraView()
|
||||
if (cameraPreview != null) {
|
||||
return Promise.resolve<CameraView>(cameraPreview);
|
||||
}
|
||||
let resolveFunc : ((res : CameraView) => void) | null = null
|
||||
let rejectFunc : ((res : string) => void) | null = null
|
||||
|
||||
let maxAttempts : number = 5
|
||||
let currentAttempt : number = 0
|
||||
|
||||
function attemptGetCamera() {
|
||||
let camera = CameraManager.getInstance().getCameraView()
|
||||
if (camera !== null) {
|
||||
resolveFunc?.(camera)
|
||||
} else if (currentAttempt < maxAttempts) {
|
||||
currentAttempt++
|
||||
setTimeout(() => {
|
||||
attemptGetCamera()
|
||||
}, 100)
|
||||
} else {
|
||||
rejectFunc?.('Camera not found after multiple attempts')
|
||||
}
|
||||
}
|
||||
return new Promise<CameraView>((resolve, reject) => {
|
||||
resolveFunc = resolve
|
||||
rejectFunc = reject
|
||||
attemptGetCamera()
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
takePhoto(option : TakePhotoOption) {
|
||||
this.getCamera().then((camera : CameraView) => {
|
||||
camera.takePhoto(option)
|
||||
}).catch(() => {
|
||||
option.fail?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
option.complete?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
})
|
||||
|
||||
}
|
||||
setZoom(option : CameraContextSetZoomOption) {
|
||||
this.getCamera().then((camera : CameraView) => {
|
||||
camera.setZoom(option)
|
||||
}).catch(() => {
|
||||
option.fail?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
option.complete?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
})
|
||||
}
|
||||
startRecord(option : CameraContextStartRecordOption) {
|
||||
this.getCamera().then((camera : CameraView) => {
|
||||
camera.startRecord(option)
|
||||
}).catch(() => {
|
||||
option.fail?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
option.complete?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
})
|
||||
}
|
||||
stopRecord(option : CameraContextStopRecordOption) {
|
||||
this.getCamera().then((camera : CameraView) => {
|
||||
camera.stopRecord(option)
|
||||
}).catch(() => {
|
||||
option.fail?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
option.complete?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
})
|
||||
}
|
||||
onCameraFrame() : CameraFrameListener
|
||||
onCameraFrame(callback : OnCameraFrameCallback | null = null) : CameraFrameListener {
|
||||
if (this.mCameraFrameListener == null) {
|
||||
this.mCameraFrameListener = new CameraFrameListener(this)
|
||||
}
|
||||
this.getCamera().then((camera : CameraView) => {
|
||||
camera.onCameraFrame(callback)
|
||||
})
|
||||
return this.mCameraFrameListener!
|
||||
}
|
||||
//////////////////////////////////////////////////////
|
||||
onCameraFrameListener(option : OnCameraFrameListenerOption) {
|
||||
this.getCamera().then((camera : CameraView) => {
|
||||
camera.onCameraFrameListener(option)
|
||||
}).catch(() => {
|
||||
console.log("未找到相机")
|
||||
})
|
||||
}
|
||||
|
||||
cameraFrameOnStart(option : CameraFrameListenerStartOption) {
|
||||
this.getCamera().then((camera : CameraView) => {
|
||||
camera.cameraFrameOnStart(option)
|
||||
}).catch(() => {
|
||||
option.fail?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
option.complete?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
})
|
||||
}
|
||||
|
||||
cameraFrameOnStop(option : StopOption) {
|
||||
this.getCamera().then((camera : CameraView) => {
|
||||
camera.cameraFrameOnStop(option)
|
||||
}).catch(() => {
|
||||
option.fail?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
option.complete?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
})
|
||||
}
|
||||
|
||||
switchCamera(option : SwitchOption) {
|
||||
this.getCamera().then((camera : CameraView) => {
|
||||
camera.switchCamera(option.position)
|
||||
|
||||
option.success?.({
|
||||
errMsg: 'ok'
|
||||
} as GeneralCallbackResult)
|
||||
option.complete?.({
|
||||
errMsg: 'ok'
|
||||
} as GeneralCallbackResult)
|
||||
}).catch(() => {
|
||||
option.fail?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
option.complete?.({
|
||||
errMsg: '未找到相机'
|
||||
} as GeneralCallbackResult)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let context : CameraContext = new CameraContext();
|
||||
|
||||
export function createCameraContext() : CameraContext {
|
||||
return context;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
export const takePhoto : TakePhoto = function (options : TakePhotoOption) {
|
||||
UTSAndroid.getDispatcher("main").async(function (_) {
|
||||
context.takePhoto(options)
|
||||
}, null)
|
||||
}
|
||||
|
||||
export const setZoom : SetZoom = function (options : CameraContextSetZoomOption) {
|
||||
UTSAndroid.getDispatcher("main").async(function (_) {
|
||||
context.setZoom(options)
|
||||
}, null)
|
||||
}
|
||||
|
||||
export const startRecord : StartRecord = function (options : CameraContextStartRecordOption) {
|
||||
UTSAndroid.getDispatcher("main").async(function (_) {
|
||||
context.startRecord(options)
|
||||
}, null)
|
||||
}
|
||||
|
||||
export const stopRecord : StopRecord = function (options : CameraContextStopRecordOption) {
|
||||
UTSAndroid.getDispatcher("main").async(function (_) {
|
||||
context.stopRecord(options)
|
||||
}, null)
|
||||
}
|
||||
|
||||
@UTSJS.keepAlive
|
||||
export function onCameraFrameListener(options : OnCameraFrameListenerOption) {
|
||||
UTSAndroid.getDispatcher("main").async(function (_) {
|
||||
context.onCameraFrameListener(options)
|
||||
}, null)
|
||||
}
|
||||
|
||||
export const startCameraFrame : StartCameraFrame = function (options : CameraFrameListenerStartOption) {
|
||||
UTSAndroid.getDispatcher("main").async(function (_) {
|
||||
context.cameraFrameOnStart(options)
|
||||
}, null)
|
||||
}
|
||||
|
||||
export const stopCameraFrame : StopCameraFrame = function (options : StopOption) {
|
||||
UTSAndroid.getDispatcher("main").async(function (_) {
|
||||
context.cameraFrameOnStop(options)
|
||||
}, null)
|
||||
}
|
||||
|
||||
|
||||
export const switchCamera : SwitchCamera = function (options : SwitchOption) {
|
||||
UTSAndroid.getDispatcher("main").async(function (_) {
|
||||
context.switchCamera(options)
|
||||
}, null)
|
||||
}
|
||||
Reference in New Issue
Block a user