CanScan
Loading...
Searching...
No Matches
fr.softsf.canscan.ui.EncodedImage Class Reference

Thread-safe singleton to hold a shared QR code BufferedImage. More...

+ Collaboration diagram for fr.softsf.canscan.ui.EncodedImage:

Public Member Functions

void drawFinderPatterns (Graphics2D g, int matrixWidth, CommonFields config)
 Renders the three QR code finder patterns at the corners using the specified style.
 
void drawLogoIfPresent (Graphics2D g, CommonFields config) throws IOException
 Draws the logo at the center of the QR code if a valid logo file is provided.
 
void drawSquareFinderPatternAtPixel (Graphics2D g, double x, double y, double diameter, Color qrColor, Color bgColor)
 Draws a standard square QR code finder pattern at the specified pixel coordinates.
 
void freeQrOriginal ()
 Releases the current QR code image and its resources.
 
BufferedImage generateImage (String data, CommonFields config) throws WriterException, IOException
 Generates a QR code image with optional logo and styling.
 
BufferedImage getQrOriginal ()
 Retrieves a defensive copy of the QR code image.
 
void updateQrOriginal (BufferedImage newImage)
 Updates the QR code image using a defensive copy.
 

Private Member Functions

BufferedImage createDefensiveCopy (BufferedImage sourceImage)
 Creates a defensive copy of the source image.
 
BitMatrix createMatrix (String text, int margin) throws WriterException
 Creates a QR code matrix for the given text.
 
void drawModule (Graphics2D g, int x, int y, double moduleSizeX, double moduleSizeY, CommonFields config)
 Draws a single QR code module at the specified coordinates with configured size and shape.
 
void drawModules (Graphics2D g, BitMatrix matrix, CommonFields config)
 Renders all QR code modules onto the provided graphics context.
 
void drawRoundedFinderPatternAtPixel (Graphics2D g, double x, double y, double diameter, Color qrColor, Color bgColor)
 Draws a QR code finder pattern with rounded corners at the specified pixel coordinates.
 
long estimateImageMemoryMB (int size)
 Estimates the memory required to create a BufferedImage.
 
void fillBackground (Graphics2D g, int size, Color bgColor)
 Fills the entire QR code area with the specified background color.
 
long getAvailableMemoryMB ()
 Gets the available memory in the JVM with safety margin for image generation.
 
boolean isInBottomLeftPattern (int x, int y, int matrixHeight, int margin)
 Checks if coordinates are within the bottom-left position pattern.
 
boolean isInPositionPattern (int x, int y, int matrixWidth, int matrixHeight, int margin)
 Determines whether the given coordinates fall within any of the three QR code position patterns.
 
boolean isInTopLeftPattern (int x, int y, int margin)
 Checks if coordinates are within the top-left position pattern.
 
boolean isInTopRightPattern (int x, int y, int matrixWidth, int margin)
 Checks if coordinates are within the top-right position pattern.
 
boolean shouldSkipModule (int x, int y, BitMatrix matrix, ModuleContext ctx)
 Determines whether a QR code module should be skipped during rendering.
 
void validateMemoryForImageSize (int size)
 Validates that sufficient memory is available to generate a square image of the given size.
 

Private Attributes

final Object imageLock = new Object()
 
BufferedImage qrOriginal
 

Static Private Attributes

static final int AVAILABLE_MEMORY_TO_GENERATE_IMAGE = 50
 
static final String BG_COLOR = "bgColor"
 
static final int BYTES_PER_KILOBYTE = 1024
 
static final String CONFIG = "config"
 
static final double DEFAULT_GAP_BETWEEN_LOGO_AND_MODULES = 0.9
 
static final String DRAW_MODULES = "drawModules"
 
static final String DRAW_ROUNDED_FINDER_PATTERN_AT_PIXEL
 
static final String DRAW_SQUARE_FINDER_PATTERN_AT_PIXEL
 
static final String GENERATE_QR_CODE_IMAGE = "generateQrCodeImage"
 
static final String MATRIX = "matrix"
 
static final String SHOULD_SKIP_MODULE = "shouldSkipModule"
 

Detailed Description

Thread-safe singleton to hold a shared QR code BufferedImage.

All access is synchronized for thread safety.

Member Function Documentation

◆ createDefensiveCopy()

BufferedImage fr.softsf.canscan.ui.EncodedImage.createDefensiveCopy ( BufferedImage sourceImage)
private

Creates a defensive copy of the source image.

Parameters
sourceImageThe image to copy (must not be null).
Returns
A new BufferedImage instance with the content of the source image.

◆ createMatrix()

BitMatrix fr.softsf.canscan.ui.EncodedImage.createMatrix ( String text,
int margin ) throws WriterException
private

Creates a QR code matrix for the given text.

Parameters
textThe string to encode in the QR code.
marginThe outer margin of the QR code in modules.
Returns
A BitMatrix representing the encoded QR code.
Exceptions
WriterExceptionIf encoding fails.

◆ drawFinderPatterns()

void fr.softsf.canscan.ui.EncodedImage.drawFinderPatterns ( Graphics2D g,
int matrixWidth,
CommonFields config )

Renders the three QR code finder patterns at the corners using the specified style.

Parameters
gThe graphics context for rendering.
matrixWidthWidth of the QR code matrix.
configConfiguration containing size, colors, module shape, and margin.
+ Here is the call graph for this function:

◆ drawLogoIfPresent()

void fr.softsf.canscan.ui.EncodedImage.drawLogoIfPresent ( Graphics2D g,
CommonFields config ) throws IOException

Draws the logo at the center of the QR code if a valid logo file is provided.

The logo is scaled to fit within 90% of the designated white box area, which is determined by the QR code size and configured image ratio.

Parameters
gThe Graphics2D context used for rendering the QR code.
configQR code configuration containing size, logo file, and image ratio.
Exceptions
IOExceptionIf reading the logo file fails or the file is not a valid image.

◆ drawModule()

void fr.softsf.canscan.ui.EncodedImage.drawModule ( Graphics2D g,
int x,
int y,
double moduleSizeX,
double moduleSizeY,
CommonFields config )
private

Draws a single QR code module at the specified coordinates with configured size and shape.

Supports square or rounded modules depending on configuration.

Parameters
gthe graphics context used for rendering
xthe module's x-coordinate in the matrix
ythe module's y-coordinate in the matrix
moduleSizeXwidth of the module in pixels
moduleSizeYheight of the module in pixels
configthe QR code configuration

◆ drawModules()

void fr.softsf.canscan.ui.EncodedImage.drawModules ( Graphics2D g,
BitMatrix matrix,
CommonFields config )
private

Renders all QR code modules onto the provided graphics context.

Modules that are part of finder patterns or the central logo area are skipped. Supports rounded or square modules according to configuration.

Parameters
gthe graphics context used for drawing
matrixthe QR code bit matrix representing module positions
configthe QR code configuration including size, colors, module shape, margin, and logo ratio
+ Here is the call graph for this function:

◆ drawRoundedFinderPatternAtPixel()

void fr.softsf.canscan.ui.EncodedImage.drawRoundedFinderPatternAtPixel ( Graphics2D g,
double x,
double y,
double diameter,
Color qrColor,
Color bgColor )
private

Draws a QR code finder pattern with rounded corners at the specified pixel coordinates.

Parameters
gGraphics2D context to draw on.
xX-coordinate of the top-left corner.
yY-coordinate of the top-left corner.
diameterDiameter of the finder pattern.
qrColorColor of the QR modules.
bgColorBackground color inside the pattern.

◆ drawSquareFinderPatternAtPixel()

void fr.softsf.canscan.ui.EncodedImage.drawSquareFinderPatternAtPixel ( Graphics2D g,
double x,
double y,
double diameter,
Color qrColor,
Color bgColor )

Draws a standard square QR code finder pattern at the specified pixel coordinates.

Parameters
gGraphics2D context to draw on.
xX-coordinate of the top-left corner.
yY-coordinate of the top-left corner.
diameterDiameter of the finder pattern.
qrColorColor of the QR modules.
bgColorBackground color inside the pattern.

◆ estimateImageMemoryMB()

long fr.softsf.canscan.ui.EncodedImage.estimateImageMemoryMB ( int size)
private

Estimates the memory required to create a BufferedImage.

Parameters
sizeThe width and height of the square image.
Returns
Estimated memory in megabytes.

◆ fillBackground()

void fr.softsf.canscan.ui.EncodedImage.fillBackground ( Graphics2D g,
int size,
Color bgColor )
private

Fills the entire QR code area with the specified background color.

Parameters
gThe graphics context used for drawing.
sizeThe width and height of the QR code area in pixels.
bgColorThe background color to fill.

◆ freeQrOriginal()

void fr.softsf.canscan.ui.EncodedImage.freeQrOriginal ( )

Releases the current QR code image and its resources.

◆ generateImage()

BufferedImage fr.softsf.canscan.ui.EncodedImage.generateImage ( String data,
CommonFields config ) throws WriterException, IOException

Generates a QR code image with optional logo and styling.

Uses ZXing with high error correction to ensure scanability even when a logo is embedded. Visual properties such as size, colors, margin, and module style are applied from the given configuration.

Parameters
datathe string to encode in the QR code
configconfiguration including size, colors, margin, module style, and optional logo
Returns
a BufferedImage containing the generated QR code
Exceptions
WriterExceptionif encoding the data fails
IOExceptionif reading the logo file fails
OutOfMemoryErrorif the requested size exceeds available memory
+ Here is the call graph for this function:

◆ getAvailableMemoryMB()

long fr.softsf.canscan.ui.EncodedImage.getAvailableMemoryMB ( )
private

Gets the available memory in the JVM with safety margin for image generation.

Returns
Available memory in megabytes, minus the reserved safety margin. Returns 0 if not enough memory is available.

◆ getQrOriginal()

BufferedImage fr.softsf.canscan.ui.EncodedImage.getQrOriginal ( )

Retrieves a defensive copy of the QR code image.

Synchronized. Returns a copy to prevent external mutation of the internal state.

Returns
A defensive copy of the image, or null.
+ Here is the call graph for this function:

◆ isInBottomLeftPattern()

boolean fr.softsf.canscan.ui.EncodedImage.isInBottomLeftPattern ( int x,
int y,
int matrixHeight,
int margin )
private

Checks if coordinates are within the bottom-left position pattern.

Parameters
xX-coordinate
yY-coordinate
matrixHeightHeight of the QR matrix
marginQR code margin
Returns
true if inside bottom-left pattern; false otherwise

◆ isInPositionPattern()

boolean fr.softsf.canscan.ui.EncodedImage.isInPositionPattern ( int x,
int y,
int matrixWidth,
int matrixHeight,
int margin )
private

Determines whether the given coordinates fall within any of the three QR code position patterns.

Parameters
xX-coordinate in the QR matrix.
yY-coordinate in the QR matrix.
matrixWidthWidth of the QR matrix.
matrixHeightHeight of the QR matrix.
marginQR code margin in modules.
Returns
true if the coordinate is inside a position pattern; false otherwise.
+ Here is the call graph for this function:

◆ isInTopLeftPattern()

boolean fr.softsf.canscan.ui.EncodedImage.isInTopLeftPattern ( int x,
int y,
int margin )
private

Checks if coordinates are within the top-left position pattern.

Parameters
xX-coordinate
yY-coordinate
marginQR code margin
Returns
true if inside top-left pattern; false otherwise

◆ isInTopRightPattern()

boolean fr.softsf.canscan.ui.EncodedImage.isInTopRightPattern ( int x,
int y,
int matrixWidth,
int margin )
private

Checks if coordinates are within the top-right position pattern.

Parameters
xX-coordinate
yY-coordinate
matrixWidthWidth of the QR matrix
marginQR code margin
Returns
true if inside top-right pattern; false otherwise

◆ shouldSkipModule()

boolean fr.softsf.canscan.ui.EncodedImage.shouldSkipModule ( int x,
int y,
BitMatrix matrix,
ModuleContext ctx )
private

Determines whether a QR code module should be skipped during rendering.

A module is skipped if it is unset, part of a finder pattern, or overlaps the central logo area.

Parameters
xthe module's x-coordinate in the QR matrix
ythe module's y-coordinate in the QR matrix
matrixthe QR code bit matrix
ctxthe module context containing configuration, matrix dimensions, and logo box
Returns
true if the module should be skipped, false otherwise
+ Here is the call graph for this function:

◆ updateQrOriginal()

void fr.softsf.canscan.ui.EncodedImage.updateQrOriginal ( BufferedImage newImage)

Updates the QR code image using a defensive copy.

Stores a copy of the input (input defense).

Parameters
newImageThe new image; null clears the current image.
+ Here is the call graph for this function:

◆ validateMemoryForImageSize()

void fr.softsf.canscan.ui.EncodedImage.validateMemoryForImageSize ( int size)
private

Validates that sufficient memory is available to generate a square image of the given size.

Applies a hard limit of 200M px and ensures a minimum memory margin before allocation.

Parameters
sizethe width and height of the square image in pixels
Exceptions
OutOfMemoryErrorif the image exceeds pixel limits or available memory is insufficient
+ Here is the call graph for this function:

Member Data Documentation

◆ AVAILABLE_MEMORY_TO_GENERATE_IMAGE

final int fr.softsf.canscan.ui.EncodedImage.AVAILABLE_MEMORY_TO_GENERATE_IMAGE = 50
staticprivate

◆ BG_COLOR

final String fr.softsf.canscan.ui.EncodedImage.BG_COLOR = "bgColor"
staticprivate

◆ BYTES_PER_KILOBYTE

final int fr.softsf.canscan.ui.EncodedImage.BYTES_PER_KILOBYTE = 1024
staticprivate

◆ CONFIG

final String fr.softsf.canscan.ui.EncodedImage.CONFIG = "config"
staticprivate

◆ DEFAULT_GAP_BETWEEN_LOGO_AND_MODULES

final double fr.softsf.canscan.ui.EncodedImage.DEFAULT_GAP_BETWEEN_LOGO_AND_MODULES = 0.9
staticprivate

◆ DRAW_MODULES

final String fr.softsf.canscan.ui.EncodedImage.DRAW_MODULES = "drawModules"
staticprivate

◆ DRAW_ROUNDED_FINDER_PATTERN_AT_PIXEL

final String fr.softsf.canscan.ui.EncodedImage.DRAW_ROUNDED_FINDER_PATTERN_AT_PIXEL
staticprivate
Initial value:
=
"drawRoundedFinderPatternAtPixel"

◆ DRAW_SQUARE_FINDER_PATTERN_AT_PIXEL

final String fr.softsf.canscan.ui.EncodedImage.DRAW_SQUARE_FINDER_PATTERN_AT_PIXEL
staticprivate
Initial value:
=
"drawSquareFinderPatternAtPixel"

◆ GENERATE_QR_CODE_IMAGE

final String fr.softsf.canscan.ui.EncodedImage.GENERATE_QR_CODE_IMAGE = "generateQrCodeImage"
staticprivate

◆ imageLock

final Object fr.softsf.canscan.ui.EncodedImage.imageLock = new Object()
private

◆ MATRIX

final String fr.softsf.canscan.ui.EncodedImage.MATRIX = "matrix"
staticprivate

◆ qrOriginal

BufferedImage fr.softsf.canscan.ui.EncodedImage.qrOriginal
private

◆ SHOULD_SKIP_MODULE

final String fr.softsf.canscan.ui.EncodedImage.SHOULD_SKIP_MODULE = "shouldSkipModule"
staticprivate

The documentation for this class was generated from the following file: