我正在尝试使用钩子在react原生中构建一个应用程序生成器和扫描器二维码。我想要一个解决方案,在生成二维码后将其保存/下载到设备中。我需要帮助,谢谢
react-native-qrcode-generator谁能给我一些关于如何保存这个生成的二维码的建议?任何帮助都非常感谢
发布于 2020-05-29 01:19:08
如果要将二维码中的信息保存为二维码,则必须将二维码转换为png图像,然后将其作为图像存储在数据库中。
所以,我建议你做的是,在保存时,只将信息作为字符串(或任何需要的)存储在数据库中。下载时,只需从您的数据库检索信息,并立即生成二维码。它不会消耗更长的时间。生成二维码后,显示二维码。
我认为这是最适合你的解决方案。
您可能需要使用react-native-qrcode-generator来生成二维码。
下面是一个示例代码。
您可以输入key和value (在顶部输入),然后按“保存”按钮。当按下“保存”按钮时,您必须将键值对(数据)存储到数据库中。
然后,输入您想要下载的key (在底部输入),然后按“下载”按钮。然后,您必须检索与给定键关联的数据(值)。
状态值是在从数据库中成功检索值之后设置的。该状态以prop的形式提供给QRCode组件。
import React, { useState } from 'react';
import { StyleSheet, View, Text, TextInput, TouchableOpacity, AsyncStorage } from 'react-native';
import QRCode from 'react-native-qrcode-generator';
export default function App() {
const [key, setKey] = useState(null);
const [value, setValue] = useState(null);
const [downloadKey, setDownloadKey] = useState(null);
const [qrCodeValue, setQRCodeValue] = useState('');
const save = async () => {
//You have to implement the function that saves your information into your database.
//Here I'm saving data to AsyncStorage. (For sample)
await AsyncStorage.setItem(key, value);
}
const download = async () => {
//You have to implement the function that retrieves your information from your database for given key.
//Here I'm retrieving data from AsyncStorage. (For sample)
const qrValue = await AsyncStorage.getItem(downloadKey);
setQRCodeValue(qrValue);
}
return (
<View style={styles.container}>
<View style={styles.row}>
<TextInput placeholder={'Key'} value={key} onChangeText={(key) => setKey(key)} style={styles.textInput}/>
<TextInput placeholder={'Value'} value={value} onChangeText={(value) => setValue(value)} style={styles.textInput}/>
</View>
<TouchableOpacity style={{ flexDirection: 'row', marginBottom: 50 }} onPress={save}>
<Text style={styles.button}>Save</Text>
</TouchableOpacity>
{qrCodeValue ? <QRCode value={qrCodeValue} size={200} /> : null}
<Text style={{ margin: 10 }}>{qrCodeValue}</Text>
<View style={[styles.row, { marginTop: 50 }]}>
<TextInput placeholder={'Key'} value={downloadKey} onChangeText={(downloadKey) => setDownloadKey(downloadKey)} style={styles.textInput}/>
<TouchableOpacity style={{ flex: 1, flexDirection: 'row' }} onPress={download}>
<Text style={styles.button}>Download</Text>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
paddingTop: 50,
paddingBottom: 50
},
row: {
flexDirection: 'row',
},
textInput: {
flex: 1,
borderColor: '#808080',
borderWidth: 1,
borderRadius: 5,
margin: 5,
paddingLeft: 5,
paddingRight: 5,
},
button: {
flex: 1,
borderColor: '#0040FF',
backgroundColor: '#0080FF',
borderWidth: 1,
borderRadius: 5,
margin: 5,
textAlign: 'center',
textAlignVertical: 'center',
height: 30
},
});这是一个演示..。

请看完这篇文章,如果你还有其他问题,请随时问我。祝好运!
发布于 2020-10-21 16:29:07
您可以使用rn-qr-generator生成具有给定字符串值的二维码。它将返回图像的路径或base64表示。稍后,您可以使用CameraRoll保存图像。
import RNQRGenerator from 'rn-qr-generator';
RNQRGenerator.generate({
value: 'string_value',
height: 100,
width: 100,
base64: false,
backgroundColor: 'black',
color: 'white',
})
.then(response => {
const { uri, width, height, base64 } = response;
})
.catch(error => console.log('Error creating QR code image', error));发布于 2020-05-30 16:23:07
如果要将二维码作为图像保存到设备上,则应将QRcode视图组件转换为png图像,然后将其存储。
您可以使用react-native-view-shot将QRcode视图组件转换为png图像,使用rn-fetch-blob将图像保存到您的图库。
https://stackoverflow.com/questions/62070137
复制相似问题