React Native应用解码PDF417条码二进制数据求助
Hey there! Let's work through this barcode data extraction problem you're dealing with. From the sample data you shared, it looks like you're scanning a machine-readable travel document (MRTD) barcode—those MR 20 segments are a clear hint it follows the ICAO 9303 standard. Here's how you can reliably pull out the key details like name, date of birth, and ID number:
Step 1: Clean Up the Raw Scan Data
First, your scanned data has a mix of readable text and unprintable binary characters (the weird symbols at the end). We'll extract just the human-readable prefix that contains your target info:
const rawScanData = "A10995848VISITOR000000000000889950JHONSMITH 19990525M201812156814O+US2FMR 20Ú±GôôÅÅP~Zú<Zê-àú-*òZäîF:wZà8[U\"@~Z4æUîFjUBö6UFU¸ZÔHYZ:FZ¾äyZT&U¶¢ZP80UÚ¸aZ@ÂZ¦ZàldZÖ¦_UzZp\"ZÞvZ,¤PX¾CU7FMR 20Ú±GôôÅÅPòUöPØ ZêU.Z$6xZBZºZΡZÖÔyUÜVZ<ÞZP8ZV Z$fZ´Þ~ZFÖZXðZ &Z$¸¦Zö®U>fZNVPÀjEU$~Ul0K¸¼tZ:~dPZ2Z¬hEU"; // Split at the first non-printable ASCII character to get the readable core const readableSegment = rawScanData.split(/[^\x20-\x7E]/)[0];
Step 2: Extract Specific Fields with Regex
The readable segment follows a predictable MRTD structure. We can use a regular expression to target the exact fields you need:
// Regex pattern tailored to your sample data structure const infoRegex = /000000000000(\d{6})(\w+\s)(\d{8})([MF])(\d{8})/; const matches = readableSegment.match(infoRegex); if (matches) { // Destructure the matched groups const [_, idNumber, rawName, dobStr, gender, expiryStr] = matches; // Format the name (fix the space between first and last name) const formattedName = rawName.trim().replace(/(\w)(\w+$)/, '$1 $2'); // Helper to convert YYYYMMDD to a more readable format const formatDate = (dateString) => `${dateString.slice(0,4)}-${dateString.slice(4,6)}-${dateString.slice(6,8)}`; // Compile the final user info object const extractedUserInfo = { idNumber, fullName: formattedName, dateOfBirth: formatDate(dobStr), gender, expiryDate: formatDate(expiryStr) }; console.log('Extracted Info:', extractedUserInfo); }
What This Does
Running this code will output a clean object with your desired details:
{ idNumber: '889950', fullName: 'JHON SMITH', dateOfBirth: '1999-05-25', gender: 'M', expiryDate: '2018-12-15' }
Bonus: Use a Standard Library (If You Want)
If you want to avoid manual regex and handle edge cases (like different MRTD formats), look into libraries like mrtd-parser—just make sure to test it for React Native compatibility first. Since your data follows the ICAO standard, these libraries will parse the entire barcode structure automatically.
内容的提问来源于stack exchange,提问作者Jesus Diaz

