expo-barcode
Version:
Library to generate barcodes and qr codes natively using Kotlin and Swift, integrated with expo.
70 lines • 2.43 kB
JavaScript
import { useEffect, useState } from "react";
import { Image } from "react-native";
import ExpoBarcodeModule from "./ExpoBarcodeModule";
export async function generateBarcode(value, width, height) {
const returnValue = await ExpoBarcodeModule.generateBarcode(value, width, height);
const finalReturn = `data:image/png;base64,${returnValue}`;
return finalReturn;
}
export async function generateQRCode(value, width, height) {
const returnValue = await ExpoBarcodeModule.generateQRCode(value, width, height);
const finalReturn = `data:image/png;base64,${returnValue}`;
return finalReturn;
}
export function BarcodeView({ value, width, height }) {
const [barcode, setBarcode] = useState("");
if (value === "") {
throw new Error("Value cannot be empty");
}
if (width <= 0 || typeof width !== "number") {
throw new Error("Width must be a positive number");
}
if (height <= 0 || typeof height !== "number") {
throw new Error("Height must be a positive number");
}
useEffect(() => {
async function waitFor() {
const barcodeGeneradet = await generateBarcode(value, width, height);
setBarcode(barcodeGeneradet);
}
waitFor();
return () => {
setBarcode("");
};
}, [height, value, width]);
return (<>
{barcode && (<Image testID="barcode" source={{ uri: barcode }} style={{
width,
height,
}}/>)}
</>);
}
export function QRCodeView({ value, width, height }) {
const [barcode, setBarcode] = useState("");
if (value === "") {
throw new Error("Value cannot be empty");
}
if (width <= 0 || typeof width !== "number") {
throw new Error("Width must be a positive number");
}
if (height <= 0 || typeof height !== "number") {
throw new Error("Height must be a positive number");
}
useEffect(() => {
async function waitFor() {
const qrCodeGeneradet = await generateQRCode(value, width, height);
setBarcode(qrCodeGeneradet);
}
waitFor();
return () => {
setBarcode("");
};
}, [height, value, width]);
return (<>
{barcode && (<Image testID="qr-code" source={{ uri: barcode }} style={{
width,
height,
}}/>)}
</>);
}
//# sourceMappingURL=index.js.map