Last active
August 28, 2024 22:40
-
-
Save tbotalla/88a9d62a98f88fba5b95b11ec38b01d5 to your computer and use it in GitHub Desktop.
Argentina's DNI parser reading PDF417 code, supports old & new formats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tbotalla.dniparser; | |
import com.google.zxing.BinaryBitmap; | |
import com.google.zxing.LuminanceSource; | |
import com.google.zxing.client.j2se.BufferedImageLuminanceSource; | |
import com.google.zxing.common.HybridBinarizer; | |
import com.google.zxing.pdf417.PDF417Reader; | |
import org.apache.commons.lang3.StringUtils; | |
import javax.imageio.ImageIO; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
/** | |
* Required dependencies: | |
* <dependency> | |
* <groupId>com.google.zxing</groupId> | |
* <artifactId>core</artifactId> | |
* <version>3.3.0</version> | |
* </dependency> | |
* <dependency> | |
* <groupId>com.google.zxing</groupId> | |
* <artifactId>javase</artifactId> | |
* <version>3.3.0</version> | |
* </dependency> | |
* <dependency> | |
* <groupId>org.apache.commons</groupId> | |
* <artifactId>commons-lang3</artifactId> | |
* <version>3.11</version> | |
* </dependency> | |
*/ | |
public class PDF417Parser { | |
/** | |
* Tested and working with files in .png and .jpg, not with .jpeg | |
* | |
* @param imageWithBarcode | |
* @return | |
* @throws IOException if file not found | |
*/ | |
private static String decodePDF417Code(File imageWithBarcode) throws IOException { | |
BufferedImage bufferedImage = ImageIO.read(imageWithBarcode); | |
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); | |
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); | |
try { | |
return new PDF417Reader().decode(bitmap).getText(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println("There is no PDF417 code in the image"); | |
} | |
return StringUtils.EMPTY; | |
} | |
public static void main(String[] args) { | |
try { | |
File file = new File("dni_batata.jpg"); | |
String decodedText = decodePDF417Code(file); | |
if (decodedText == null) { | |
System.out.println("No PDF417 Code found in the image"); | |
} else { | |
System.out.println("Decoded text = " + decodedText); | |
DniData dniData = parseDocumentData(decodedText); | |
} | |
} catch (IOException e) { | |
System.out.println("Could not decode QR Code, IOException :: " + e.getMessage()); | |
} | |
} | |
public static DniData parseDocumentData(String decodedText) { | |
String[] splittedData = decodedText.split("@"); | |
DniData dniData = new DniData(); | |
try { | |
if (splittedData.length == 8 || splittedData.length == 9) { | |
// Formato nuevo (codigo barras en el frente), no contiene fecha de expiracion | |
dniData.setLastName(splittedData[1].trim()); | |
dniData.setFirstName(splittedData[2].trim()); | |
dniData.setGender(splittedData[3].trim()); | |
dniData.setDocumentNumber(splittedData[4].trim()); | |
dniData.setBirthDate(splittedData[6].trim()); | |
dniData.setIssuedDate(splittedData[7].trim()); | |
} else if (splittedData.length == 17 || splittedData.length == 16) { | |
// Formato anterior (codigo barras en el reverso) | |
dniData.setDocumentNumber(splittedData[1].trim()); | |
dniData.setLastName(splittedData[4].trim()); | |
dniData.setFirstName(splittedData[5].trim()); | |
dniData.setBirthDate(splittedData[7].trim()); | |
dniData.setGender(splittedData[8].trim()); | |
dniData.setIssuedDate(splittedData[9].trim()); | |
dniData.setExpirationDate(splittedData[12].trim()); | |
} else { | |
// Formato no identificado | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
return dniData; | |
} | |
} | |
public static class DniData { | |
private String firstName; | |
private String lastName; | |
private String gender; | |
private String documentNumber; | |
private String birthDate; | |
private String issuedDate; | |
private String expirationDate; | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public String getGender() { | |
return gender; | |
} | |
public void setGender(String gender) { | |
this.gender = gender; | |
} | |
public String getDocumentNumber() { | |
return documentNumber; | |
} | |
public void setDocumentNumber(String documentNumber) { | |
this.documentNumber = documentNumber; | |
} | |
public String getBirthDate() { | |
return birthDate; | |
} | |
public void setBirthDate(String birthDate) { | |
this.birthDate = birthDate; | |
} | |
public String getIssuedDate() { | |
return issuedDate; | |
} | |
public void setIssuedDate(String issuedDate) { | |
this.issuedDate = issuedDate; | |
} | |
public String getExpirationDate() { | |
return expirationDate; | |
} | |
public void setExpirationDate(String expirationDate) { | |
this.expirationDate = expirationDate; | |
} | |
@Override | |
public String toString() { | |
return "DniData{" + | |
"firstName='" + firstName + '\'' + | |
", lastName='" + lastName + '\'' + | |
", gender='" + gender + '\'' + | |
", documentNumber='" + documentNumber + '\'' + | |
", birthDate='" + birthDate + '\'' + | |
", issuedDate='" + issuedDate + '\'' + | |
", expirationDate='" + expirationDate + '\'' + | |
'}'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment