本地保存图像(从外部库检索)-Angular? [英] Saving an image (retrieved from external library) locally - Angular?

查看:86
本文介绍了本地保存图像(从外部库检索)-Angular?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的对Angular和所有其他人都是新手.因此,我从库(angularx-qrcode)生成器中获取了QR码图像.

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?

不同的Angular版本(例如2 vs 7)的语法也不同吗?

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

非常感谢!!任何帮助,将不胜感激:)

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

推荐答案

现在我要有一个按钮,允许用户保存上面的图像 本地.我该如何实现?


因此,您想将二维码图像下载到本地设备中

检查 工作堆栈

这是我的工作方式!

  • 首先,您需要从生成的图像中获取base64图像数据
  • 将基数为64的编码图像转换为blob数据
  • 添加按钮以下载图像
  • 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

您的component.html可能是这样的:〜

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>

您的component.ts可能是这样的:〜

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 });
  }

}


此外,如果您想将文本保存在QRCode中,请在下面选中以下一项:〜

检查 工作堆BLACKZ

这篇关于本地保存图像(从外部库检索)-Angular?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆