Reading Damaged Barcode
Reading damaged barcodes
Real-world barcodes are rarely perfect. Photos from mobile devices, low-quality printers, scanners with dust or scratches — all of this can introduce noise, blur, and other distortions that make recognition harder.
Aspose.BarCode for Java provides several tuning knobs under QualitySettings that help you keep recognition robust even when the image is degraded:
BarcodeQualityMode— how aggressive the recognition pipeline should beDeconvolutionMode— how strong blur compensation should beXDimensionModeandsetMinimalXDimension— hints about expected bar/module size
This article is based on the sample class:
com.aspose.barcode.guide.recognition.performance.ReadingDamagedBarcodeExample
You can find the full source code on GitHub:
ReadingDamagedBarcodeExample.java
In the snippets below, helper methods like ExampleAssist.checkOrCreateImage are used only to generate test images in the sample project. In your application you can replace them with your own file I/O.
1. Test images: clean vs damaged
The example class generates a small set of baseline images and then creates damaged variants:
-
Clean images
code128_clean.png— clean Code 128 barcodeqr_clean.png— clean QR code with slightly thicker modules
-
Damaged images
code128_noisy.png— Code 128 with added Gaussian noise (simulates low SNR)qr_blurred_mild.png— mildly blurred QR (recoverable with stronger settings)qr_blurred_heavy.png— heavily blurred + slightly noisy QR (too degraded for FAST mode)
The damaged images are produced using helper methods like:
ExampleAssist.addGaussianNoise(inputPath, outputPath, stdDev)ExampleAssist.blur(inputPath, outputPath, sigma)
This setup allows you to compare recognition behavior under different QualitySettings configurations.
2. Handling noise on Code 128 with BarcodeQualityMode
2.1 Noisy Code 128 with BarcodeQualityMode.LOW
The method read_Code128_Noisy_WithBarcodeQuality_LOW demonstrates how to recognize a noisy Code 128 barcode by switching to a more robust quality profile.
String imagePath = ExampleAssist.pathCombine(FOLDER, "code128_noisy.png");
// Reader is restricted to Code 128
BarCodeReader barCodeReader = new BarCodeReader(imagePath, DecodeType.CODE_128);
// Start from a robust preset
QualitySettings qualitySettings = QualitySettings.getHighQuality();
// Enable heavy methods for low-quality / noisy bars
qualitySettings.setBarcodeQuality(BarcodeQualityMode.LOW);
// Apply settings to the reader
barCodeReader.setQualitySettings(qualitySettings);
// Validate that Code 128 is recognized
ExampleAssist.assertRecognized(
barCodeReader,
"code128_noisy.png",
1,
DecodeType.CODE_128
);
Key ideas:
QualitySettings.getHighQuality()enables a more thorough recognition pipeline.BarcodeQualityMode.LOWturns on additional heavy methods for very noisy / low-contrast barcodes.- This combination is appropriate when latency is less important than robustness.
2.2 Tuning for tiny or weak modules with XDimensionMode
The method read_Code128_Noisy_TinyModules_Tuned shows how to hint that bars may be very small by using XDimensionMode and setMinimalXDimension.
String imagePath = ExampleAssist.pathCombine(FOLDER, "code128_noisy.png");
BarCodeReader barCodeReader = new BarCodeReader(imagePath, DecodeType.CODE_128);
// Start from a performance-oriented preset
QualitySettings qualitySettings = QualitySettings.getHighPerformance();
// Hint that bars can be very small
qualitySettings.setXDimension(XDimensionMode.SMALL);
qualitySettings.setMinimalXDimension(1.0f);
// Still use LOW quality for noisy / weak bars
qualitySettings.setBarcodeQuality(BarcodeQualityMode.LOW);
barCodeReader.setQualitySettings(qualitySettings);
ExampleAssist.assertRecognized(
barCodeReader,
"code128_noisy.png",
1,
DecodeType.CODE_128
);
What this configuration does:
XDimensionMode.SMALLtells the engine to search for small bars / modules.setMinimalXDimension(1.0f)expresses that you expect bar width to be as small as 1 pixel.BarcodeQualityMode.LOWagain enables heavier processing to help with noise.
This combination is useful for:
- Low-resolution images (for example, thumbnails, webcam captures)
- Screenshots where barcodes are very small
- Any scenario where bars are thin and signal-to-noise ratio is low
3. Recovering blurred QR codes with DeconvolutionMode
Blur (motion or out-of-focus) is a common failure mode for mobile captures. Aspose.BarCode exposes a blur compensation pipeline through DeconvolutionMode:
FAST— light, speed-first restorationNORMAL— balancedSLOW— strongest blur compensation, but also the slowest
3.1 Mild blur + DeconvolutionMode.SLOW (positive case)
The method read_QR_BlurredMild_WithDeconvolution_SLOW demonstrates how to restore a mildly blurred QR code.
String imagePath = ExampleAssist.pathCombine(FOLDER, "qr_blurred_mild.png");
BarCodeReader barCodeReader = new BarCodeReader(imagePath, DecodeType.QR);
// Start from HighQuality preset
QualitySettings qualitySettings = QualitySettings.getHighQuality();
// Enable strongest deconvolution for blur
qualitySettings.setDeconvolution(DeconvolutionMode.SLOW);
// Hint that modules can be small if needed
qualitySettings.setXDimension(XDimensionMode.SMALL);
qualitySettings.setMinimalXDimension(1.0f);
barCodeReader.setQualitySettings(qualitySettings);
ExampleAssist.assertRecognized(
barCodeReader,
"qr_blurred_mild.png",
1,
DecodeType.QR
);
Why this works:
DeconvolutionMode.SLOWenables the strongest blur-compensation pipeline, suitable for mild to moderate blur.- The X-dimension hints help when blur slightly reduces contrast and apparent module size.
- This configuration trades performance for maximum robustness.
Use it when:
- You receive images with clear blur from camera motion or focus issues.
- Missing a barcode is worse than spending extra CPU time per frame.
3.2 Heavy blur + DeconvolutionMode.FAST (negative case)
The method read_QR_BlurredHeavy_WithDeconvolution_FAST_Negative is intentionally a negative test.
It shows that using a lightweight deconvolution profile on a heavily blurred image can result in zero recognized barcodes.
String imagePath = ExampleAssist.pathCombine(FOLDER, "qr_blurred_heavy.png");
BarCodeReader barCodeReader = new BarCodeReader(imagePath, DecodeType.QR);
// Use performance preset with light deconvolution
QualitySettings qualitySettings = QualitySettings.getHighPerformance();
qualitySettings.setDeconvolution(DeconvolutionMode.FAST);
barCodeReader.setQualitySettings(qualitySettings);
// Expectation: image is too blurred for FAST profile → no results
ExampleAssist.assertNotRecognized(
barCodeReader,
"qr_blurred_heavy.png"
);
Takeaways:
DeconvolutionMode.FASTis ideal for slightly blurred or sharp images where performance is critical.- For heavily blurred barcodes, FAST may not be sufficient — you should switch to
NORMALorSLOW. - Using negative tests like this in your own QA can help you understand where the performance/quality trade-off breaks for your images.
4. Practical guidelines
When working with damaged barcodes (noise, blur, small modules), consider the following patterns:
-
Start from a preset that matches your typical quality
QualitySettings.getHighPerformance()for speed-first scenarios with mostly clean images.QualitySettings.getNormalQuality()when you want balanced speed and robustness.QualitySettings.getHighQuality()when you expect many difficult images.
-
Use
BarcodeQualityModefor noise and contrast issues- Use
BarcodeQualityMode.LOWwhen bars are noisy, low-contrast or partially degraded. - Use
BarcodeQualityMode.NORMALfor regular images. - Use
BarcodeQualityMode.HIGHwhen you care about speed and inputs are high-quality.
- Use
-
Use
DeconvolutionModefor blurDeconvolutionMode.FAST— high-quality or slightly blurred images.DeconvolutionMode.NORMAL— mild blur from typical camera motion.DeconvolutionMode.SLOW— heavily blurred or difficult captures.
-
Hint expected bar/module size with X-dimension settings
setXDimension(XDimensionMode.SMALL)andsetMinimalXDimension(...)help for tiny barcodes.- Do not set
MinimalXDimensiontoo small if your images are not tiny — it may slow recognition.
-
Use negative tests deliberately
- Like
read_QR_BlurredHeavy_WithDeconvolution_FAST_Negative, negative tests help document the limits of a given configuration and provide guidance on when to use stronger modes.
- Like
Summary
The ReadingDamagedBarcodeExample class shows how to combine QualitySettings options to handle various kinds of degradation:
-
Noise on 1D Code 128:
- Use
BarcodeQualityMode.LOWwith a robust preset. - Combine with X-dimension hints for tiny or weak bars.
- Use
-
Blur on QR codes:
- Use
DeconvolutionMode.SLOWtogether withQualitySettings.getHighQuality()and X-dimension hints for mildly blurred images. - Understand that
DeconvolutionMode.FASTis not sufficient for heavily blurred images — expect failures and fall back to SLOW when needed.
- Use
All examples in this article are based on:
ReadingDamagedBarcodeExample.java
Use these patterns as a starting point when tuning Aspose.BarCode for Java to your own damaged or low-quality barcode images.