/*
* Copyright 2011 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using ZXing.Common;
namespace ZXing.OneD
{
///
///
Encapsulates functionality and implementation that is common to one-dimensional barcodes.
/// dsbnatut@gmail.com (Kazuki Nishiura)
///
public abstract class OneDimensionalCodeWriter : Writer
{
///
/// Encode a barcode using the default settings.
///
/// The contents to encode in the barcode
/// The barcode format to generate
/// The preferred width in pixels
/// The preferred height in pixels
///
/// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
///
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height)
{
return encode(contents, format, width, height, null);
}
///
/// Encode the contents following specified format.
/// {@code width} and {@code height} are required size. This method may return bigger size
/// {@code BitMatrix} when specified size is too small. The user can set both {@code width} and
/// {@code height} to zero to get minimum size barcode. If negative value is set to {@code width}
/// or {@code height}, {@code IllegalArgumentException} is thrown.
///
public virtual BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
IDictionary hints)
{
if (String.IsNullOrEmpty(contents))
{
throw new ArgumentException("Found empty contents");
}
if (width < 0 || height < 0)
{
throw new ArgumentException("Negative size is not allowed. Input: "
+ width + 'x' + height);
}
int sidesMargin = DefaultMargin;
if (hints != null)
{
var sidesMarginInt = hints.ContainsKey(EncodeHintType.MARGIN) ? (int)hints[EncodeHintType.MARGIN] : (int?)null;
if (sidesMarginInt != null)
{
sidesMargin = sidesMarginInt.Value;
}
}
var code = encode(contents);
return renderResult(code, width, height, sidesMargin);
}
///
///
/// a byte array of horizontal pixels (0 = white, 1 = black)
private static BitMatrix renderResult(bool[] code, int width, int height, int sidesMargin)
{
int inputWidth = code.Length;
// Add quiet zone on both sides.
int fullWidth = inputWidth + sidesMargin;
int outputWidth = Math.Max(width, fullWidth);
int outputHeight = Math.Max(1, height);
int multiple = outputWidth / fullWidth;
int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
BitMatrix output = new BitMatrix(outputWidth, outputHeight);
for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple)
{
if (code[inputX])
{
output.setRegion(outputX, 0, multiple, outputHeight);
}
}
return output;
}
///
/// Appends the given pattern to the target array starting at pos.
///
/// encode black/white pattern into this array
/// position to start encoding at in target
/// lengths of black/white runs to encode
/// starting color - false for white, true for black
/// the number of elements added to target.
protected static int appendPattern(bool[] target, int pos, int[] pattern, bool startColor)
{
bool color = startColor;
int numAdded = 0;
foreach (int len in pattern)
{
for (int j = 0; j < len; j++)
{
target[pos++] = color;
}
numAdded += len;
color = !color; // flip color after each segment
}
return numAdded;
}
///
/// Gets the default margin.
///
virtual public int DefaultMargin
{
get
{
// CodaBar spec requires a side margin to be more than ten times wider than narrow space.
// This seems like a decent idea for a default for all formats.
return 10;
}
}
///
/// Encode the contents to bool array expression of one-dimensional barcode.
/// Start code and end code should be included in result, and side margins should not be included.
///
/// barcode contents to encode
/// a bool[] of horizontal pixels (false = white, true = black)
public abstract bool[] encode(String contents);
///
/// Calculates the checksum digit modulo10.
///
/// The contents.
///
public static String CalculateChecksumDigitModulo10(String contents)
{
var oddsum = 0;
var evensum = 0;
for (var index = contents.Length - 1; index >= 0; index -= 2)
{
oddsum += (contents[index] - '0');
}
for (var index = contents.Length - 2; index >= 0; index -= 2)
{
evensum += (contents[index] - '0');
}
return contents + ((10 - ((oddsum * 3 + evensum) % 10)) % 10);
}
}
}