diff --git a/ohos/src/main/ets/CameraUtil.ets b/ohos/src/main/ets/CameraUtil.ets index 435cda3750db87a08d1410dd5b9bb21f8b590688..baa2a53948db017c3f25eb964d53a75c983dbf58 100644 --- a/ohos/src/main/ets/CameraUtil.ets +++ b/ohos/src/main/ets/CameraUtil.ets @@ -13,164 +13,54 @@ * limitations under the License. */ -import camera from '@ohos.multimedia.camera'; import { BusinessError } from '@ohos.base'; import common from '@ohos.app.ability.common'; -import image from '@ohos.multimedia.image'; +import { camera } from '@kit.CameraKit'; +import Log from '@ohos/flutter_ohos/src/main/ets/util/Log'; + +const TAG: string = "CameraUtil"; /** * 如果获取对象失败,说明相机可能被占用或无法使用。如果被占用,须等到相机被释放后才能重新获取。 * @param context * @returns */ -export function getCameraManager(context: common.BaseContext): camera.CameraManager { - let cameraManager: camera.CameraManager = camera.getCameraManager(context); - return cameraManager; -} - -/** - * 通过cameraManager类中的getSupportedCameras()方法,获取当前设备支持的相机列表,列表中存储了设备支持的所有相机ID。 - * 若列表不为空,则说明列表中的每个ID都支持独立创建相机对象;否则,说明当前设备无可用相机,不可继续后续操作。 - * @param cameraManager - * @returns - */ -export function getCameraDevices(cameraManager: camera.CameraManager): Array { - let cameraArray: Array = cameraManager.getSupportedCameras(); - if (cameraArray != undefined && cameraArray.length <= 0) { - console.error("[camera test] cameraManager.getSupportedCameras error"); - return []; - } - for (let index = 0; index < cameraArray.length; index++) { - console.info('[camera test] cameraId : ' + cameraArray[index].cameraId); // 获取相机ID - console.info('[camera test] cameraPosition : ' + cameraArray[index].cameraPosition); // 获取相机位置 - console.info('[camera test] cameraType : ' + cameraArray[index].cameraType); // 获取相机类型 - console.info('[camera test] connectionType : ' + cameraArray[index].connectionType); // 获取相机连接类型 - } - return cameraArray; -} - -/** - * 调用cameraManager类中的createCaptureSession()方法创建一个会话 - * @param cameraManager - * @returns - */ -export function getCaptureSession(cameraManager: camera.CameraManager): camera.CaptureSession | undefined { - let captureSession: camera.CaptureSession | undefined = undefined; - try { - captureSession = cameraManager.createCaptureSession(); - } catch (error) { - let err = error as BusinessError; - console.error(`[camera test] Failed to create the CaptureSession instance. error: ${JSON.stringify(err)}`); - } - return captureSession; -} - -/** - * 调用captureSession类中的beginConfig()方法配置会话 - * @param captureSession - */ -export function beginConfig(captureSession: camera.CaptureSession): void { +export function getCameraManager(context: common.BaseContext): camera.CameraManager | undefined { + let cameraManager: camera.CameraManager | undefined = undefined; try { - captureSession.beginConfig(); + cameraManager = camera.getCameraManager(context); } catch (error) { let err = error as BusinessError; - console.error(`[camera test] Failed to beginConfig. error: ${JSON.stringify(err)}`); + Log.e(TAG, `The getCameraManager call failed. error code: ${err.code}`); } + return cameraManager; } -/** - * 调用captureSession类中的commitConfig()和start()方法提交相关配置,并启动会话 - * @param captureSession - * @returns - */ -export async function startSession(captureSession: camera.CaptureSession): Promise { - try { - await captureSession.commitConfig(); - } catch (error) { - let err = error as BusinessError; - console.error(`[camera test] Failed to commitConfig. error: ${JSON.stringify(err)}`); - } - - try { - await captureSession.start() - } catch (error) { - let err = error as BusinessError; - console.error(`[camera test] Failed to start. error: ${JSON.stringify(err)}`); - } -} - -export function getCameraInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput | null{ - // 创建相机输入流 - let cameraInput: camera.CameraInput | null = null; - try { - cameraInput = cameraManager.createCameraInput(cameraDevice); - // 监听cameraInput错误信息 - cameraInput.on('error', cameraDevice, (error: BusinessError) => { - console.info(`[camera test] Camera input error code: ${error.code}`); - }); - } catch (error) { - let err = error as BusinessError; - console.error('[camera test] Failed to createCameraInput errorCode = ' + err.code); - } - - return cameraInput; -} - -/** - * 通过getSupportedOutputCapability()方法,获取当前设备支持的所有输出流 - * 输出流在CameraOutputCapability中的各个profile字段中 - * @param cameraDevice - * @param cameraManager - * @returns - */ -export async function getSupportedOutputCapability(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager, - cameraInput: camera.CameraInput): Promise { - // 打开相机 - await cameraInput.open(); - // 获取相机设备支持的输出流能力 - let cameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice); - if (!cameraOutputCapability) { - console.error("[camera test] cameraManager.getSupportedOutputCapability error"); - return undefined; - } - console.info("[camera test] outputCapability: " + JSON.stringify(cameraOutputCapability)); - return cameraOutputCapability; -} - -/** - * 获取预览流 - * @param cameraManager - * @param cameraOutputCapability - * @param surfaceId - * @returns - */ -export function getPreviewOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability, - surfaceId: string): camera.PreviewOutput | undefined { - let previewProfilesArray: Array = cameraOutputCapability.previewProfiles; - let previewOutput: camera.PreviewOutput | undefined = undefined; - try { - previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId); - } catch (error) { - let err = error as BusinessError; - console.error("[camera test] Failed to create the PreviewOutput instance. error code: " + err.code); - } - return previewOutput; -} - -export function setSessionCameraInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void{ +export function createSession(cameraManager: camera.CameraManager, mode: camera.SceneMode): camera.PhotoSession | undefined { + let photoSession: camera.PhotoSession | undefined = undefined; try { - captureSession.addInput(cameraInput); + photoSession = cameraManager.createSession(mode) as camera.PhotoSession; } catch (error) { + // 失败返回错误码error.code并处理 let err = error as BusinessError; - console.error(`[camera test] Failed to addInput. error: ${JSON.stringify(err)}`); + Log.e(TAG, `createCaptureSession error. error code: ${err.code}`); } + return photoSession; } -export function setSessionPreviewOutput(captureSession: camera.CaptureSession, previewOutput: camera.PreviewOutput): void{ +export function hasFlashUnit(context: common.BaseContext):boolean { + let status: boolean = true; + /* try { - captureSession.addOutput(previewOutput); + let cameraManager = getCameraManager(context)!; + let photoSession = createSession(cameraManager, camera.SceneMode.NORMAL_PHOTO)!; + photoSession.beginConfig(); + status = photoSession.hasFlash(); } catch (error) { + // 失败返回错误码error.code并处理 let err = error as BusinessError; - console.error(`[camera test] Failed to add previewOutput. error: ${JSON.stringify(err)}`); + Log.e(TAG, `The hasFlash call failed. error code: ${err.code}`); } + */ + return status; } diff --git a/ohos/src/main/ets/MobileScannerPlugin.ets b/ohos/src/main/ets/MobileScannerPlugin.ets index d4100236bd220d1ab1516b64d5cf9564b39e8f89..15f2009a611fc72d3607899a3709a4ac3da2f6a6 100644 --- a/ohos/src/main/ets/MobileScannerPlugin.ets +++ b/ohos/src/main/ets/MobileScannerPlugin.ets @@ -33,6 +33,8 @@ import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; import { display } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { Point } from '@ohos.UiTest'; +import { hasFlashUnit, getCameraManager} from './CameraUtil'; +import { image } from '@kit.ImageKit'; const TAG: string = "mobile_scanner"; @@ -174,6 +176,7 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab if (!this.isStart) { this.isStart = true; + let torchable:boolean = hasFlashUnit(this.applicationContext!) let scanTypes = [scanCore.ScanType.ALL] if (formats) { @@ -204,6 +207,17 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab this.setDisplay() this.startScan(cameraWidth, cameraHeight) + //customScan.on('lightingFlash', this.torchCallback); + + if (torch) { + customScan.openFlashLight() + } else { + customScan.closeFlashLight() + } + this.publishEvent({ + name: 'torchState', + data: customScan.getFlashLightStatus() ? TorchState.on : TorchState.off + }); result.success({ "textureId": this.textureId, @@ -211,7 +225,7 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab "width": cameraWidth, "height": cameraHeight }, - "torchable": torch, + "torchable": torchable, "numberOfCameras": 1 }); @@ -222,7 +236,6 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab null ) } - } // 返回自定义扫描结果的回调 @@ -431,4 +444,16 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab "data": error }) } + + torchCallback(error: BusinessError, bool: boolean) { + if (error) { + Log.e(TAG, `Failed to cancel Flash by callback. Code: ${error.code}, message: ${error.message}`); + return; + } + + this.publishEvent({ + name: 'torchState', + data: customScan.getFlashLightStatus() ? TorchState.on : TorchState.off + }); + } } \ No newline at end of file