uni-app 中应用H5+自定义二维码扫码界面

前段时间因项目需求,要在uni-app的扫码uni.scanCode(OBJECT),中并没有自定的参数或者接口,所以自己用H5+搞了个扫码界面,这里做一下记录,就算是一个轮子。

下面开始造轮子。

1,创建一个.vue的页面文件。因为我这需求是全屏的扫码,所以在pages.json 中设置去掉原生导航栏

uni-app 中应用H5+自定义二维码扫码界面_第1张图片

2,开始在页面中绘制界面

如图我们看到有内容,返回箭头,标题,提示文本信息,扫码框,打开手电动按钮等内容,

虽然看上去是有这5部分,但是我们可以认为是二维码一层,文字及操作按钮是一层,

A.二维码层创建

// 创建二维码窗口
            createBarcode(currentWebview) {
                barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
                    top: '0',
                    left: '0',
                    width: '100%',
                    height: '100%',
                    scanbarColor: '#1DA7FF',
                    position: 'static',
                    frameColor: '#1DA7FF'
                });
                barcode.onmarked = this.onmarked;
                barcode.setFlash(this.flash);
                currentWebview.append(barcode);
                barcode.start();
            }

// 扫码成功回调
            onmarked(type, result) {
                var text = '未知: ';
                switch (type) {
                    case plus.barcode.QR:
                        text = 'QR: ';
                        break;
                    case plus.barcode.EAN13:
                        text = 'EAN13: ';
                        break;
                    case plus.barcode.EAN8:
                        text = 'EAN8: ';
                        break;
                }
                plus.navigator.setFullscreen(false);
                uni.navigateBack({
                    delta: 1
                });
                this.$eventHub.$emit(this.type, {
                    result: result
                });
                barcode.close();
            }

// 创建操作按钮及tips
            createView(currentWebview) {
                // 创建返回原生按钮
                var backVew = new plus.nativeObj.View('backVew', {
                        top: '0px',
                        left: '0px',
                        height: '40px',
                        width: '100%'
                    },
                    [{
                        tag: 'img',
                        id: 'backBar',
                        src: 'static/backBar.png',
                        position: {
                            top: '2px',
                            left: '3px',
                            width: '35px',
                            height: '35px'
                        }
                    }]);
                // 创建打开手电筒的按钮
                var scanBarVew = new plus.nativeObj.View('scanBarVew', {
                        top: '60%',
                        left: '40%',
                        height: '10%',
                        width: '20%'

                    },
                    [{
                            tag: 'img',
                            id: 'scanBar',
                            src: 'static/scanBar.png',
                            position: {
                                width: '28%',
                                left: '36%',
                                height: '30%'
                            }
                        },
                        {
                            tag: 'font',
                            id: 'font',
                            text: '轻触照亮',
                            textStyles: {
                                size: '10px',
                                color: '#ffffff'
                            },
                            position: {
                                width: '80%',
                                left: '10%'
                            }
                        }
                    ]);
                // 创建展示类内容组件
                var content = new plus.nativeObj.View('content', {
                        top: '0px',
                        left: '0px',
                        height: '100%',
                        width: '100%'

                    },
                    [{
                            tag: 'font',
                            id: 'scanTitle',
                            text: '扫码',
                            textStyles: {
                                size: '18px',
                                color: '#ffffff'
                            },
                            position: {
                                top: '0px',
                                left: '0px',
                                width: '100%',
                                height: '40px'
                            }
                        },
                        {
                            tag: 'font',
                            id: 'scanTips',
                            text: this.name,
                            textStyles: {
                                size: '14px',
                                color: '#ffffff',
                                whiteSpace: 'normal'
                            },
                            position: {
                                top: '90px',
                                left: '10%',
                                width: '80%',
                                height: 'wrap_content'

                            }
                        }

                    ]);
                backVew.interceptTouchEvent(true);
                scanBarVew.interceptTouchEvent(true);
                currentWebview.append(content);
                currentWebview.append(scanBarVew);
                currentWebview.append(backVew);

backVew.addEventListener("click", function(e) { //返回按钮
                    uni.navigateBack({
                        delta: 1
                    });
                    barcode.close();
                    plus.navigator.setFullscreen(false);

                }, false);

    var temp = this;
                scanBarVew.addEventListener("click", function(e) { //点亮手电筒
                    temp.flash = !temp.flash;
                    if (temp.flash) {
                        scanBarVew.draw([{
                                tag: 'img',
                                id: 'scanBar',
                                src: 'static/yellow-scanBar.png',
                                position: {
                                    width: '28%',
                                    left: '36%',
                                    height: '30%'
                                }
                            },
                            {
                                tag: 'font',
                                id: 'font',
                                text: '轻触照亮',
                                textStyles: {
                                    size: '10px',
                                    color: '#ffffff'
                                },
                                position: {
                                    width: '80%',
                                    left: '10%'
                                }
                            }
                        ]);
                    } else {
                        scanBarVew.draw([{
                                tag: 'img',
                                id: 'scanBar',
                                src: 'static/scanBar.png',
                                position: {
                                    width: '28%',
                                    left: '36%',
                                    height: '30%'
                                }
                            },
                            {
                                tag: 'font',
                                id: 'font',
                                text: '轻触照亮',
                                textStyles: {
                                    size: '10px',
                                    color: '#ffffff'
                                },
                                position: {
                                    width: '80%',
                                    left: '10%'
                                }
                            }
                        ])
                    }
                    if (barcode) {
                        barcode.setFlash(temp.flash);
                    }
                }, false)

}

将代码都贴出来看着比较繁琐,但是觉还是在这里记录一下,算是uni-app应用过程的学习笔记

项目代码

 

 

你可能感兴趣的:(uni-app,H5+,uni-app,自定义二维码扫码界面,H5+)