QR Code generator doesn't work properly

Hello guys,

Newbie with ionic 2, I try without success to make qr code app. I know there is already many posts about that but I don’t find solutions for my issues. What I want? Generate, display and download a qr code and in the other way read a qr code. I’m using cordova plugin barcodescanner. I include { BarcodeScanner } from ‘ionic-native’. I’ve also read the documentation about that but not so much information or example for usage. I managed to read a barcode implementating method scan() but when I use encode(), my app crashes. I don’t understand why. See my code:
In home.ts file:

import { Component } from ‘@angular/core’;
import { NavController } from ‘ionic-angular’;
import { BarcodeScanner } from ‘ionic-native’;

@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {
private urlImage: any;

constructor(public navCtrl: NavController) {

}

// For reading barcode, work properly
click() {
BarcodeScanner.scan().then((barcodeData) => {
alert("Text: " + barcodeData.text + “\n” + "Format: " + barcodeData.format);
}, (err) => {
alert(err.message);
});
}

// For generating qr code, when I click on it, it display qr code then my app crashes
** make() {**
** BarcodeScanner.encode (BarcodeScanner.Encode.TEXT_TYPE, ‘Hello World’)**
** .then ((success) => {**
** this.urlImage = success.file;**
** //alert(this.urlImage);**
** }), (error) => {**
** alert (error);**
** }**
** }**
}

And this is home.html file:

<ion-header>
   <ion-navbar>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>
<ion-content padding>
  <h2>Welcome to Ionic!</h2>
  <p>
    This starter project comes with simple tabs-based layout for apps
    that are going to primarily use a Tabbed UI.
  </p>
  <p>
    Take a look at the <code>src/pages/</code> directory to add or change tabs,
    update any existing page or create new pages.
  </p>
  <button ion-button block (click)="click()">Scan</button>
  <button ion-button block (click)="make()">Make</button>
// I've put an image tag before, but there is no display
</ion-content>

I’ve seen some topics talk about angular-qrcode but I don’t know how to use it properly in ionic 2. So how and where I can retrieve and display (btw download it) my qr code generated by make function
Please anyone help me, thanks guys.

Which device are you testing on? From the documentation:

Encodes data into a barcode. NOTE: not well supported on Android

Android! Indeed I read this line.

So, how can I do to generate a qr code? I see plenty external libraries but I don’t know how to include them. Examples or links will be very appreciated!

I have used neither yet, but you can look into:

https://www.npmjs.com/package/ng2-qrcode

and

https://www.npmjs.com/package/angular2-qrcode

They have respective examples on their npm-profilepage.

I think what happens to you is that in the line

BarcodeScanner.encode (BarcodeScanner.Encode.TEXTTYPE, 'Hello World') you forgot the _ between text and type. It shoud be TEXT_TYPE.

Also, the way of displaying the QR code is <img src="{{urlImage}}">.

Hope it works.

I try that. Unfornutaly, I’ve always a crash. Look at my code in home.ts:
make function is a bouton which active encode method.
export class HomePage {
private urlImage: any;
constructor (navCtrl: NavController) {
this.urlImage = ’ ';
}
make() {
BarcodeScanner.encode (BarcodeScanner.Encode.TEXT_TYPE, ‘Hello World’)
.then ((success) => {
this.urlImage = success.file; // I think the problem is here
//alert("Encoding success: " + success);
}), (error) => {
alert (error);
}
}
}

See what I’ve done in home.html:

<button ion-button block (click)="click()">Scan</button>
<button ion-button block (click)="make()">Make</button>
<img src="{{urlImage}}">

What do you think of this?

I tried angular2-qrcode, work fine, thanks! I try now to make it dynamically. Maybe I’m doing wrong but look at my code please:
home.ts:
export class HomePage {
private textField: any;
private textEncode: any;
private urlImage: any;

constructor(public navCtrl: NavController) {
this.textField = ‘’;
this.textEncode = ‘’;
this.urlImage = ‘’;
}

// For reading barcode, work properly
click() {
BarcodeScanner.scan().then((barcodeData) => {
alert("Text: " + barcodeData.text + “\n” + "Format: " + barcodeData.format);
}, (err) => {
alert(err.message);
});
}

// For generating qr code, when I click on it, it display qr code then my app crashes
make() {
this.textEncode = this.textField; // Put input value named textField into textCode
}

In home.html:
<ion-item>
<ion-label>Entrez votre texte</ion-label>
<ion-input type="text" [(ngModel)]="textField"></ion-input>
</ion-item>
<button ion-button block (click)="click()">Scan</button>
<button ion-button block (click)="make()">Make</button>
<p>{{textEncode}}</p> If I delete div tag with qr-code tag, tag p display correctly input value but when I use qr-code tag, I see the white screen death!!! Don’t understand why
<div>
<qr-code [data]="'{{textEncode}}'" [size]="250"></qr-code>
</div>

The following works for me:

qrfood: string;

<ion-item>
  <ion-input type="text" [(ngModel)]="qrfood"></ion-input>
</ion-item>

<qr-code [data]="qrfood" size="250"><qr-code>

Note: I did incorporate this PR, not sure if that matters.

1 Like

Thanks for your reply, I did like that and it works! Very thankful for your help everyone. Now my new challenge is to print qr code I generate. I use the ionic native printer but in my device “Android”, it doesn’t work. Always looking solutions. Thanks again. I’ll post my solution about generate qr code for anyone who can have same problem.

I’m a little lost here with the error: Can’t bind to ‘data’ since it isn’t a known property of ‘qr-code’.

I’ve added angular2-qrcode to my project

npm install angular2-qrcode

In src > app > app.module.ts, I added

import { QRCodeModule } from 'angular2-qrcode';
...
imports: [
    IonicModule.forRoot(MyApp),
    QRCodeModule
  ],

In src > pages > home > home.ts, I added…

import {OnInit} from '@angular/core';
import {QRCodeComponent} from 'angular2-qrcode';
...
public qrfood: string;    // in the HomePage class

And in src > pages > home > home.html, I added…

<ion-item>
    hello: <ion-input type="text" [(ngModel)]="qrfood"></ion-input>
</ion-item>

<qr-code [data]="qrfood" size="250"></qr-code>

Any help is appreciated!

Thanks,
Ryan

Did you add the NgModule fix linked in my earlier post?

Im not sure what I have to do to the node_modules > angular2-qrcode > angular2-qrcode.d.ts file.

You need to decorate QRCodeModule with NgModule().

Sorry, I don’t know what exactly that means.

I don’t know how I can explain it more definitively than the diff in the PR.

When I add

@NgModule()

in front of

export declare class QRCodeModule {
}

I get the error

Typescript Error
Cannot find name ‘NgModule’.

So I figured there was something else I was missing.

Perhaps importing NgModule from @angular/core in that file?

Added that now. It’s moved me on to the new error:

Unhandled Promise rejection: Template parse errors:
Can’t bind to ‘data’ since it isn’t a known property of ‘qr-code’.

…so I completely swapped out the angular2-qrcode.d.ts file contents with this

But I still get the data error.

Try modifying your @NgModule on the QRCodeModuledeclaration to look like this:

@NgModule({
    exports: [QRCodeComponent],
    declarations: [QRCodeComponent],
    entryComponents: [QRCodeComponent]
})

That’s what I have in the scratch project I was using to test this originally. Also decorate QRCodeComponent thusly:

@Component({
    moduleId: 'module.id',
    selector: 'qr-code',
    template: ''
})