001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.getahead.dwrdemo.files;
017:
018: import java.awt.Font;
019: import java.awt.Graphics2D;
020: import java.awt.geom.AffineTransform;
021: import java.awt.image.AffineTransformOp;
022: import java.awt.image.BufferedImage;
023: import java.io.ByteArrayOutputStream;
024:
025: import org.directwebremoting.io.FileTransfer;
026: import org.getahead.dwrdemo.util.ColorUtil;
027:
028: import com.lowagie.text.Document;
029: import com.lowagie.text.Paragraph;
030: import com.lowagie.text.pdf.PdfWriter;
031:
032: /**
033: * A demonstration of uploading files and images
034: * @author Joe Walker [joe at getahead dot ltd dot uk]
035: */
036: public class UploadDownload {
037: /**
038: * Take 2 uploaded files and return an image based on them
039: * @param uploadImage The uploaded image
040: * @param uploadFile The uploaded file
041: * @param color The selected color
042: * @return A mangled image based on the 2 uploaded files
043: */
044: public BufferedImage uploadFiles(BufferedImage uploadImage,
045: String uploadFile, String color) {
046: uploadImage = scaleToSize(uploadImage);
047: uploadImage = grafitiTextOnImage(uploadImage, uploadFile, color);
048:
049: return uploadImage;
050: }
051:
052: /**
053: * Generates a PDF file with the given text
054: * http://itext.ugent.be/itext-in-action/
055: * @return A PDF file as a byte array
056: */
057: public FileTransfer downloadPdfFile(String contents)
058: throws Exception {
059: if (contents == null || contents.length() == 0) {
060: contents = "[BLANK]";
061: }
062:
063: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
064:
065: Document document = new Document();
066: PdfWriter.getInstance(document, buffer);
067:
068: document.addCreator("DWR.war using iText");
069: document.open();
070: document.add(new Paragraph(contents));
071: document.close();
072:
073: return new FileTransfer("example.pdf", "application/pdf",
074: buffer.toByteArray());
075: }
076:
077: /**
078: * Voodoo to scale the image to 200x200
079: * @param uploadImage The image to work on
080: * @return The altered image
081: */
082: private BufferedImage scaleToSize(BufferedImage uploadImage) {
083: AffineTransform atx = new AffineTransform();
084: atx.scale(200d / uploadImage.getWidth(), 200d / uploadImage
085: .getHeight());
086: // AffineTransformOp.TYPE_BILINEAR is very slow
087: AffineTransformOp afop = new AffineTransformOp(atx,
088: AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
089: uploadImage = afop.filter(uploadImage, null);
090: return uploadImage;
091: }
092:
093: /**
094: * And scrawl the text on the image in 10 rows of 21 chars
095: * @param uploadImage The image to work on
096: * @param uploadFile The text to write on the image
097: * @param color The selected color
098: * @return The altered image
099: */
100: private BufferedImage grafitiTextOnImage(BufferedImage uploadImage,
101: String uploadFile, String color) {
102: StringBuilder buffer = new StringBuilder();
103: while (buffer.length() < 200) {
104: buffer.append(" ");
105: buffer.append(uploadFile);
106: }
107:
108: Graphics2D g2d = uploadImage.createGraphics();
109: for (int row = 0; row < 10; row++) {
110: String output = null;
111: if (buffer.length() > (row + 1) * CHARS_PER_LINE) {
112: output = buffer.substring(row * CHARS_PER_LINE,
113: (row + 1) * CHARS_PER_LINE);
114: } else {
115: output = buffer.substring(row * CHARS_PER_LINE);
116: }
117:
118: g2d.setFont(new Font("SansSerif", Font.BOLD, 16));
119: g2d.setColor(ColorUtil.decodeHtmlColorString(color));
120: g2d.drawString(output, 5, (row + 1) * CHARS_PER_LINE);
121: }
122:
123: return uploadImage;
124: }
125:
126: /**
127: *
128: */
129: private static final int CHARS_PER_LINE = 21;
130: }
|