我正在尝试在我的应用程序中实现一个功能,用户可以从他们的相机胶卷中选择一张图片,并且应用程序将解码图像中的QR码 .
我目前正在使用react-native-camera-roll-picker:https://github.com/jeanpan/react-native-camera-roll-picker和react-native-qrcode-local-image:https://github.com/remobile/react-native-qrcode-local-image
问题是本地QR码图像库要求我传递本地路径,并且与react-native-camera-roll-picker提供的本机uri不兼容 . 我会使用另一个库来解码图像QR码但这个似乎是唯一适用于iOS和Android并从现有图像而不是从实际相机扫描的图像 .
我也给了我麻烦:https://github.com/wkh237/react-native-fetch-blob
这是我目前在一个函数中的尝试,我在react-native-camera-roll-picker的“回调”道具中调用(以前的尝试被注释掉了):
_pickedImage(array,currentImg) {
console.log(currentImg)
var path = RNFS.DocumentDirectoryPath + '/pickedqr';
let rnfbURI = RNFetchBlob.wrap(RNFetchBlob.fs.asset(currentImg.uri))
const Blob = RNFetchBlob.polyfill.Blob
Blob.build(rnfbURI, {type:'image/jpg'}).then((b) => {
tmpBlob = b;
RNFetchBlob.fs.readFile(tmpBlob, 'base64').then((data) => {
console.log("Base64", data)
QRDecoder.decode(`data:image/gif;base64,${data}`, (error, result)=>{
console.log("Code", result)
console.log("Error", error)
});
});
})
/*fullPath = currentImg.uri.replace("assets-library://", "cdvfile://localhost/assets-library/")
QRDecoder.decode(fullPath, (error, result)=>{
console.log("Code", result)
console.log("Error", error)
});*/
/*let blb = Blob.build( rnfbURI, { type: 'image/jpg'})
console.log(blb)*/
/*RNFetchBlob.fs.readFile(rnfbURI, 'base64').then((data) => {
console.log("Base64", data)
QRDecoder.decode(`data:image/gif;base64,${data}`, (error, result)=>{
console.log("Code", result)
console.log("Error", error)
});
})*/
}
我现在完全失去了所以任何方法或见解都会受到高度赞赏 .
1 回答
您可以使用
react-native-qrcode-scanner
从图像或直接通过相机扫描QR .INSTALLATION:
使用此命令安装依赖项:
使用以下方法链接这些库:
您需要将权限添加到项目的AndroidManifest.xml中 . 这应该在你的_2413158中找到_添加以下内容:
对于iOS 10及更高版本,您需要将"Privacy - Camera Usage Description"键添加到项目的info.plist中 . 这应该在
your_project/ios/your_project/Info.plist
中找到 . 添加以下代码:用法:
你可以在这里阅读更多关于这个图书馆的信息:https://www.npmjs.com/package/react-native-qr-scanner