javascript - 如何在 native react 中保存/下载生成二维码

标签 javascript reactjs react-native react-redux qr-code

通过使用此代码,我可以生成 QRCode,但是,我不知道如何在长按或自动保存 png/jpeg 格式的 qrcode。 我需要一些帮助或想法来解决这个问题。我尝试了几个例子但没有成功。我一直在努力。

import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, TextInput, TouchableOpacity, Text,} from 'react-native';
// import all basic components
import QRCode from 'react-native-qrcode-svg';
//import QRCode

class App extends Component {
    svg;
  constructor() {
    super();
    this.state = {
      inputValue: '',
      inputValue2: '',
      // Default Value of the TextInput
      valueForQRCode: '',
      // Default value for the QR Code
    };
  }

  getTextInputValue = () => {
    // Function to get the value from input
    // and Setting the value to the QRCode
    this.setState({ valueForQRCode: this.state.inputValue + this.state.inputValue2 });
  };
  shareQR =()  =>{
    this.svg.toDataURL((data) => {
      const shareImageBase64 = {
        title: "QR",
        message: "Ehi, this is my QR code",
        url: `data:image/png;base64,${data}`
      };
      Share.open(shareImageBase64);
    });
  }
  render() {
    return (
      <View style={styles.MainContainer}>
        <QRCode
          value={"Abhigyan" + this.state.valueForQRCode}
          //Setting the value of QRCode
          size={250}
          //Size of QRCode
          bgColor="#000"
          //Backgroun Color of QRCode
          fgColor="#fff"
          //Front Color of QRCode
          getRef={(ref) => (this.svg = ref)}
          onPress={() =>{shareQR()}}
        />
        <TextInput
          // Input to get the value to set on QRCode
          style={styles.TextInputStyle}
          onChangeText={text => this.setState({ inputValue: text })}
          underlineColorAndroid="transparent"
          placeholder="Enter text to Generate QR Code"
        />

<TextInput
          // Input to get the value to set on QRCode
          style={styles.TextInputStyle}
          onChangeText={text => this.setState({ inputValue2: text })}
          underlineColorAndroid="transparent"
          placeholder="Enter text to Generate QR Code"
        />
        <TouchableOpacity
          onPress={this.getTextInputValue}
          activeOpacity={0.7}
          style={styles.button}>
          <Text style={styles.TextStyle}> Generate QR Code </Text>
        </TouchableOpacity>

        <TouchableOpacity style={styles.button}onPress={this.shareQR}>
          <Text style={styles.buttonText}>Share</Text>
      </TouchableOpacity>
      </View>
    );
  }
}
export default App;
const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    margin: 10,
    alignItems: 'center',
    paddingTop: 40,
  },

  TextInputStyle: {
    width: '100%',
    height: 40,
    marginTop: 20,
    borderWidth: 1,
    textAlign: 'center',
  },

  button: {
    width: '100%',
    paddingTop: 8,
    marginTop: 10,
    paddingBottom: 8,
    backgroundColor: '#F44336',
    marginBottom: 20,
  },

  TextStyle: {
    color: '#fff',
    textAlign: 'center',
    fontSize: 18,
  },
});

//谢谢。 //在 QRCode 中,我能够生成 QRCode 扫描器,我已经完成了,但是如何下载/保存或共享该 qrcode,请帮助

最佳答案

这个答案是指 react-native-qrcode-svg 库,如问题评论中所写

使用这个库,您可以创建一个 svg 来显示您想要的 QR,然后通过引用访问它。因此,在您的组件中创建一个引用:

class App extends Component {
  svg;
  constructor() {
    ...
    };
  }
...
}

为其分配二维码,例如:

<QRCode
  value={"Abhigyan" +this.state.valueForQRCode}
  size={250}
  color="#fff"
  getRef={(ref?) => (this.svg = ref)}
/>

现在您可以使用 this.svg.toDataURL(//callback) 访问其内容。 示例:您想通过单击调用此函数的按钮使用 react-native-share 将 QR 作为图像/png 共享:

  shareQR() {
    this.svg.toDataURL((data) => {
      const shareImageBase64 = {
        title: "QR",
        message: "Ehi, this is my QR code",
        url: `data:image/png;base64,${data}`
      };
      Share.open(shareImageBase64);
    });
  }

这只是一个例子,如果你更喜欢使用react-native-fs,你可以引用example在官方存储库中给出。

更新以支持 onPress 功能

您正尝试将 onPress 属性传递给 QRCode,但 QRCode 不支持它。相反,将其包装在 TouchableOpacity 中:

<TouchableOpacity onPress={this.shareQR}>
  <QRCode
    value={"Abhigyan" +this.state.valueForQRCode}
    size={250}
    color="#fff"
    getRef={(ref?) => (this.svg = ref)}
  />
</TouchableOpacity>

关于javascript - 如何在 native react 中保存/下载生成二维码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54665267/

相关文章:

javascript - 如果屏幕尺寸为移动尺寸,则使用 jquery 更改字体大小

javascript - 连接 html 元素后,结果变量返回 0

javascript - executeScript - 注入(inject)提示和返回值 - 找不到值字段

reactjs - 使用 utility-reactstrap-validation 根据两个输入进行验证

reactjs - 有没有办法检查JS线程是否繁忙?

android - React-Native 替代 AlertIOS.prompt for android?

android - react native Android : Generating Signed APK through Android Studio doesn't include JS Bundle

javascript - Rails AJAX 反馈,尤其是错误反馈

javascript - MUI 中的 <Div>、<StyledDiv> 和 <Box sx={...}> 之间有什么区别?

javascript - slick-slider 中的纯 JS 到 ReactJS