001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id$ */
019:
020: package image.writer;
021:
022: import java.awt.Color;
023: import java.awt.Graphics2D;
024: import java.awt.image.BufferedImage;
025: import java.io.File;
026: import java.io.IOException;
027: import java.io.OutputStream;
028:
029: import org.apache.commons.io.IOUtils;
030: import org.apache.xmlgraphics.image.writer.ImageWriter;
031: import org.apache.xmlgraphics.image.writer.ImageWriterParams;
032: import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
033: import org.apache.xmlgraphics.image.writer.MultiImageWriter;
034:
035: public class ImageWriterExample2 extends ImageWriterExample1 {
036:
037: private BufferedImage createAnImage(String compression, int pageNum) {
038: boolean monochrome = compression.startsWith("CCITT"); //CCITT is for 1bit b/w only
039:
040: BufferedImage bimg;
041: if (monochrome) {
042: bimg = new BufferedImage(400, 200,
043: BufferedImage.TYPE_BYTE_BINARY);
044: } else {
045: bimg = new BufferedImage(400, 200,
046: BufferedImage.TYPE_INT_RGB);
047: }
048:
049: Graphics2D g2d = bimg.createGraphics();
050: g2d.setBackground(Color.white);
051: g2d.clearRect(0, 0, 400, 200);
052: g2d.setColor(Color.black);
053:
054: //Paint something
055: paintSome(g2d, pageNum);
056:
057: return bimg;
058: }
059:
060: /**
061: * Creates a bitmap file. We paint a few things on a bitmap and then save the bitmap using
062: * an ImageWriter.
063: * @param outputFile the target file
064: * @param format the target format (a MIME type, ex. "image/png")
065: * @throws IOException In case of an I/O error
066: */
067: public void generateBitmapUsingJava2D(File outputFile, String format)
068: throws IOException {
069: //String compression = "CCITT T.6";
070: String compression = "PackBits";
071:
072: OutputStream out = new java.io.FileOutputStream(outputFile);
073: out = new java.io.BufferedOutputStream(out);
074: try {
075:
076: ImageWriter writer = ImageWriterRegistry.getInstance()
077: .getWriterFor(format);
078: ImageWriterParams params = new ImageWriterParams();
079: params.setCompressionMethod(compression);
080: params.setResolution(72);
081:
082: if (writer.supportsMultiImageWriter()) {
083: MultiImageWriter multiWriter = writer
084: .createMultiImageWriter(out);
085: multiWriter.writeImage(createAnImage(compression, 1),
086: params);
087: multiWriter.writeImage(createAnImage(compression, 2),
088: params);
089: multiWriter.close();
090: } else {
091: throw new UnsupportedOperationException(
092: "multi-page images not supported for " + format);
093: }
094:
095: } finally {
096: IOUtils.closeQuietly(out);
097: }
098: }
099:
100: /**
101: * Command-line interface
102: * @param args command-line arguments
103: */
104: public static void main(String[] args) {
105: try {
106: File targetDir;
107: if (args.length >= 1) {
108: targetDir = new File(args[0]);
109: } else {
110: targetDir = new File(".");
111: }
112: if (!targetDir.exists()) {
113: System.err.println("Target Directory does not exist: "
114: + targetDir);
115: }
116: File outputFile = new File(targetDir, "eps-example2.tif");
117: ImageWriterExample2 app = new ImageWriterExample2();
118: app.generateBitmapUsingJava2D(outputFile, "image/tiff");
119: System.out.println("File written: "
120: + outputFile.getCanonicalPath());
121: } catch (Exception e) {
122: e.printStackTrace();
123: }
124: }
125:
126: }
|