Intosoft / qrcode

The best tool for QR Code generation in JavaScript (React, React Native, Node.js, VueJS, Angular, JQuery, VanillaJS)
https://custoqr.com
MIT License
10 stars 1 forks source link

Save QR React native #34

Closed Djhitman closed 6 months ago

Djhitman commented 7 months ago

How I can get base64 string of qrcode in react native app?

sakul-budhathoki commented 7 months ago

@Djhitman you can try below code for it:

import RNFetchBlob from 'rn-fetch-blob';
import Share from 'react-native-share';

const downloadAndShareSvg = async (svgString) => {
  const dirs = RNFetchBlob.fs.dirs;
  const filePath = `${dirs.DocumentDir}/image.svg`;

  try {
    // Write the SVG string to a file
    await RNFetchBlob.fs.writeFile(filePath, svgString, 'utf8');
    console.log('SVG file saved:', filePath);

    // Share the downloaded file
    await Share.open({ url: `file://${filePath}` });
  } catch (error) {
    console.error('Error saving or sharing SVG file:', error);
  }
};
Djhitman commented 7 months ago

@Djhitman you can try below code for it:

import RNFetchBlob from 'rn-fetch-blob';
import Share from 'react-native-share';

const downloadAndShareSvg = async (svgString) => {
  const dirs = RNFetchBlob.fs.dirs;
  const filePath = `${dirs.DocumentDir}/image.svg`;

  try {
    // Write the SVG string to a file
    await RNFetchBlob.fs.writeFile(filePath, svgString, 'utf8');
    console.log('SVG file saved:', filePath);

    // Share the downloaded file
    await Share.open({ url: `file://${filePath}` });
  } catch (error) {
    console.error('Error saving or sharing SVG file:', error);
  }
};

But how i can get base64 string of qrcode? SvgFromXml component doesn't have a toDataURL function

sakul-budhathoki commented 7 months ago

@Djhitman you can try below code for it:

import RNFetchBlob from 'rn-fetch-blob';
import Share from 'react-native-share';

const downloadAndShareSvg = async (svgString) => {
  const dirs = RNFetchBlob.fs.dirs;
  const filePath = `${dirs.DocumentDir}/image.svg`;

  try {
    // Write the SVG string to a file
    await RNFetchBlob.fs.writeFile(filePath, svgString, 'utf8');
    console.log('SVG file saved:', filePath);

    // Share the downloaded file
    await Share.open({ url: `file://${filePath}` });
  } catch (error) {
    console.error('Error saving or sharing SVG file:', error);
  }
};

But how i can get base64 string of qrcode? SvgFromXml component doesn't have a toDataURL function

@Djhitman For this function downloadAndShareSvg we will send the svgString (not the base64 string)

const svgString = generateSVGString(config);
downloadAndShareSvg(svgString);

If you need base64 for other use cases then can try as below: npm install buffer const svgDataUri = `data:image/svg+xml;base64,${Buffer.from(svgString).toString('base64')}`;