自定义相机

build() {
    Stack() {
        if(this.userGrant){
            Column(){
                XComponent({
                    id: 'componentId',
                    type: XComponentType.SURFACE,
                    controller: this.mXComponentController
                }).onLoad(async ()=>{
                    // 获取XComponent组件的surfaceId
                    this.surfaceId = this.mXComponentController.getXComponentSurfaceId();
                    hilog.info(0x0001, TAG, `Succeeded in getting surfaceId: ${this.surfaceId}`);
                    this.initCamera();
                    // 闪光灯监听接口
                    this.lightingFlashHandler = (error, isLightingFlash) => {
                        if (error) {
                            hilog.error(0x0001, TAG,
                                `Failed to on lightingFlash. Code: ${error.code}, message: ${error.message}`);
                            return;
                        }
                        if (isLightingFlash) {
                            this.isFlashLightEnable = true;
                        } else {
                            try {
                                if (!customScan.getFlashLightStatus()) {
                                    this.isFlashLightEnable = false;
                                }
                            } catch (error) {
                                hilog.error(0x0001, TAG,
                                    `Failed to get FlashLightStatus. Code: ${error.code}, message: ${error.message}`);
                            }
                            this.isSensorLight = isLightingFlash;
                        }
                    }
                    customScan.on('lightingFlash',this.lightingFlashHandler)
                }).backgroundColor($r('app.color.transparent'))
                    .position({ x: 0, y: 0 })
            }
        }

        Column() {
            if(this.isShowBack==false){
                if(this.iscount==false){
                    Text(this.texts[this.count] )
                        .fontColor(Color.White)
                        .fontSize(13)
                }else if(this.iscount==true){
                    Column(){
                        Text('未识别到条码?点击手动输入')
                            .fontSize(13)
                            .fontColor($r('app.color.white'))
                            .textAlign(TextAlign.Center)
                            .lineHeight(20)
                    }
                    .width(210)
                    .height(25)
                    .backgroundColor($r('app.color.gray_light'))
                    .borderRadius({
                        topLeft:15,
                        topRight:15,
                        bottomLeft:15,
                        bottomRight:15
                    })
                }
            }
        }
        .margin({
            top:430
        })

        Column(){
            Image($r('app.media.smb_img'))
                .width(120)
                .margin({
                    bottom:10
                })

            Text('条形码样式示例')
                .fontSize(13)
                .fontColor($r('app.color.gray_semi_dark'))
                .zIndex(1)
        }
        .margin({
            top:600
        })

        // 单码、多码扫描后,显示码图蓝点位置。点击toast码图信息
        ForEach(this.scanResult, (item: scanBarcode.ScanResult, index: number) => {
            if (item.scanCodeRect) {
                Text(item.originalValue)
                    .margin({
                        top:440
                    })
                // .onClick(()=>{
                    //     promptAction.showToast({
                    //         message:`Scan result: ${item.originalValue}`
                    //     })
                    // })
            }
        })

        this.TopTool()

        RelativeContainer() {

            Row()
                .width("100%")
                .height(this.scanRectMarginTop * px2vp(this.screenHeight))
                .backgroundColor($r('app.color.gray_dark'))
                .alignRules({
                    top: { anchor: '__container__', align: VerticalAlign.Top },
                    left: { anchor: '__container__', align: HorizontalAlign.Start }
                })
                .id("topRect")

            Row()
                .width(((1 - this.scanRectSize) / 2.0) * px2vp(this.screenWidth))
                .height(this.scanRectSize * px2vp(this.screenWidth)-0.5)
                .backgroundColor($r('app.color.gray_dark'))
                .alignRules({
                    top: { anchor: 'topRect', align: VerticalAlign.Bottom },
                    left: { anchor: 'topRect', align: HorizontalAlign.Start }
                })
                .id('leftRect')

            Row()
                .width(((1 - this.scanRectSize) / 2.0) * px2vp(this.screenWidth)-1)
                .height(this.scanRectSize * px2vp(this.screenWidth)-0.5)
                .backgroundColor($r('app.color.gray_dark'))
                .alignRules({
                    top: { anchor: 'topRect', align: VerticalAlign.Bottom },
                    right: { anchor: 'topRect', align: HorizontalAlign.End }
                })
                .id('rightRect')

            Row()
                .width("100%")
                .height("100%")
                .backgroundColor($r('app.color.gray_dark'))
                .alignRules({
                    top: { anchor: 'topRect', align: VerticalAlign.Bottom },
                    left: { anchor: 'topRect', align: HorizontalAlign.Start }
                })
                .margin({ top: this.scanRectSize * px2vp(this.screenWidth) })
                .id("bottomRect")


            ScanTopLeftRect().alignRules({
                top: { anchor: 'topRect', align: VerticalAlign.Bottom },
                left: { anchor: 'leftRect', align: HorizontalAlign.End }
            }).id('scanTopLeftRect')

            ScanTopRightRect().alignRules({
                top: { anchor: 'topRect', align: VerticalAlign.Bottom },
                right: { anchor: 'rightRect', align: HorizontalAlign.Start }
            }).id('scanTopRightRect')

            ScanBottomLeftRect().alignRules({
                bottom: { anchor: 'bottomRect', align: VerticalAlign.Top },
                left: { anchor: 'leftRect', align: HorizontalAlign.End }
            }).id('ScanBottomRightRect')

            ScanBottomRightRect().alignRules({
                bottom: { anchor: 'bottomRect', align: VerticalAlign.Top },
                right: { anchor: 'rightRect', align: HorizontalAlign.Start }
            }).id('scanBottomRightRect')


            Column() {
                Image($r('app.media.imgicon'))
                    .width(20)
                    .height(20)

                Text(this.isFlashLightEnable ? "轻触关灯" : "轻触照亮")
                    .margin({
                        top: 4
                    })
                    .fontColor($r('app.color.white'))
                    .fontSize(10)
            }.alignRules({
                top: { anchor: 'topRect', align: VerticalAlign.Bottom },
                middle: { anchor: '__container__', align: HorizontalAlign.Center },
            })
            .margin({
                top: 0.78 * this.scanRectSize * px2vp(this.screenWidth),
            })
            .onClick(() => {
                this.isFlashLightEnable = !this.isFlashLightEnable;
                let lightStatus: boolean = false;
                try {
                    lightStatus = customScan.getFlashLightStatus();
                } catch (error) {
                    hilog.error(0x0001, TAG,
                        `Failed to get flashLightStatus. Code: ${error.code}, message: ${error.message}`);
                }


                // 根据当前闪光灯状态,选择打开或关闭闪关灯
                if (lightStatus) {
                    try {
                        customScan.closeFlashLight();
                        setTimeout(() => {
                            this.isFlashLightEnable = this.isSensorLight;
                        }, 200);
                    } catch (error) {
                        hilog.error(0x0001, TAG,
                            `Failed to close flashLight. Code: ${error.code}, message: ${error.message}`);
                    }
                } else {
                    try {
                        customScan.openFlashLight();
                    } catch (error) {
                        hilog.error(0x0001, TAG,
                            `Failed to open flashLight. Code: ${error.code}, message: ${error.message}`);
                    }
                }
            })

        }.backgroundColor(Color.Transparent)
    }.alignContent(Alignment.Top)

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值