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);
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 {
/**
* @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;
/**
*
* @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;
}
--- /dev/null
+/*
+ * 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);
+ }
+
+}
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;
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");
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");
}
}