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.Font;
024: import java.awt.Graphics2D;
025: import java.awt.font.TextAttribute;
026: import java.awt.image.BufferedImage;
027: import java.io.File;
028: import java.io.IOException;
029: import java.io.OutputStream;
030: import java.text.AttributedString;
031:
032: import org.apache.commons.io.IOUtils;
033: import org.apache.xmlgraphics.image.writer.ImageWriter;
034: import org.apache.xmlgraphics.image.writer.ImageWriterParams;
035: import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
036:
037: public class ImageWriterExample1 {
038:
039: /**
040: * Paints a few things on a Graphics2D instance.
041: * @param g2d the Graphics2D instance
042: * @param pageNum a page number
043: */
044: protected void paintSome(Graphics2D g2d, int pageNum) {
045: //Paint a bounding box
046: g2d.drawRect(0, 0, 400, 200);
047:
048: //A few rectangles rotated and with different color
049: Graphics2D copy = (Graphics2D) g2d.create();
050: int c = 12;
051: for (int i = 0; i < c; i++) {
052: float f = ((i + 1) / (float) c);
053: Color col = new Color(0.0f, 1 - f, 0.0f);
054: copy.setColor(col);
055: copy.fillRect(70, 90, 50, 50);
056: copy.rotate(-2 * Math.PI / (double) c, 70, 90);
057: }
058: copy.dispose();
059:
060: //Some text
061: copy = (Graphics2D) g2d.create();
062: copy.rotate(-0.25);
063: copy.setColor(Color.RED);
064: copy.setFont(new Font("sans-serif", Font.PLAIN, 36));
065: copy.drawString("Hello world!", 140, 140);
066: copy.setColor(Color.RED.darker());
067: copy.setFont(new Font("serif", Font.PLAIN, 36));
068: copy.drawString("Hello world!", 140, 180);
069: copy.dispose();
070:
071: //Try attributed text
072: AttributedString aString = new AttributedString(
073: "This is attributed text.");
074: aString.addAttribute(TextAttribute.FAMILY, "SansSerif");
075: aString.addAttribute(TextAttribute.FAMILY, "Serif", 8, 18);
076: aString.addAttribute(TextAttribute.FOREGROUND, Color.orange, 8,
077: 18);
078: g2d.drawString(aString.getIterator(), 250, 170);
079:
080: g2d.drawString("Page: " + pageNum, 250, 190);
081: }
082:
083: /**
084: * Creates a bitmap file. We paint a few things on a bitmap and then save the bitmap using
085: * an ImageWriter.
086: * @param outputFile the target file
087: * @param format the target format (a MIME type, ex. "image/png")
088: * @throws IOException In case of an I/O error
089: */
090: public void generateBitmapUsingJava2D(File outputFile, String format)
091: throws IOException {
092: //String compression = "CCITT T.6";
093: String compression = "PackBits";
094: boolean monochrome = compression.startsWith("CCITT"); //CCITT is for 1bit b/w only
095:
096: BufferedImage bimg;
097: if (monochrome) {
098: bimg = new BufferedImage(400, 200,
099: BufferedImage.TYPE_BYTE_BINARY);
100: } else {
101: bimg = new BufferedImage(400, 200,
102: BufferedImage.TYPE_INT_RGB);
103: }
104:
105: Graphics2D g2d = bimg.createGraphics();
106: g2d.setBackground(Color.white);
107: g2d.clearRect(0, 0, 400, 200);
108: g2d.setColor(Color.black);
109:
110: //Paint something
111: paintSome(g2d, 1);
112:
113: OutputStream out = new java.io.FileOutputStream(outputFile);
114: out = new java.io.BufferedOutputStream(out);
115: try {
116:
117: ImageWriter writer = ImageWriterRegistry.getInstance()
118: .getWriterFor(format);
119: ImageWriterParams params = new ImageWriterParams();
120: params.setCompressionMethod(compression);
121: params.setResolution(72);
122: writer.writeImage(bimg, out, params);
123:
124: } finally {
125: IOUtils.closeQuietly(out);
126: }
127: }
128:
129: /**
130: * Command-line interface
131: * @param args command-line arguments
132: */
133: public static void main(String[] args) {
134: try {
135: File targetDir;
136: if (args.length >= 1) {
137: targetDir = new File(args[0]);
138: } else {
139: targetDir = new File(".");
140: }
141: if (!targetDir.exists()) {
142: System.err.println("Target Directory does not exist: "
143: + targetDir);
144: }
145: File outputFile = new File(targetDir, "eps-example1.tif");
146: ImageWriterExample1 app = new ImageWriterExample1();
147: app.generateBitmapUsingJava2D(outputFile, "image/tiff");
148: System.out.println("File written: "
149: + outputFile.getCanonicalPath());
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: }
154:
155: }
|