com.google.zxing.Dimension Java Examples

The following examples show how to use com.google.zxing.Dimension. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SymbolInfo.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
public static SymbolInfo lookup(int dataCodewords,
                                SymbolShapeHint shape, 
                                Dimension minSize, 
                                Dimension maxSize, 
                                boolean fail) {
  for (SymbolInfo symbol : symbols) {
    if (shape == SymbolShapeHint.FORCE_SQUARE && symbol.rectangular) {
      continue;
    }
    if (shape == SymbolShapeHint.FORCE_RECTANGLE && !symbol.rectangular) {
      continue;
    }
    if (minSize != null
        && (symbol.getSymbolWidth() < minSize.getWidth()
        || symbol.getSymbolHeight() < minSize.getHeight())) {
      continue;
    }
    if (maxSize != null
        && (symbol.getSymbolWidth() > maxSize.getWidth()
        || symbol.getSymbolHeight() > maxSize.getHeight())) {
      continue;
    }
    if (dataCodewords <= symbol.dataCapacity) {
      return symbol;
    }
  }
  if (fail) {
    throw new IllegalArgumentException(
        "Can't find a symbol arrangement that matches the message. Data codewords: "
            + dataCodewords);
  }
  return null;
}
 
Example #2
Source File: SymbolInfo.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public static SymbolInfo lookup(int dataCodewords, SymbolShapeHint shape, Dimension minSize, Dimension maxSize,
		boolean fail) {
	for (SymbolInfo symbol : symbols) {
		if (shape == SymbolShapeHint.FORCE_SQUARE && symbol.rectangular) {
			continue;
		}
		if (shape == SymbolShapeHint.FORCE_RECTANGLE && !symbol.rectangular) {
			continue;
		}
		if (minSize != null && (symbol.getSymbolWidth() < minSize.getWidth()
				|| symbol.getSymbolHeight() < minSize.getHeight())) {
			continue;
		}
		if (maxSize != null && (symbol.getSymbolWidth() > maxSize.getWidth()
				|| symbol.getSymbolHeight() > maxSize.getHeight())) {
			continue;
		}
		if (dataCodewords <= symbol.dataCapacity) {
			return symbol;
		}
	}
	if (fail) {
		throw new IllegalArgumentException(
				"Can't find a symbol arrangement that matches the message. Data codewords: " + dataCodewords);
	}
	return null;
}
 
Example #3
Source File: SymbolInfo.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
public static SymbolInfo lookup(int dataCodewords,
                                SymbolShapeHint shape, 
                                Dimension minSize, 
                                Dimension maxSize, 
                                boolean fail) {
  for (SymbolInfo symbol : symbols) {
    if (shape == SymbolShapeHint.FORCE_SQUARE && symbol.rectangular) {
      continue;
    }
    if (shape == SymbolShapeHint.FORCE_RECTANGLE && !symbol.rectangular) {
      continue;
    }
    if (minSize != null
        && (symbol.getSymbolWidth() < minSize.getWidth()
        || symbol.getSymbolHeight() < minSize.getHeight())) {
      continue;
    }
    if (maxSize != null
        && (symbol.getSymbolWidth() > maxSize.getWidth()
        || symbol.getSymbolHeight() > maxSize.getHeight())) {
      continue;
    }
    if (dataCodewords <= symbol.dataCapacity) {
      return symbol;
    }
  }
  if (fail) {
    throw new IllegalArgumentException(
        "Can't find a symbol arrangement that matches the message. Data codewords: "
            + dataCodewords);
  }
  return null;
}
 
Example #4
Source File: SymbolInfo.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
public static SymbolInfo lookup(int dataCodewords,
                                SymbolShapeHint shape, 
                                Dimension minSize, 
                                Dimension maxSize, 
                                boolean fail) {
  for (SymbolInfo symbol : symbols) {
    if (shape == SymbolShapeHint.FORCE_SQUARE && symbol.rectangular) {
      continue;
    }
    if (shape == SymbolShapeHint.FORCE_RECTANGLE && !symbol.rectangular) {
      continue;
    }
    if (minSize != null
        && (symbol.getSymbolWidth() < minSize.getWidth()
        || symbol.getSymbolHeight() < minSize.getHeight())) {
      continue;
    }
    if (maxSize != null
        && (symbol.getSymbolWidth() > maxSize.getWidth()
        || symbol.getSymbolHeight() > maxSize.getHeight())) {
      continue;
    }
    if (dataCodewords <= symbol.dataCapacity) {
      return symbol;
    }
  }
  if (fail) {
    throw new IllegalArgumentException(
        "Can't find a symbol arrangement that matches the message. Data codewords: "
            + dataCodewords);
  }
  return null;
}
 
Example #5
Source File: SymbolInfo.java    From weex with Apache License 2.0 5 votes vote down vote up
public static SymbolInfo lookup(int dataCodewords,
                                SymbolShapeHint shape, 
                                Dimension minSize, 
                                Dimension maxSize, 
                                boolean fail) {
  for (SymbolInfo symbol : symbols) {
    if (shape == SymbolShapeHint.FORCE_SQUARE && symbol.rectangular) {
      continue;
    }
    if (shape == SymbolShapeHint.FORCE_RECTANGLE && !symbol.rectangular) {
      continue;
    }
    if (minSize != null
        && (symbol.getSymbolWidth() < minSize.getWidth()
        || symbol.getSymbolHeight() < minSize.getHeight())) {
      continue;
    }
    if (maxSize != null
        && (symbol.getSymbolWidth() > maxSize.getWidth()
        || symbol.getSymbolHeight() > maxSize.getHeight())) {
      continue;
    }
    if (dataCodewords <= symbol.dataCapacity) {
      return symbol;
    }
  }
  if (fail) {
    throw new IllegalArgumentException(
        "Can't find a symbol arrangement that matches the message. Data codewords: "
            + dataCodewords);
  }
  return null;
}
 
Example #6
Source File: DataMatrixWriter.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
		Map<EncodeHintType, ?> hints) {

	if (contents.isEmpty()) {
		throw new IllegalArgumentException("Found empty contents");
	}

	if (format != BarcodeFormat.DATA_MATRIX) {
		throw new IllegalArgumentException("Can only encode DATA_MATRIX, but got " + format);
	}

	if (width < 0 || height < 0) {
		throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
	}

	// Try to get force shape & min / max size
	SymbolShapeHint shape = SymbolShapeHint.FORCE_NONE;
	Dimension minSize = null;
	Dimension maxSize = null;
	if (hints != null) {
		SymbolShapeHint requestedShape = (SymbolShapeHint) hints.get(EncodeHintType.DATA_MATRIX_SHAPE);
		if (requestedShape != null) {
			shape = requestedShape;
		}
		Dimension requestedMinSize = (Dimension) hints.get(EncodeHintType.MIN_SIZE);
		if (requestedMinSize != null) {
			minSize = requestedMinSize;
		}
		Dimension requestedMaxSize = (Dimension) hints.get(EncodeHintType.MAX_SIZE);
		if (requestedMaxSize != null) {
			maxSize = requestedMaxSize;
		}
	}

	// 1. step: Data encodation
	String encoded = HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize);

	SymbolInfo symbolInfo = SymbolInfo.lookup(encoded.length(), shape, minSize, maxSize, true);

	// 2. step: ECC generation
	String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);

	// 3. step: Module placement in Matrix
	DefaultPlacement placement = new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(),
			symbolInfo.getSymbolDataHeight());
	placement.place();

	// 4. step: low-level encoding
	return encodeLowLevel(placement, symbolInfo);
}
 
Example #7
Source File: HighLevelEncoder.java    From ZXing-Orient with Apache License 2.0 4 votes vote down vote up
/**
 * Performs message encoding of a DataMatrix message using the algorithm described in annex P
 * of ISO/IEC 16022:2000(E).
 *
 * @param msg     the message
 * @param shape   requested shape. May be {@code SymbolShapeHint.FORCE_NONE},
 *                {@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.
 * @param minSize the minimum symbol size constraint or null for no constraint
 * @param maxSize the maximum symbol size constraint or null for no constraint
 * @return the encoded message (the char values range from 0 to 255)
 */
public static String encodeHighLevel(String msg,
                                     SymbolShapeHint shape, 
                                     Dimension minSize, 
                                     Dimension maxSize) {
  //the codewords 0..255 are encoded as Unicode characters
  Encoder[] encoders = {
      new ASCIIEncoder(), new C40Encoder(), new TextEncoder(), 
      new X12Encoder(), new EdifactEncoder(),  new Base256Encoder()
  };

  EncoderContext context = new EncoderContext(msg);
  context.setSymbolShape(shape);
  context.setSizeConstraints(minSize, maxSize);

  if (msg.startsWith(MACRO_05_HEADER) && msg.endsWith(MACRO_TRAILER)) {
    context.writeCodeword(MACRO_05);
    context.setSkipAtEnd(2);
    context.pos += MACRO_05_HEADER.length();
  } else if (msg.startsWith(MACRO_06_HEADER) && msg.endsWith(MACRO_TRAILER)) {
    context.writeCodeword(MACRO_06);
    context.setSkipAtEnd(2);
    context.pos += MACRO_06_HEADER.length();
  }

  int encodingMode = ASCII_ENCODATION; //Default mode
  while (context.hasMoreCharacters()) {
    encoders[encodingMode].encode(context);
    if (context.getNewEncoding() >= 0) {
      encodingMode = context.getNewEncoding();
      context.resetEncoderSignal();
    }
  }
  int len = context.getCodewordCount();
  context.updateSymbolInfo();
  int capacity = context.getSymbolInfo().getDataCapacity();
  if (len < capacity) {
    if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION) {
      context.writeCodeword('\u00fe'); //Unlatch (254)
    }
  }
  //Padding
  StringBuilder codewords = context.getCodewords();
  if (codewords.length() < capacity) {
    codewords.append(PAD);
  }
  while (codewords.length() < capacity) {
    codewords.append(randomize253State(PAD, codewords.length() + 1));
  }

  return context.getCodewords().toString();
}
 
Example #8
Source File: EncoderContext.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
public void setSizeConstraints(Dimension minSize, Dimension maxSize) {
	this.minSize = minSize;
	this.maxSize = maxSize;
}
 
Example #9
Source File: HighLevelEncoder.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
/**
 * Performs message encoding of a DataMatrix message using the algorithm
 * described in annex P of ISO/IEC 16022:2000(E).
 * 
 * @param msg
 *            the message
 * @param shape
 *            requested shape. May be {@code SymbolShapeHint.FORCE_NONE},
 *            {@code SymbolShapeHint.FORCE_SQUARE} or
 *            {@code SymbolShapeHint.FORCE_RECTANGLE}.
 * @param minSize
 *            the minimum symbol size constraint or null for no constraint
 * @param maxSize
 *            the maximum symbol size constraint or null for no constraint
 * @return the encoded message (the char values range from 0 to 255)
 */
public static String encodeHighLevel(String msg, SymbolShapeHint shape, Dimension minSize, Dimension maxSize) {
	// the codewords 0..255 are encoded as Unicode characters
	Encoder[] encoders = { new ASCIIEncoder(), new C40Encoder(), new TextEncoder(), new X12Encoder(),
			new EdifactEncoder(), new Base256Encoder() };

	EncoderContext context = new EncoderContext(msg);
	context.setSymbolShape(shape);
	context.setSizeConstraints(minSize, maxSize);

	if (msg.startsWith(MACRO_05_HEADER) && msg.endsWith(MACRO_TRAILER)) {
		context.writeCodeword(MACRO_05);
		context.setSkipAtEnd(2);
		context.pos += MACRO_05_HEADER.length();
	} else if (msg.startsWith(MACRO_06_HEADER) && msg.endsWith(MACRO_TRAILER)) {
		context.writeCodeword(MACRO_06);
		context.setSkipAtEnd(2);
		context.pos += MACRO_06_HEADER.length();
	}

	int encodingMode = ASCII_ENCODATION; // Default mode
	while (context.hasMoreCharacters()) {
		encoders[encodingMode].encode(context);
		if (context.getNewEncoding() >= 0) {
			encodingMode = context.getNewEncoding();
			context.resetEncoderSignal();
		}
	}
	int len = context.getCodewordCount();
	context.updateSymbolInfo();
	int capacity = context.getSymbolInfo().getDataCapacity();
	if (len < capacity) {
		if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION) {
			context.writeCodeword('\u00fe'); // Unlatch (254)
		}
	}
	// Padding
	StringBuilder codewords = context.getCodewords();
	if (codewords.length() < capacity) {
		codewords.append(PAD);
	}
	while (codewords.length() < capacity) {
		codewords.append(randomize253State(PAD, codewords.length() + 1));
	}

	return context.getCodewords().toString();
}
 
Example #10
Source File: DataMatrixWriter.java    From barcodescanner-lib-aar with MIT License 4 votes vote down vote up
@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }
  
  if (format != BarcodeFormat.DATA_MATRIX) {
    throw new IllegalArgumentException("Can only encode DATA_MATRIX, but got " + format);
  }
  
  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
  }

  // Try to get force shape & min / max size
  SymbolShapeHint shape = SymbolShapeHint.FORCE_NONE;
  Dimension minSize = null;
  Dimension maxSize = null;
  if (hints != null) {
    SymbolShapeHint requestedShape = (SymbolShapeHint) hints.get(EncodeHintType.DATA_MATRIX_SHAPE);
    if (requestedShape != null) {
      shape = requestedShape;
    }
    @SuppressWarnings("deprecation")
    Dimension requestedMinSize = (Dimension) hints.get(EncodeHintType.MIN_SIZE);
    if (requestedMinSize != null) {
      minSize = requestedMinSize;
    }
    @SuppressWarnings("deprecation")
    Dimension requestedMaxSize = (Dimension) hints.get(EncodeHintType.MAX_SIZE);
    if (requestedMaxSize != null) {
      maxSize = requestedMaxSize;
    }
  }


  //1. step: Data encodation
  String encoded = HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize);

  SymbolInfo symbolInfo = SymbolInfo.lookup(encoded.length(), shape, minSize, maxSize, true);

  //2. step: ECC generation
  String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);

  //3. step: Module placement in Matrix
  DefaultPlacement placement =
      new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
  placement.place();

  //4. step: low-level encoding
  return encodeLowLevel(placement, symbolInfo);
}
 
Example #11
Source File: EncoderContext.java    From barcodescanner-lib-aar with MIT License 4 votes vote down vote up
public void setSizeConstraints(Dimension minSize, Dimension maxSize) {
  this.minSize = minSize;
  this.maxSize = maxSize;
}
 
Example #12
Source File: HighLevelEncoder.java    From barcodescanner-lib-aar with MIT License 4 votes vote down vote up
/**
 * Performs message encoding of a DataMatrix message using the algorithm described in annex P
 * of ISO/IEC 16022:2000(E).
 *
 * @param msg     the message
 * @param shape   requested shape. May be {@code SymbolShapeHint.FORCE_NONE},
 *                {@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.
 * @param minSize the minimum symbol size constraint or null for no constraint
 * @param maxSize the maximum symbol size constraint or null for no constraint
 * @return the encoded message (the char values range from 0 to 255)
 */
public static String encodeHighLevel(String msg,
                                     SymbolShapeHint shape, 
                                     Dimension minSize, 
                                     Dimension maxSize) {
  //the codewords 0..255 are encoded as Unicode characters
  Encoder[] encoders = {
      new ASCIIEncoder(), new C40Encoder(), new TextEncoder(), 
      new X12Encoder(), new EdifactEncoder(),  new Base256Encoder()
  };

  EncoderContext context = new EncoderContext(msg);
  context.setSymbolShape(shape);
  context.setSizeConstraints(minSize, maxSize);

  if (msg.startsWith(MACRO_05_HEADER) && msg.endsWith(MACRO_TRAILER)) {
    context.writeCodeword(MACRO_05);
    context.setSkipAtEnd(2);
    context.pos += MACRO_05_HEADER.length();
  } else if (msg.startsWith(MACRO_06_HEADER) && msg.endsWith(MACRO_TRAILER)) {
    context.writeCodeword(MACRO_06);
    context.setSkipAtEnd(2);
    context.pos += MACRO_06_HEADER.length();
  }

  int encodingMode = ASCII_ENCODATION; //Default mode
  while (context.hasMoreCharacters()) {
    encoders[encodingMode].encode(context);
    if (context.getNewEncoding() >= 0) {
      encodingMode = context.getNewEncoding();
      context.resetEncoderSignal();
    }
  }
  int len = context.getCodewordCount();
  context.updateSymbolInfo();
  int capacity = context.getSymbolInfo().getDataCapacity();
  if (len < capacity) {
    if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION) {
      context.writeCodeword('\u00fe'); //Unlatch (254)
    }
  }
  //Padding
  StringBuilder codewords = context.getCodewords();
  if (codewords.length() < capacity) {
    codewords.append(PAD);
  }
  while (codewords.length() < capacity) {
    codewords.append(randomize253State(PAD, codewords.length() + 1));
  }

  return context.getCodewords().toString();
}
 
Example #13
Source File: DataMatrixWriter.java    From weex with Apache License 2.0 4 votes vote down vote up
@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }
  
  if (format != BarcodeFormat.DATA_MATRIX) {
    throw new IllegalArgumentException("Can only encode DATA_MATRIX, but got " + format);
  }
  
  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
  }

  // Try to get force shape & min / max size
  SymbolShapeHint shape = SymbolShapeHint.FORCE_NONE;
  Dimension minSize = null;
  Dimension maxSize = null;
  if (hints != null) {
    SymbolShapeHint requestedShape = (SymbolShapeHint) hints.get(EncodeHintType.DATA_MATRIX_SHAPE);
    if (requestedShape != null) {
      shape = requestedShape;
    }
    @SuppressWarnings("deprecation")
    Dimension requestedMinSize = (Dimension) hints.get(EncodeHintType.MIN_SIZE);
    if (requestedMinSize != null) {
      minSize = requestedMinSize;
    }
    @SuppressWarnings("deprecation")
    Dimension requestedMaxSize = (Dimension) hints.get(EncodeHintType.MAX_SIZE);
    if (requestedMaxSize != null) {
      maxSize = requestedMaxSize;
    }
  }


  //1. step: Data encodation
  String encoded = HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize);

  SymbolInfo symbolInfo = SymbolInfo.lookup(encoded.length(), shape, minSize, maxSize, true);

  //2. step: ECC generation
  String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);

  //3. step: Module placement in Matrix
  DefaultPlacement placement =
      new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
  placement.place();

  //4. step: low-level encoding
  return encodeLowLevel(placement, symbolInfo);
}
 
Example #14
Source File: EncoderContext.java    From weex with Apache License 2.0 4 votes vote down vote up
public void setSizeConstraints(Dimension minSize, Dimension maxSize) {
  this.minSize = minSize;
  this.maxSize = maxSize;
}
 
Example #15
Source File: HighLevelEncoder.java    From weex with Apache License 2.0 4 votes vote down vote up
/**
 * Performs message encoding of a DataMatrix message using the algorithm described in annex P
 * of ISO/IEC 16022:2000(E).
 *
 * @param msg     the message
 * @param shape   requested shape. May be {@code SymbolShapeHint.FORCE_NONE},
 *                {@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.
 * @param minSize the minimum symbol size constraint or null for no constraint
 * @param maxSize the maximum symbol size constraint or null for no constraint
 * @return the encoded message (the char values range from 0 to 255)
 */
public static String encodeHighLevel(String msg,
                                     SymbolShapeHint shape, 
                                     Dimension minSize, 
                                     Dimension maxSize) {
  //the codewords 0..255 are encoded as Unicode characters
  Encoder[] encoders = {
      new ASCIIEncoder(), new C40Encoder(), new TextEncoder(), 
      new X12Encoder(), new EdifactEncoder(),  new Base256Encoder()
  };

  EncoderContext context = new EncoderContext(msg);
  context.setSymbolShape(shape);
  context.setSizeConstraints(minSize, maxSize);

  if (msg.startsWith(MACRO_05_HEADER) && msg.endsWith(MACRO_TRAILER)) {
    context.writeCodeword(MACRO_05);
    context.setSkipAtEnd(2);
    context.pos += MACRO_05_HEADER.length();
  } else if (msg.startsWith(MACRO_06_HEADER) && msg.endsWith(MACRO_TRAILER)) {
    context.writeCodeword(MACRO_06);
    context.setSkipAtEnd(2);
    context.pos += MACRO_06_HEADER.length();
  }

  int encodingMode = ASCII_ENCODATION; //Default mode
  while (context.hasMoreCharacters()) {
    encoders[encodingMode].encode(context);
    if (context.getNewEncoding() >= 0) {
      encodingMode = context.getNewEncoding();
      context.resetEncoderSignal();
    }
  }
  int len = context.getCodewordCount();
  context.updateSymbolInfo();
  int capacity = context.getSymbolInfo().getDataCapacity();
  if (len < capacity) {
    if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION) {
      context.writeCodeword('\u00fe'); //Unlatch (254)
    }
  }
  //Padding
  StringBuilder codewords = context.getCodewords();
  if (codewords.length() < capacity) {
    codewords.append(PAD);
  }
  while (codewords.length() < capacity) {
    codewords.append(randomize253State(PAD, codewords.length() + 1));
  }

  return context.getCodewords().toString();
}
 
Example #16
Source File: DataMatrixWriter.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }
  
  if (format != BarcodeFormat.DATA_MATRIX) {
    throw new IllegalArgumentException("Can only encode DATA_MATRIX, but got " + format);
  }
  
  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
  }

  // Try to get force shape & min / max size
  SymbolShapeHint shape = SymbolShapeHint.FORCE_NONE;
  Dimension minSize = null;
  Dimension maxSize = null;
  if (hints != null) {
    SymbolShapeHint requestedShape = (SymbolShapeHint) hints.get(EncodeHintType.DATA_MATRIX_SHAPE);
    if (requestedShape != null) {
      shape = requestedShape;
    }
    @SuppressWarnings("deprecation")
    Dimension requestedMinSize = (Dimension) hints.get(EncodeHintType.MIN_SIZE);
    if (requestedMinSize != null) {
      minSize = requestedMinSize;
    }
    @SuppressWarnings("deprecation")
    Dimension requestedMaxSize = (Dimension) hints.get(EncodeHintType.MAX_SIZE);
    if (requestedMaxSize != null) {
      maxSize = requestedMaxSize;
    }
  }


  //1. step: Data encodation
  String encoded = HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize);

  SymbolInfo symbolInfo = SymbolInfo.lookup(encoded.length(), shape, minSize, maxSize, true);

  //2. step: ECC generation
  String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);

  //3. step: Module placement in Matrix
  DefaultPlacement placement =
      new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
  placement.place();

  //4. step: low-level encoding
  return encodeLowLevel(placement, symbolInfo);
}
 
Example #17
Source File: EncoderContext.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
public void setSizeConstraints(Dimension minSize, Dimension maxSize) {
  this.minSize = minSize;
  this.maxSize = maxSize;
}
 
Example #18
Source File: HighLevelEncoder.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
/**
 * Performs message encoding of a DataMatrix message using the algorithm described in annex P
 * of ISO/IEC 16022:2000(E).
 *
 * @param msg     the message
 * @param shape   requested shape. May be {@code SymbolShapeHint.FORCE_NONE},
 *                {@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.
 * @param minSize the minimum symbol size constraint or null for no constraint
 * @param maxSize the maximum symbol size constraint or null for no constraint
 * @return the encoded message (the char values range from 0 to 255)
 */
public static String encodeHighLevel(String msg,
                                     SymbolShapeHint shape, 
                                     Dimension minSize, 
                                     Dimension maxSize) {
  //the codewords 0..255 are encoded as Unicode characters
  Encoder[] encoders = {
      new ASCIIEncoder(), new C40Encoder(), new TextEncoder(), 
      new X12Encoder(), new EdifactEncoder(),  new Base256Encoder()
  };

  EncoderContext context = new EncoderContext(msg);
  context.setSymbolShape(shape);
  context.setSizeConstraints(minSize, maxSize);

  if (msg.startsWith(MACRO_05_HEADER) && msg.endsWith(MACRO_TRAILER)) {
    context.writeCodeword(MACRO_05);
    context.setSkipAtEnd(2);
    context.pos += MACRO_05_HEADER.length();
  } else if (msg.startsWith(MACRO_06_HEADER) && msg.endsWith(MACRO_TRAILER)) {
    context.writeCodeword(MACRO_06);
    context.setSkipAtEnd(2);
    context.pos += MACRO_06_HEADER.length();
  }

  int encodingMode = ASCII_ENCODATION; //Default mode
  while (context.hasMoreCharacters()) {
    encoders[encodingMode].encode(context);
    if (context.getNewEncoding() >= 0) {
      encodingMode = context.getNewEncoding();
      context.resetEncoderSignal();
    }
  }
  int len = context.getCodewordCount();
  context.updateSymbolInfo();
  int capacity = context.getSymbolInfo().getDataCapacity();
  if (len < capacity) {
    if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION) {
      context.writeCodeword('\u00fe'); //Unlatch (254)
    }
  }
  //Padding
  StringBuilder codewords = context.getCodewords();
  if (codewords.length() < capacity) {
    codewords.append(PAD);
  }
  while (codewords.length() < capacity) {
    codewords.append(randomize253State(PAD, codewords.length() + 1));
  }

  return context.getCodewords().toString();
}
 
Example #19
Source File: DataMatrixWriter.java    From ZXing-Orient with Apache License 2.0 4 votes vote down vote up
@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }
  
  if (format != BarcodeFormat.DATA_MATRIX) {
    throw new IllegalArgumentException("Can only encode DATA_MATRIX, but got " + format);
  }
  
  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
  }

  // Try to get force shape & min / max size
  SymbolShapeHint shape = SymbolShapeHint.FORCE_NONE;
  Dimension minSize = null;
  Dimension maxSize = null;
  if (hints != null) {
    SymbolShapeHint requestedShape = (SymbolShapeHint) hints.get(EncodeHintType.DATA_MATRIX_SHAPE);
    if (requestedShape != null) {
      shape = requestedShape;
    }
    Dimension requestedMinSize = (Dimension) hints.get(EncodeHintType.MIN_SIZE);
    if (requestedMinSize != null) {
      minSize = requestedMinSize;
    }
    Dimension requestedMaxSize = (Dimension) hints.get(EncodeHintType.MAX_SIZE);
    if (requestedMaxSize != null) {
      maxSize = requestedMaxSize;
    }
  }


  //1. step: Data encodation
  String encoded = HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize);

  SymbolInfo symbolInfo = SymbolInfo.lookup(encoded.length(), shape, minSize, maxSize, true);

  //2. step: ECC generation
  String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);

  //3. step: Module placement in Matrix
  DefaultPlacement placement =
      new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
  placement.place();

  //4. step: low-level encoding
  return encodeLowLevel(placement, symbolInfo);
}
 
Example #20
Source File: EncoderContext.java    From ZXing-Orient with Apache License 2.0 4 votes vote down vote up
public void setSizeConstraints(Dimension minSize, Dimension maxSize) {
  this.minSize = minSize;
  this.maxSize = maxSize;
}