mirror of
https://github.com/zxing/zxing.git
synced 2024-11-13 14:34:08 -08:00
Refactor checking of write format in 1D writers
This commit is contained in:
parent
2d77cec7c8
commit
723b65fe3d
|
@ -17,11 +17,9 @@
|
||||||
package com.google.zxing.oned;
|
package com.google.zxing.oned;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
import com.google.zxing.EncodeHintType;
|
|
||||||
import com.google.zxing.WriterException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class renders CodaBar as {@code boolean[]}.
|
* This class renders CodaBar as {@code boolean[]}.
|
||||||
|
@ -36,16 +34,8 @@ public final class CodaBarWriter extends OneDimensionalCodeWriter {
|
||||||
private static final char DEFAULT_GUARD = START_END_CHARS[0];
|
private static final char DEFAULT_GUARD = START_END_CHARS[0];
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BitMatrix encode(String contents,
|
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||||
BarcodeFormat format,
|
return Collections.singleton(BarcodeFormat.CODABAR);
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
Map<EncodeHintType,?> hints) throws WriterException {
|
|
||||||
if (format != BarcodeFormat.CODABAR) {
|
|
||||||
throw new IllegalArgumentException("Can only encode CODABAR, but got " + format);
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.encode(contents, format, width, height, hints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -17,13 +17,11 @@
|
||||||
package com.google.zxing.oned;
|
package com.google.zxing.oned;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
import com.google.zxing.EncodeHintType;
|
|
||||||
import com.google.zxing.WriterException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This object renders a CODE128 code as a {@link BitMatrix}.
|
* This object renders a CODE128 code as a {@link BitMatrix}.
|
||||||
|
@ -61,15 +59,8 @@ public final class Code128Writer extends OneDimensionalCodeWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BitMatrix encode(String contents,
|
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||||
BarcodeFormat format,
|
return Collections.singleton(BarcodeFormat.CODE_128);
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
Map<EncodeHintType,?> hints) throws WriterException {
|
|
||||||
if (format != BarcodeFormat.CODE_128) {
|
|
||||||
throw new IllegalArgumentException("Can only encode CODE_128, but got " + format);
|
|
||||||
}
|
|
||||||
return super.encode(contents, format, width, height, hints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -17,11 +17,10 @@
|
||||||
package com.google.zxing.oned;
|
package com.google.zxing.oned;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
import com.google.zxing.EncodeHintType;
|
|
||||||
import com.google.zxing.WriterException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This object renders a CODE39 code as a {@link BitMatrix}.
|
* This object renders a CODE39 code as a {@link BitMatrix}.
|
||||||
|
@ -31,15 +30,8 @@ import java.util.Map;
|
||||||
public final class Code39Writer extends OneDimensionalCodeWriter {
|
public final class Code39Writer extends OneDimensionalCodeWriter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BitMatrix encode(String contents,
|
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||||
BarcodeFormat format,
|
return Collections.singleton(BarcodeFormat.CODE_39);
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
Map<EncodeHintType,?> hints) throws WriterException {
|
|
||||||
if (format != BarcodeFormat.CODE_39) {
|
|
||||||
throw new IllegalArgumentException("Can only encode CODE_39, but got " + format);
|
|
||||||
}
|
|
||||||
return super.encode(contents, format, width, height, hints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -16,26 +16,18 @@
|
||||||
package com.google.zxing.oned;
|
package com.google.zxing.oned;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
import com.google.zxing.EncodeHintType;
|
|
||||||
import com.google.zxing.WriterException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This object renders a CODE93 code as a BitMatrix
|
* This object renders a CODE93 code as a BitMatrix
|
||||||
*/
|
*/
|
||||||
public class Code93Writer extends OneDimensionalCodeWriter {
|
public class Code93Writer extends OneDimensionalCodeWriter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BitMatrix encode(String contents,
|
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||||
BarcodeFormat format,
|
return Collections.singleton(BarcodeFormat.CODE_93);
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
Map<EncodeHintType,?> hints) throws WriterException {
|
|
||||||
if (format != BarcodeFormat.CODE_93) {
|
|
||||||
throw new IllegalArgumentException("Can only encode CODE_93, but got " + format);
|
|
||||||
}
|
|
||||||
return super.encode(contents, format, width, height, hints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,12 +17,11 @@
|
||||||
package com.google.zxing.oned;
|
package com.google.zxing.oned;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
import com.google.zxing.EncodeHintType;
|
|
||||||
import com.google.zxing.FormatException;
|
import com.google.zxing.FormatException;
|
||||||
import com.google.zxing.WriterException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This object renders an EAN13 code as a {@link BitMatrix}.
|
* This object renders an EAN13 code as a {@link BitMatrix}.
|
||||||
|
@ -38,16 +37,8 @@ public final class EAN13Writer extends UPCEANWriter {
|
||||||
3; // end guard
|
3; // end guard
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BitMatrix encode(String contents,
|
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||||
BarcodeFormat format,
|
return Collections.singleton(BarcodeFormat.EAN_13);
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
Map<EncodeHintType,?> hints) throws WriterException {
|
|
||||||
if (format != BarcodeFormat.EAN_13) {
|
|
||||||
throw new IllegalArgumentException("Can only encode EAN_13, but got " + format);
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.encode(contents, format, width, height, hints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -17,12 +17,11 @@
|
||||||
package com.google.zxing.oned;
|
package com.google.zxing.oned;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
import com.google.zxing.EncodeHintType;
|
|
||||||
import com.google.zxing.FormatException;
|
import com.google.zxing.FormatException;
|
||||||
import com.google.zxing.WriterException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This object renders an EAN8 code as a {@link BitMatrix}.
|
* This object renders an EAN8 code as a {@link BitMatrix}.
|
||||||
|
@ -38,17 +37,8 @@ public final class EAN8Writer extends UPCEANWriter {
|
||||||
3; // end guard
|
3; // end guard
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BitMatrix encode(String contents,
|
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||||
BarcodeFormat format,
|
return Collections.singleton(BarcodeFormat.EAN_8);
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
Map<EncodeHintType,?> hints) throws WriterException {
|
|
||||||
if (format != BarcodeFormat.EAN_8) {
|
|
||||||
throw new IllegalArgumentException("Can only encode EAN_8, but got "
|
|
||||||
+ format);
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.encode(contents, format, width, height, hints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,11 +17,10 @@
|
||||||
package com.google.zxing.oned;
|
package com.google.zxing.oned;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
import com.google.zxing.EncodeHintType;
|
|
||||||
import com.google.zxing.WriterException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This object renders a ITF code as a {@link BitMatrix}.
|
* This object renders a ITF code as a {@link BitMatrix}.
|
||||||
|
@ -52,16 +51,8 @@ public final class ITFWriter extends OneDimensionalCodeWriter {
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BitMatrix encode(String contents,
|
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||||
BarcodeFormat format,
|
return Collections.singleton(BarcodeFormat.ITF);
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
Map<EncodeHintType,?> hints) throws WriterException {
|
|
||||||
if (format != BarcodeFormat.ITF) {
|
|
||||||
throw new IllegalArgumentException("Can only encode ITF, but got " + format);
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.encode(contents, format, width, height, hints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -22,6 +22,7 @@ import com.google.zxing.Writer;
|
||||||
import com.google.zxing.WriterException;
|
import com.google.zxing.WriterException;
|
||||||
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@ -60,6 +61,11 @@ public abstract class OneDimensionalCodeWriter implements Writer {
|
||||||
throw new IllegalArgumentException("Negative size is not allowed. Input: "
|
throw new IllegalArgumentException("Negative size is not allowed. Input: "
|
||||||
+ width + 'x' + height);
|
+ width + 'x' + height);
|
||||||
}
|
}
|
||||||
|
Collection<BarcodeFormat> supportedFormats = getSupportedWriteFormats();
|
||||||
|
if (supportedFormats != null && !supportedFormats.contains(format)) {
|
||||||
|
throw new IllegalArgumentException("Can only encode " + supportedFormats +
|
||||||
|
", but got " + format);
|
||||||
|
}
|
||||||
|
|
||||||
int sidesMargin = getDefaultMargin();
|
int sidesMargin = getDefaultMargin();
|
||||||
if (hints != null && hints.containsKey(EncodeHintType.MARGIN)) {
|
if (hints != null && hints.containsKey(EncodeHintType.MARGIN)) {
|
||||||
|
@ -70,6 +76,10 @@ public abstract class OneDimensionalCodeWriter implements Writer {
|
||||||
return renderResult(code, width, height, sidesMargin);
|
return renderResult(code, width, height, sidesMargin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return a byte array of horizontal pixels (0 = white, 1 = black)
|
* @return a byte array of horizontal pixels (0 = white, 1 = black)
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -16,12 +16,11 @@
|
||||||
|
|
||||||
package com.google.zxing.oned;
|
package com.google.zxing.oned;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
import com.google.zxing.EncodeHintType;
|
|
||||||
import com.google.zxing.FormatException;
|
import com.google.zxing.FormatException;
|
||||||
import com.google.zxing.WriterException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,16 +35,8 @@ public final class UPCEWriter extends UPCEANWriter {
|
||||||
6; // end guard
|
6; // end guard
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BitMatrix encode(String contents,
|
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||||
BarcodeFormat format,
|
return Collections.singleton(BarcodeFormat.UPC_E);
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
Map<EncodeHintType, ?> hints) throws WriterException {
|
|
||||||
if (format != BarcodeFormat.UPC_E) {
|
|
||||||
throw new IllegalArgumentException("Can only encode UPC_E, but got " + format);
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.encode(contents, format, width, height, hints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue