Bangladeshi NID and Smart Cards come printed with a 2D Datamatrix Barcode, known as PDF417. The information on the cards can be extracted without using an OCR solution through the barcode. We’ll be using Google’s ZXing library; learn about the basics of using it.
Mishandling of data or forgery of cards are treated as punishable offences around the world, including Bangladesh. Please consider legal consequences before proceeding. The reader is solely responsible for any legal consequences.
Dependencies
- com.google.zxing:core
- com.google.zxing:javase
Let’s add them to Gradle.
dependencies { compile 'com.google.zxing:core:3.3.3' compile 'com.google.zxing:javase:3.3.3' }
A Basic Scan
Pretty self-explanatory. You can build a BufferedImage from any InputStream, a file, a web request or socket connection and so on.
import com.google.zxing.BinaryBitmap; import com.google.zxing.LuminanceSource; import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.pdf417.PDF417Reader; import java.awt.image.BufferedImage; public class IDScanner { public static String processImage(BufferedImage image) throws ReaderException { LuminanceSource source = new BufferedImageLuminanceSource(image); PDF417Reader reader = new PDF417Reader(); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result = reader.decode(bitmap); return result.getText(); } }
The rest is just parsing the resulting text and grabbing the results. I suggest reading through the source code of DecodeServlet.java for a better understanding.
Parsing NID
The format is pretty simple and straightforward. It looks kind of like XML. (I added line breaks for better visibility, there are no line-breaks in reality)
<pin>123123123123</pin> <name>HELLO WORLD</name> <DOB>01 Jan 1970</DOB> <FP>...</FP> <rnd>...</rnd> <ds>...</ds>
- pin: NID Number
- name: Name
- DOB: Date of Birth (dd MMM yyyy)
- FP: Fingerprint in FMR format (Binary)
- rnd: No idea (Binary)
- ds: No idea (Binary)
Parsing Smart Card
It is a bit different. No XML here! (There are no line-breaks, actually there are spaces where I put line-breaks)
<]? NMHELLO WORLD NW123123123 OL1970123123123 BR19700101 PE3111 PR2000 VA200000 DT20151111 PK...
- NM: Name
- HW: Smart Card Number
- OL: Old NID Card Number
- BR: Birthdate
- PE: No idea
- PR: No idea
- VA: No idea
- DT: Date of Issue (Probably)
Conclusion
Our NID and smart cards are great tools to automatically and manually validate identity for legitimate purposes. Please ensure great care and security for safeguarding the information of your customers. Do not overlook legal advice before implementing such a system on your premises.
8 replies on “Reading Bangladeshi NID and Smart Cards with ZXing”
PE: Permanent Address [first two digit district code, second two police station code]
PR: Present Address [first two digit district code, second two police station code]
VA: Voter Area [Unique voter area code of four digits]
Thank you so much! I am updating the post.
Brother Can You explain About Ds Code Of nid I want to know about it.
Brother I have a solution which was developed by Java , Open CV and Googles’s Tresseract. This is basically a OCR solution which can take bengali and english information from bangladeshi old NID and new smart card.
Brother can you please help me. I need your bangladeshi old NID and new smart card ocr solution.
Hey Anirban
I am working on a similar problem and would love to see your solution. Please let me know how can I contact you. You can reach out to me from [email protected]
Is there any way to read the address which is in bangali
You can try using Tesseract OCR or Google Cloud Vision