【问题标题】:How to save/download generate QRCode inn react native如何在 React Native 中保存/下载生成 QR 码
【发布时间】:2019-02-13 08:07:30
【问题描述】:

通过使用此代码,我能够生成 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,
  },
});

//谢谢。 // 在二维码中,我可以生成二维码扫描仪,我已经完成了,但是如何下载/保存或共享该二维码,请帮助

【问题讨论】:

  • 请注意,react-native-qrcode 不再维护,您的请求似乎从未实现。我建议您尝试 react-native-qrcode-svg,效果很好,您可以将图像导出为 png,例如
  • 谢谢..还有一件事我需要链接..??
  • 不直接:我的意思是库使用 react-native-svg,所以我应该在安装 react-native-qrcode-svg 之前安装并链接它
  • 好的让我试试.. 谢谢
  • null 不是对象(正在评估 RNGestureHandlerModule.State).. 收到此错误

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


【解决方案1】:

此答案指的是 react-native-qrcode-svg 库,如问题 cmets 中所写

使用这个库,您可以创建一个 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>

【讨论】:

  • 我会试试的.. 非常感谢 :)
  • getRef={(ref?) - ?- 只能在 .ts 文件中使用..我做了更改..我可以更新我的问题吗?或者如果不介意我们可以在某个地方聊天吗?比如 LinkedIn 或 gmail?
  • 是的,对不起,我忘记删除 ?,因为我从我正在处理的 .ts 项目中获取了它
  • 我不知道我做的是否正确,因为长按什么都没有发生..
  • 没有任何代码我无法理解问题,更新您的答案或尝试解释您在哪里长按呼叫
【解决方案2】:

您可以使用react-native-view-shot创建二维码图像,然后将其保存在相机胶卷或磁盘存储中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多