Created a WriterException class and updated a bunch of documentation.

git-svn-id: https://zxing.googlecode.com/svn/trunk@725 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin 2008-11-18 21:46:24 +00:00
parent 1357c87441
commit b5dbec4469
4 changed files with 58 additions and 8 deletions

View file

@ -21,16 +21,22 @@ import com.google.zxing.qrcode.QRCodeWriter;
import java.util.Hashtable;
/**
* This is a factory class which finds the appropriate Writer subclass for the BarcodeFormat
* requested and encodes the barcode with the supplied contents.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public final class MultiFormatWriter implements Writer {
public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width,
int height) throws Exception {
int height) throws WriterException {
return encode(contents, format, width, height, null);
}
public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height,
Hashtable hints) throws Exception {
Hashtable hints) throws WriterException {
if (format == BarcodeFormat.QR_CODE) {
return new QRCodeWriter().encode(contents, format, width, height, hints);

View file

@ -20,6 +20,11 @@ import com.google.zxing.common.ByteMatrix;
import java.util.Hashtable;
/**
* The base class for all objects which encode/generate a barcode image.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public interface Writer {
/**
@ -31,7 +36,8 @@ public interface Writer {
* @param height The preferred height in pixels
* @return The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
*/
ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height) throws Exception;
ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height)
throws WriterException;
/**
*
@ -43,6 +49,6 @@ public interface Writer {
* @return The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
*/
ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height, Hashtable hints)
throws Exception;
throws WriterException;
}

View file

@ -0,0 +1,31 @@
/*
* Copyright 2008 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;
/**
* A base class which covers the range of exceptions which may occur when encoding a barcode using
* the Writer framework.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public class WriterException extends Exception {
public WriterException(String message) {
super(message);
}
}

View file

@ -18,6 +18,7 @@ package com.google.zxing.qrcode;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.common.ByteMatrix;
import com.google.zxing.qrcode.encoder.ByteArray;
import com.google.zxing.qrcode.encoder.Encoder;
@ -25,14 +26,21 @@ import com.google.zxing.qrcode.encoder.QRCode;
import java.util.Hashtable;
/**
* This object renders a QR Code as a ByteMatrix 2D array of greyscale values.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public final class QRCodeWriter implements Writer {
public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height) {
public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height)
throws WriterException {
return encode(contents, format, width, height, null);
}
public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height, Hashtable hints) {
public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height,
Hashtable hints) throws WriterException {
if (contents == null || contents.length == 0) {
throw new IllegalArgumentException("Found empty contents");
@ -53,8 +61,7 @@ public final class QRCodeWriter implements Writer {
if (Encoder.Encode(new ByteArray(contents), errorCorrectionLevel, code)) {
return renderResult(code, width, height);
} else {
// TODO need a "WriterException" or something
throw new RuntimeException("Could not generate a QR Code");
throw new WriterException("Could not generate a QR Code");
}
}