Search code examples
angularangular7

Saving an image (retrieved from external library) locally - Angular?


I'm really really new to Angular and all. So I've gotten a QR code image from a library (angularx-qrcode) generator.

Here's the code to get the image:

     <qrcode [qrdata]="'Your QR code data string'" [size]="256" [level]="'M'"></qrcode> 

Now I wanna have a button that allows the user to save the above image locally. How can I achieve this?

Also is the syntax different for different Angular versions (e.g. 2 vs 7)?

Thank you so much!! any help would be appreciated:)


Solution

  • So, you want to download the Qr code image into your local device, check this stackblitz.

    This is my approach:

    • First, you need to get the base64 image data from the generated image
    • Convert the base 64 encoded image into blob data
    • Add a button to download the image

    Your component.html can be something like this:~

    <qrcode #parent [qrdata]="qrdata" [size]="256" [level]="'M'"></qrcode>
    <br>
    <button (click)="saveAsImage(parent)">Download QR Code Image</button>
    

    Your component.ts can be something like this:~

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      qrdata = 'Initial QR code data string';
    
      saveAsImage(parent) {
        // fetches base 64 date from image
        const parentElement = parent.el.nativeElement.querySelector("img").src;
    
        // converts base 64 encoded image to blobData
        let blobData = this.convertBase64ToBlob(parentElement);
    
        // saves as image
        if (window.navigator && window.navigator.msSaveOrOpenBlob) { //IE
          window.navigator.msSaveOrOpenBlob(blobData, 'Qrcode');
        } else { // chrome
          const blob = new Blob([blobData], { type: "image/png" });
          const url = window.URL.createObjectURL(blob);
          // window.open(url);
          const link = document.createElement('a');
          link.href = url;
          link.download = 'Qrcode';
          link.click();
        }
    
      }
    
      private convertBase64ToBlob(Base64Image: any) {
        // SPLIT INTO TWO PARTS
        const parts = Base64Image.split(';base64,');
        // HOLD THE CONTENT TYPE
        const imageType = parts[0].split(':')[1];
        // DECODE BASE64 STRING
        const decodedData = window.atob(parts[1]);
        // CREATE UNIT8ARRAY OF SIZE SAME AS ROW DATA LENGTH
        const uInt8Array = new Uint8Array(decodedData.length);
        // INSERT ALL CHARACTER CODE INTO UINT8ARRAY
        for (let i = 0; i < decodedData.length; ++i) {
          uInt8Array[i] = decodedData.charCodeAt(i);
        }
        // RETURN BLOB IMAGE AFTER CONVERSION
        return new Blob([uInt8Array], { type: imageType });
      }
    
    }
    

    (For the function convertBase64ToBlob, credits to Kaushik Parmar)

    Also, if you want to save the text inside the QRCode check this other stackblitz.