From 5403af5906c8d241e99f2e879a600a9cedc90eed Mon Sep 17 00:00:00 2001 From: dswitkin Date: Tue, 18 Nov 2008 21:46:24 +0000 Subject: [PATCH] Created a WriterException class and updated a bunch of documentation. git-svn-id: http://zxing.googlecode.com/svn/trunk@725 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../com/google/zxing/MultiFormatWriter.java | 10 ++++-- core/src/com/google/zxing/Writer.java | 10 ++++-- .../src/com/google/zxing/WriterException.java | 31 +++++++++++++++++++ .../com/google/zxing/qrcode/QRCodeWriter.java | 15 ++++++--- 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 core/src/com/google/zxing/WriterException.java diff --git a/core/src/com/google/zxing/MultiFormatWriter.java b/core/src/com/google/zxing/MultiFormatWriter.java index 2913de1a..a57ea7b1 100644 --- a/core/src/com/google/zxing/MultiFormatWriter.java +++ b/core/src/com/google/zxing/MultiFormatWriter.java @@ -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); diff --git a/core/src/com/google/zxing/Writer.java b/core/src/com/google/zxing/Writer.java index 21fcc3f1..936f1ddc 100644 --- a/core/src/com/google/zxing/Writer.java +++ b/core/src/com/google/zxing/Writer.java @@ -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; } diff --git a/core/src/com/google/zxing/WriterException.java b/core/src/com/google/zxing/WriterException.java new file mode 100644 index 00000000..cf1ec160 --- /dev/null +++ b/core/src/com/google/zxing/WriterException.java @@ -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); + } + +} diff --git a/core/src/com/google/zxing/qrcode/QRCodeWriter.java b/core/src/com/google/zxing/qrcode/QRCodeWriter.java index f223935c..4c5c6e21 100644 --- a/core/src/com/google/zxing/qrcode/QRCodeWriter.java +++ b/core/src/com/google/zxing/qrcode/QRCodeWriter.java @@ -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"); } } -- 2.20.1