207 lines
5.4 KiB
Plaintext
207 lines
5.4 KiB
Plaintext
import {
|
|
TakePhotoOption,
|
|
GeneralCallbackResult,
|
|
CameraContextSetZoomOption,
|
|
CameraContextStartRecordOption,
|
|
CameraContextStopRecordOption,
|
|
OnCameraFrameCallback,
|
|
CameraFrameListenerStartOption,
|
|
StopOption,
|
|
TakePhoto,
|
|
SetZoom,
|
|
StartRecord,
|
|
StopRecord,
|
|
StartCameraFrame,
|
|
StopCameraFrame,
|
|
OnCameraFrameListenerOption,
|
|
SwitchCamera,
|
|
SwitchOption
|
|
} from '../interface'
|
|
import { CameraManager } from './CameraManager';
|
|
|
|
class CameraFrameListener {
|
|
context : CameraContext
|
|
constructor(context : CameraContext) {
|
|
this.context = context
|
|
}
|
|
start() : void
|
|
start(option ?: CameraFrameListenerStartOption) {
|
|
CameraManager.getInstance().getCameraView()?.cameraFrameOnStart(option)
|
|
}
|
|
stop() : void
|
|
stop(option ?: StopOption) {
|
|
CameraManager.getInstance().getCameraView()?.cameraFrameOnStop(option)
|
|
}
|
|
}
|
|
|
|
class CameraContext {
|
|
private mCameraFrameListener : CameraFrameListener | null = null
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
takePhoto(option : TakePhotoOption) {
|
|
let camera = CameraManager.getInstance().getCameraView()
|
|
if (camera == null) {
|
|
option.fail?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
option.complete?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
return;
|
|
}
|
|
camera?.takePhoto(option)
|
|
}
|
|
|
|
setZoom(option : CameraContextSetZoomOption) {
|
|
let camera = CameraManager.getInstance().getCameraView()
|
|
if (camera == null) {
|
|
option.fail?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
option.complete?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
return;
|
|
}
|
|
|
|
camera?.setZoom(option)
|
|
}
|
|
|
|
startRecord(option : CameraContextStartRecordOption) {
|
|
let camera = CameraManager.getInstance().getCameraView()
|
|
if (camera == null) {
|
|
option.fail?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
option.complete?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
return;
|
|
}
|
|
camera?.startRecord(option)
|
|
}
|
|
|
|
stopRecord(option : CameraContextStopRecordOption) {
|
|
let camera = CameraManager.getInstance().getCameraView()
|
|
if (camera == null) {
|
|
option.fail?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
option.complete?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
return;
|
|
}
|
|
camera?.stopRecord(option)
|
|
}
|
|
|
|
onCameraFrame() : CameraFrameListener
|
|
onCameraFrame(callback : OnCameraFrameCallback | null = null) : CameraFrameListener {
|
|
if (this.mCameraFrameListener == null) {
|
|
this.mCameraFrameListener = new CameraFrameListener(this)
|
|
}
|
|
CameraManager.getInstance().getCameraView()?.onCameraFrame(callback)
|
|
return this.mCameraFrameListener!
|
|
}
|
|
//////////////////////////////////////////////////////
|
|
onCameraFrameListener(option : OnCameraFrameListenerOption) {
|
|
let camera = CameraManager.getInstance().getCameraView()
|
|
if (camera == null) {
|
|
return;
|
|
}
|
|
camera?.onCameraFrameListener(option)
|
|
}
|
|
|
|
cameraFrameOnStart(option : CameraFrameListenerStartOption) {
|
|
let camera = CameraManager.getInstance().getCameraView()
|
|
if (camera == null) {
|
|
option.fail?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
option.complete?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
return;
|
|
}
|
|
camera?.cameraFrameOnStart(option)
|
|
}
|
|
|
|
cameraFrameOnStop(option : StopOption) {
|
|
let camera = CameraManager.getInstance().getCameraView()
|
|
if (camera == null) {
|
|
option.fail?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
option.complete?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
return;
|
|
}
|
|
camera?.cameraFrameOnStop(option)
|
|
}
|
|
|
|
switchCamera(option : SwitchOption) {
|
|
let camera = CameraManager.getInstance().getCameraView()
|
|
if (camera == null) {
|
|
option.fail?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
option.complete?.({
|
|
errMsg: '未找到相机'
|
|
} as GeneralCallbackResult)
|
|
return;
|
|
}
|
|
camera?.switchCamera(option.position)
|
|
|
|
option.success?.({
|
|
errMsg: 'ok'
|
|
} as GeneralCallbackResult)
|
|
option.complete?.({
|
|
errMsg: 'ok'
|
|
} as GeneralCallbackResult)
|
|
}
|
|
}
|
|
|
|
let context : CameraContext = new CameraContext();
|
|
|
|
export function createCameraContext() : CameraContext {
|
|
return context;
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
export const takePhoto : TakePhoto = function (options : TakePhotoOption) {
|
|
context.takePhoto(options)
|
|
}
|
|
|
|
export const setZoom : SetZoom = function (options : CameraContextSetZoomOption) {
|
|
context.setZoom(options)
|
|
}
|
|
|
|
export const startRecord : StartRecord = function (options : CameraContextStartRecordOption) {
|
|
context.startRecord(options)
|
|
}
|
|
|
|
export const stopRecord : StopRecord = function (options : CameraContextStopRecordOption) {
|
|
context.stopRecord(options)
|
|
}
|
|
|
|
@UTSJS.keepAlive
|
|
export function onCameraFrameListener(options : OnCameraFrameListenerOption) {
|
|
context.onCameraFrameListener(options)
|
|
}
|
|
|
|
export const startCameraFrame : StartCameraFrame = function (options : CameraFrameListenerStartOption) {
|
|
context.cameraFrameOnStart(options)
|
|
}
|
|
|
|
export const stopCameraFrame : StopCameraFrame = function (options : StopOption) {
|
|
context.cameraFrameOnStop(options)
|
|
}
|
|
|
|
export const switchCamera : SwitchCamera = function (options : SwitchOption) {
|
|
context.switchCamera(options)
|
|
} |