美文网首页
01-uniapp扫描二维码&&自定义扫码界面

01-uniapp扫描二维码&&自定义扫码界面

作者: 零涂 | 来源:发表于2023-07-18 15:31 被阅读0次

1.扫描二维码

原文地址:https://blog.csdn.net/u011577355/article/details/123015237

<template>
    <view class="content">
        <view class="btn">
            <button type="default" @click="scan">扫描</button>
        </view>
        <view class="admin">
            <view>{{msg}}</view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                msg: ""
            }
        },
        methods: {
            scan() {
                uni.scanCode({
                    success: (res) => {
                        console.log(res);
                        this.msg = res.result
                    }
                })
            }
        }
    }
</script>

<style>

</style>

2.自定义扫码界面

原文地址:https://blog.csdn.net/qq_33165549/article/details/89879435

<template>
    <view>
    </view>
</template>
<script>
    var barcode = null;
    export default {
        data() {
            return {
                name: '', //要在扫码界面自定义的内容
                flash: false, //是否打开摄像头
                type: ''

            };
        },
        onLoad(d) {
            var n = d.text;
            this.type = d.type;
            if (n) {
                this.name = n;
            }
            var pages = getCurrentPages();
            var page = pages[pages.length - 1];
            // #ifdef APP-PLUS
            plus.navigator.setFullscreen(true); //全屏
            var currentWebview = page.$getAppWebview();
            this.createBarcode(currentWebview); //创建二维码窗口
            this.createView(currentWebview); //创建操作按钮及tips界面
            // #endif
        },
        methods: {
            // 扫码成功回调
            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();
            },
            // 创建二维码窗口
            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();
            },
            // 创建操作按钮及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)

            }
        },
        onBackPress() {
            // #ifdef APP-PLUS
            // 返回时退出全屏
            barcode.close();
            plus.navigator.setFullscreen(false);
            // #endif
        },
        onUnload() {
            plus.navigator.setFullscreen(false);
        }


    };
</script>

<style scoped>
</style>

相关文章

  • ZBar

    iOS中使用ZBar扫描二维码自定义扫描界面

  • Zxing扫描二维码和识别二维码图片

    需求说明 前面几篇文章讲述了Zxing自定义扫描界面,但是没有说明如何扫描二维码和识别二维码图片,通过这篇文章希望...

  • Axure学习笔记:TIM扫描

    TIM的扫一扫功能,主要用于扫描对方二维码添加好友,其中在这界面也提供了读取相册二维码功能,以及显示用户的二维码的...

  • Android二维码开发(二)优化

    [TOC] 前言 上一篇,扫码的基本功能已经实现,不过还存在一些问题 扫码界面不是我们常见的二维码扫描界面 方法调...

  • 自定义扫描二维码界面

    由于项目需求,程序需要实现自定义界面二维码扫描功能,所以我找了一下系统二维码扫描的方法。 下面是我封装代码:.h里...

  • 二维码扫描

    二维码扫描库 最全最稳定的二维码扫描库 (有so源码)Zbar源码 ZBar扫码(只有扫码,无相册选取图片解码...

  • ios-深度解析二维码的生成与使用

    前言:利用一个小demo来对二维码进行学习,总共四个界面(主界面,生成二维码界面,识别二维码界面,扫描二维码界面)...

  • Xamarin.Forms 二维码

    目录 - Xamarin.Forms 前言 本文介绍二维码:一、扫描二维码二、自定义扫描二维码二、生成二维码 环境...

  • 04-一分钟集成扫描功能,支持二维码与条形码

    基于ZXing库封装的扫一扫控件,一分钟让你的app拥有条形码,二维码扫描功能,还支持自定义界面以及闪光灯开关,赶...

  • 稚慧星使用说明

    稚慧星使用说明 下载安装请用微信或者手机的二维码扫码器扫描下发的二维码扫描二维码安装智慧星APP 扫描后,你将会进...

网友评论

      本文标题:01-uniapp扫描二维码&&自定义扫码界面

      本文链接:https://www.haomeiwen.com/subject/dhkgudtx.html