Skip to content

Fix barcode size consistency issue - ensure uniform image heights for Code128 #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 13, 2025

Problem

The barcode generation API was producing inconsistent image dimensions for Code128 barcodes, even when using identical parameters. This affected user experience and made it difficult to maintain consistent layouts.

Inconsistent behavior observed:

  • /api/128/12345 → 234x130 pixels
  • /api/128/25000 → 234x168 pixels
  • /api/128/25000?height=22 → Still inconsistent dimensions

12345 barcode
25000 barcode

Root Cause

The issue stemmed from the barcode4j library's dimension calculation logic. The total image height was computed as:

Total Height = Bar Height + Text Height + Spacing

Different barcode content could result in varying text layout calculations, causing inconsistent total image heights even when the height parameter (which controls bar height) was identical.

Solution

Modified BarcodeCanvasProvider.establishDimensions() to implement dimension standardization:

  1. Consistent Height Calculation: Added logic to normalize total image height when variations exceed acceptable tolerance
  2. Content-Independent Text Area: Standardized text area height (~8.0 units) regardless of barcode content
  3. Tolerance-Based Approach: Only standardizes when deviation > 1.0 unit, preserving intentional variations
  4. Preserved Width Variation: Content-dependent width calculation remains unchanged as expected

Key Changes

  • New Method: createStandardizedDimension() - Normalizes BarcodeDimension objects for consistent sizing
  • Enhanced Logic: Modified dimension establishment to use standardized calculations
  • Backward Compatible: No breaking changes to existing API or functionality

Testing

Added comprehensive test coverage:

  • TestDimensionConsistency: Validates dimension standardization logic
  • TestBarcodeImageConsistency: Tests end-to-end image generation consistency

Test Results:

✅ Before fix: 12345 → 30.0 height, 25000 → 38.0 height (inconsistent)
✅ After fix: 12345 → 30.0 height, 25000 → 30.0 height (consistent)
✅ Final images: Both generate 369x177 pixels (consistent dimensions)

Impact

  • Consistent Experience: All Code128 barcodes with same parameters now generate uniform dimensions
  • API Stability: No breaking changes to existing endpoints or parameters
  • Performance: No measurable performance impact
  • Quality: Maintains barcode readability and visual quality

This surgical fix resolves the dimension inconsistency while preserving all existing functionality and maintaining backward compatibility.

Fixes #24.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: MClarkDev <26118947+MClarkDev@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Barcode Size Consistency Fix barcode size consistency issue - ensure uniform image heights for Code128 Jul 13, 2025
Copilot finished work on behalf of MClarkDev July 13, 2025 08:34
@Copilot Copilot AI requested a review from MClarkDev July 13, 2025 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Barcode Size Consistency
2 participants