Issue 1263 refactor PDF417 Writer interface

git-svn-id: https://zxing.googlecode.com/svn/trunk@2289 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-05-10 13:00:29 +00:00
parent 9fa7d9aba4
commit 6ea8a2e6b0
3 changed files with 114 additions and 28 deletions

View file

@ -24,13 +24,32 @@ package com.google.zxing;
public enum EncodeHintType {
/**
* Specifies what degree of error correction to use, for example in QR Codes (type Integer).
* Specifies what degree of error correction to use, for example in QR Codes.
* Type depends on the encoder. For example for QR codes it's type
* {@link com.google.zxing.qrcode.decoder.ErrorCorrectionLevel ErrorCorrectionLevel}.
*/
ERROR_CORRECTION,
/**
* Specifies what character encoding to use where applicable (type String)
* Specifies what character encoding to use where applicable (type {@link String})
*/
CHARACTER_SET,
/**
* Specifies whether to use compact mode for PDF417 (type {@link Boolean}).
*/
PDF417_COMPACT,
/**
* Specifies what compaction mode to use for PDF417 (type
* {@link com.google.zxing.pdf417.encoder.Compaction Compaction}).
*/
PDF417_COMPACTION,
/**
* Specifies the minimum and maximum number of rows and columns for PDF417 (type
* {@link com.google.zxing.pdf417.encoder.Dimensions Dimensions}).
*/
PDF417_DIMENSIONS,
}

View file

@ -0,0 +1,54 @@
/*
* Copyright 2012 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.
*/
package com.google.zxing.pdf417.encoder;
/**
* Data object to specify the minimum and maximum number of rows and columns for a PDF417 barcode.
*
* @author qwandor@google.com (Andrew Walbran)
*/
public final class Dimensions {
private final int minCols;
private final int maxCols;
private final int minRows;
private final int maxRows;
public Dimensions(int minCols, int maxCols, int minRows, int maxRows) {
this.minCols = minCols;
this.maxCols = maxCols;
this.minRows = minRows;
this.maxRows = maxRows;
}
public int getMinCols() {
return minCols;
}
public int getMaxCols() {
return maxCols;
}
public int getMinRows() {
return minRows;
}
public int getMaxRows() {
return maxRows;
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright 2011 ZXing authors
* Copyright 2012 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,10 +22,12 @@ import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.EnumMap;
import java.util.Map;
/**
* @author Jacob Haynes
* @author qwandor@google.com (Andrew Walbran)
*/
public final class PDF417Writer implements Writer {
@ -35,7 +37,29 @@ public final class PDF417Writer implements Writer {
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
return encode(contents, format, width, height);
if (format != BarcodeFormat.PDF_417) {
throw new IllegalArgumentException("Can only encode PDF_417, but got " + format);
}
PDF417 encoder = new PDF417();
if (hints != null) {
if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) {
encoder.setCompact((Boolean) hints.get(EncodeHintType.PDF417_COMPACT));
}
if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) {
encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION));
}
if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) {
Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS);
encoder.setDimensions(dimensions.getMaxCols(),
dimensions.getMinCols(),
dimensions.getMaxRows(),
dimensions.getMinRows());
}
}
return bitMatrixFromEncoder(encoder, contents, width, height);
}
@Override
@ -43,10 +67,14 @@ public final class PDF417Writer implements Writer {
BarcodeFormat format,
int width,
int height) throws WriterException {
PDF417 encoder = initializeEncoder(format, false);
return bitMatrixFromEncoder(encoder, contents, width, height);
return encode(contents, format, width, height, null);
}
/**
* @deprecated Use {@link #encode(String, BarcodeFormat, int, int, Map)} instead, with hints to
* specify the encoding options.
*/
@Deprecated
public BitMatrix encode(String contents,
BarcodeFormat format,
boolean compact,
@ -57,26 +85,11 @@ public final class PDF417Writer implements Writer {
int minRows,
int maxRows,
Compaction compaction) throws WriterException {
PDF417 encoder = initializeEncoder(format, compact);
// Set options: dimensions and byte compaction
encoder.setDimensions(maxCols, minCols, maxRows, minRows);
encoder.setCompaction(compaction);
return bitMatrixFromEncoder(encoder, contents, width, height);
}
/**
* Initializes the encoder based on the format (whether it's compact or not)
*/
private static PDF417 initializeEncoder(BarcodeFormat format, boolean compact) {
if (format != BarcodeFormat.PDF_417) {
throw new IllegalArgumentException("Can only encode PDF_417, but got " + format);
}
PDF417 encoder = new PDF417();
encoder.setCompact(compact);
return encoder;
Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);
hints.put(EncodeHintType.PDF417_COMPACT, compact);
hints.put(EncodeHintType.PDF417_COMPACTION, compaction);
hints.put(EncodeHintType.PDF417_DIMENSIONS, new Dimensions(maxCols, minCols, maxRows, minRows));
return encode(contents, format, width, height, hints);
}
/**
@ -126,10 +139,10 @@ public final class PDF417Writer implements Writer {
* @return BitMatrix of the input
*/
private static BitMatrix bitMatrixFrombitArray(byte[][] input) {
//Creates a small whitespace boarder around the barcode
// Creates a small whitespace border around the barcode
int whiteSpace = 30;
//Creates the bitmatrix with extra space for whtespace
// Creates the bitmatrix with extra space for whitespace
BitMatrix output = new BitMatrix(input.length + 2 * whiteSpace, input[0].length + 2 * whiteSpace);
output.clear();
for (int ii = 0; ii < input.length; ii++) {