question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Embedding in NativeScript-Vue

See original GitHub issue

I’ve got Barcode Scanner working with NativeScript-Vue by calling scan() function, but I can’t figure out how to use the embed feature, it keeps returning Frame errors. This is what I’m doing:

<template>
  <Page class="page">
    <ActionBar class="action-bar" title="Home"/>
    <StackLayout>
      <Button class="btn btn-primary" @tap="onScan">Scan</Button>
      <BarcodeScanner
		class="scanner-round"
		formats="QR_CODE, EAN_13"
		beepOnScan="true"
		reportDuplicates="true"
		preferFrontCamera="false"
		@scanResult="onScan">
	</BarcodeScanner>
    </StackLayout>
  </Page>
</template>

<script>
const BarcodeScanner = require("nativescript-barcodescanner").BarcodeScanner;

export default {
	components: {
		"BarcodeScanner": require("nativescript-barcodescanner").BarcodeScannerView
	},		
	data () {
		return {
			scanner: null
		};
	},		
	async created() {
		this.scanner = new BarcodeScanner();
	},
	methods: {
		onScan() {
			this.scanner.scan({
				formats: "QR_CODE, EAN_13",
				showFlipCameraButton: true,   
				preferFrontCamera: false,     
				showTorchButton: true,        
				beepOnScan: true,             
				torchOn: false,               
				resultDisplayDuration: 500,   
				openSettingsIfPermissionWasPreviouslyDenied: true //ios only 
			}).then((result) => {
				console.log({
					title: "You Scanned ",
					message: "Format: " + result.format + ",\nContent: " + result.text,
					okButtonText: "OK"
				});
			}, (errorMessage) => {
				console.log("Error when scanning " + errorMessage);
			});
		},
		scanResult(result) {
			console.log(`Format: ${ result.format }, Value: ${ result.text }`);
		}
	}
};
</script>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
JarriddWcommented, Apr 9, 2019

I Struggled with this for a while as well.

The problem I found was that people are trying to embed when this will not work at all with android. Instead you MUST use a method call if you want this to work with android(and iOS). Functionality remains the same.

I will say that the documentation confused me to where I gave up and had to come back at a later stage, then realizing how silly I was in all my frustration… I did however get it to work! (after reading through the documentation several times)

This is how(using NativeScript-Vue):

In your: _ app.js/main.js_ Vue.registerElement('BarcodeScanner', () => require('nativescript-barcodescanner').BarcodeScannerView)

In your page.vue

(may contain some unnecessary code within elements and camera is imported because I am using this elsewhere in my app as well and have used its permission request in my solution)

<template>
    <Page class="page">

        <ActionBar class="action-bar">
                <label text="Test" />
        </ActionBar>

        <ScrollView>
            <StackLayout ~mainContent>
                <StackLayout class="home-panel">

                    <Button class="btn btn-outline" text="Scan QR code" @tap="scanQrCode"/>

                    <Label class="h4 text-center m-t-15" text="Scan Result Text: " />

                    <TextView class="h4 text-center" width="81%" editable="false" :text="scanText"/>

                </StackLayout>
            </StackLayout>
        </ScrollView>
    </Page>
</template>

<script>
    import * as Camera from "nativescript-camera";

    const BarcodeScanner = require("nativescript-barcodescanner").BarcodeScanner;
    var barcodescanner = new BarcodeScanner();

    export default {

        data() {
            return {
                mainContentText: "",
                scanText: null
            };
        },

        methods: {

            scanQrCode() {

                Camera.requestCameraPermissions()
                    .then(() => {

                        barcodescanner.scan({
                            formats: "QR_CODE",
                            cancelLabel: "Close",
                            cancelLabelBackgroundColor: "#FFFFFF",
                            message: "Use the volume buttons for extra light",
                            showFlipCameraButton: false,
                            preferFrontCamera: false,
                            showTorchButton: true,
                            beepOnScan: true,
                            torchOn: false,
                            closeCallback: function () { console.log("Scanner closed"); },
                            resultDisplayDuration: 500,
                            orientation: "portrait",
                            openSettingsIfPermissionWasPreviouslyDenied: true
                        }).then((result) => {
                            console.log("Scan format : " + result.format);
                            console.log("Scan text :   " + result.text);

                            this.scanText = result.text;
                        },
                            (error) => {
                                console.log("No scan: " + error);
                            }
                        );

                    }).catch(e => { console.log('Error requesting permission'); });
            },
        },
    };
</script>

The above code is what allowed me to eventually get the barcode scanner working in my own NS-Vue app.

My mistake was including the embedding Code from the docs as seen below:

(Do NOT include this)

<BarcodeScanner
    row="1"
    height="300"
    formats="QR_CODE, EAN_13, UPC_A"
    beepOnScan="true"
    reportDuplicates="true"
    preferFrontCamera="false"
    @scanResult="onScanResult"
    v-if="isIOS">
</BarcodeScanner>

I hope this helps someone get the nativescript-barcodescanner working on their NS-Vue app!

1reaction
mdiaz00147commented, Feb 13, 2020

I Struggled with this for a while as well.

The problem I found was that people are trying to embed when this will not work at all with android. Instead you MUST use a method call if you want this to work with android(and iOS). Functionality remains the same.

I will say that the documentation confused me to where I gave up and had to come back at a later stage, then realizing how silly I was in all my frustration… I did however get it to work! (after reading through the documentation several times)

This is how(using NativeScript-Vue):

In your: _ app.js/main.js_ Vue.registerElement('BarcodeScanner', () => require('nativescript-barcodescanner').BarcodeScannerView)

In your page.vue

(may contain some unnecessary code within elements and camera is imported because I am using this elsewhere in my app as well and have used its permission request in my solution)

<template>
    <Page class="page">

        <ActionBar class="action-bar">
                <label text="Test" />
        </ActionBar>

        <ScrollView>
            <StackLayout ~mainContent>
                <StackLayout class="home-panel">

                    <Button class="btn btn-outline" text="Scan QR code" @tap="scanQrCode"/>

                    <Label class="h4 text-center m-t-15" text="Scan Result Text: " />

                    <TextView class="h4 text-center" width="81%" editable="false" :text="scanText"/>

                </StackLayout>
            </StackLayout>
        </ScrollView>
    </Page>
</template>

<script>
    import * as Camera from "nativescript-camera";

    const BarcodeScanner = require("nativescript-barcodescanner").BarcodeScanner;
    var barcodescanner = new BarcodeScanner();

    export default {

        data() {
            return {
                mainContentText: "",
                scanText: null
            };
        },

        methods: {

            scanQrCode() {

                Camera.requestCameraPermissions()
                    .then(() => {

                        barcodescanner.scan({
                            formats: "QR_CODE",
                            cancelLabel: "Close",
                            cancelLabelBackgroundColor: "#FFFFFF",
                            message: "Use the volume buttons for extra light",
                            showFlipCameraButton: false,
                            preferFrontCamera: false,
                            showTorchButton: true,
                            beepOnScan: true,
                            torchOn: false,
                            closeCallback: function () { console.log("Scanner closed"); },
                            resultDisplayDuration: 500,
                            orientation: "portrait",
                            openSettingsIfPermissionWasPreviouslyDenied: true
                        }).then((result) => {
                            console.log("Scan format : " + result.format);
                            console.log("Scan text :   " + result.text);

                            this.scanText = result.text;
                        },
                            (error) => {
                                console.log("No scan: " + error);
                            }
                        );

                    }).catch(e => { console.log('Error requesting permission'); });
            },
        },
    };
</script>

The above code is what allowed me to eventually get the barcode scanner working in my own NS-Vue app.

My mistake was including the embedding Code from the docs as seen below:

(Do NOT include this)

<BarcodeScanner
    row="1"
    height="300"
    formats="QR_CODE, EAN_13, UPC_A"
    beepOnScan="true"
    reportDuplicates="true"
    preferFrontCamera="false"
    @scanResult="onScanResult"
    v-if="isIOS">
</BarcodeScanner>

I hope this helps someone get the nativescript-barcodescanner working on their NS-Vue app!

Definitively it works. The only thing is that it won’t work on the playground app, you must run it in a real device, since nativescript preview app does not ships with all the required plugins.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RadListView Overview
RadListView for NativeScript is a virtualizing list component that provides the most popular features associated with scenarios where a list of items is ......
Read more >
What is proper way of starting a new nativescript-vue project ...
I want to be able to use Typescript inside Vue instance methods, as documented in blog page of nativescript-vue.org.
Read more >
Persisting data between app launches with NativeScript Vue
import Vue from 'nativescript-vue'; import Vuex from 'vuex'; Vue.use(Vuex); const state = { token: false } const mutations = { setUser(state, ...
Read more >
Introduction - NativeScript-Vue
NativeScript -Vue is a NativeScript plugin which allows you to use Vue.js to craft your mobile application. If you have used Vue.js before...
Read more >
Develop Mobile Apps with NativeScript and VueJs - Littlelines
There are a few options available to us to use Vue to build mobile apps. One option is a framework called Vue Native....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found