org.hcilab.btprinter.abstractionlayer
Class ImageHandler

java.lang.Object
  extended by org.hcilab.btprinter.abstractionlayer.ImageHandler

public abstract class ImageHandler
extends java.lang.Object

This class provides an abstraction layer for the image processing as image handling (e.g. raw pixel access) may be different on different platforms. It defines methods to provide a per line access to the image data in a format that is understood by the printer. These methods have to be implemented for each supported platform separatly.

The intention of the per line access is to save memory by not keeping the whole processed image. The driver can request line by line and can send them directly to the printer.

This class also holds two shared methods that can be invoked from the platform specific implementation to process and compress the image.

ToDo

Version:
1.0.0
Author:
Dominik Schmidt

Field Summary
static int MAX_IMG_HEIGHT
          Maximum image height supported by the printer.
static int MAX_IMG_WIDTH
          Maximum image width supported by the printer.
 
Constructor Summary
ImageHandler()
           
 
Method Summary
 byte[] compressImageLine(byte[] imgLine)
          Compresses a line of an image.
abstract  int getLineCount()
          Provides the number of lines of the current image (meaning the image height) for user interface purposes.
abstract  byte[] getNextLine()
          Returns the next line of the image in a compressed format (see also compressImageLine) and moves the internal line pointer forward by one line.
abstract  boolean hasNextLine()
          Checks if there are more lines to be processed in the current image.
abstract  void initialize(java.lang.Object imgObject)
          Initializes the current image handler meaning that the image is taken and compressed by request (getNextLine).
 byte[] processImageLine(byte[] pixels)
          Converts a line of an image.
abstract  void setLogHandler(LogHandler logHandler)
          Sets the log handler.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_IMG_HEIGHT

public static final int MAX_IMG_HEIGHT
Maximum image height supported by the printer.

See Also:
Constant Field Values

MAX_IMG_WIDTH

public static final int MAX_IMG_WIDTH
Maximum image width supported by the printer.

See Also:
Constant Field Values
Constructor Detail

ImageHandler

public ImageHandler()
Method Detail

compressImageLine

public byte[] compressImageLine(byte[] imgLine)
Compresses a line of an image. In fact, it just compresses a byte array. It actually does not matter what the contents is for the compression as such. The TIFF pack bits algorithm is used.

If the output of the processImageLine method is taken as input for this method, the output array of this method is understood by the printer as an image line and therefore can directly be sent to it (after adding sequence control codes).

Parameters:
imgLine - Processed line of image
Returns:
Compressed line of image

getLineCount

public abstract int getLineCount()
                          throws java.lang.IllegalStateException
Provides the number of lines of the current image (meaning the image height) for user interface purposes.

Returns:
Number of lines in image
Throws:
java.lang.IllegalStateException - If image handler has not been initialized yet

getNextLine

public abstract byte[] getNextLine()
                            throws java.lang.IllegalStateException,
                                   java.lang.IllegalArgumentException,
                                   java.util.NoSuchElementException
Returns the next line of the image in a compressed format (see also compressImageLine) and moves the internal line pointer forward by one line.

Returns:
Next line of the image
Throws:
java.lang.IllegalStateException - If image handler has not been initialized yet
java.lang.IllegalArgumentException - If the image dimension is not supported (see processImage)
java.util.NoSuchElementException - If there are no more lines

hasNextLine

public abstract boolean hasNextLine()
                             throws java.lang.IllegalStateException
Checks if there are more lines to be processed in the current image.

Returns:
True, if there are more lines left
Throws:
java.lang.IllegalStateException - If image handler has not been initialized yet

initialize

public abstract void initialize(java.lang.Object imgObject)
                         throws java.lang.IllegalArgumentException
Initializes the current image handler meaning that the image is taken and compressed by request (getNextLine). The actual type of the image object is platform specific.

Parameters:
imgObject - Object that represents an image (platform specific)
Throws:
java.lang.IllegalArgumentException - If type of image is not recognized or if dimension is not supported

processImageLine

public byte[] processImageLine(byte[] pixels)
                        throws java.lang.IllegalArgumentException
Converts a line of an image. The input line has to be stored in an byte array whereas each byte value represents the brightness value of one pixel (reaching from zero to 255).

The output consists of an byte array where each byte holds eight pixels which can be either black or white. The decision wether an input pixel is considered being black or white is made by the threshold value (here: 128). The direction of the line is reversed as required by the printer.

The output has to be forwarded to the compressImageLine method as the printer will not understand the byte array returned by this method.

Right now there are only images supported where the pixel number per line is a multiple of 8. If the number of pixels is smaller than the printable area, white pixels will be added to the left. The image height cannot be checked here as the processing is done by line.

Parameters:
pixels - Line of source image
Returns:
Processed line of image
Throws:
java.lang.IllegalArgumentException - If the image dimension is not supported

setLogHandler

public abstract void setLogHandler(LogHandler logHandler)
Sets the log handler. This has to be done at least once.

Parameters:
logHandler - Log handler