1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* $Id: ImageWriterExample2.java 750418 2009-03-05 11:03:54Z vhennebert $ */ 19 20 package image.writer; 21 22 import java.awt.Color; 23 import java.awt.Graphics2D; 24 import java.awt.image.BufferedImage; 25 import java.io.File; 26 import java.io.IOException; 27 import java.io.OutputStream; 28 29 import org.apache.commons.io.IOUtils; 30 import org.apache.xmlgraphics.image.writer.ImageWriter; 31 import org.apache.xmlgraphics.image.writer.ImageWriterParams; 32 import org.apache.xmlgraphics.image.writer.ImageWriterRegistry; 33 import org.apache.xmlgraphics.image.writer.MultiImageWriter; 34 35 public class ImageWriterExample2 extends ImageWriterExample1 { 36 createAnImage(String compression, int pageNum)37 private BufferedImage createAnImage(String compression, int pageNum) { 38 boolean monochrome = compression.startsWith("CCITT"); //CCITT is for 1bit b/w only 39 40 BufferedImage bimg; 41 if (monochrome) { 42 bimg = new BufferedImage(400, 200, BufferedImage.TYPE_BYTE_BINARY); 43 } else { 44 bimg = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB); 45 } 46 47 Graphics2D g2d = bimg.createGraphics(); 48 g2d.setBackground(Color.white); 49 g2d.clearRect(0, 0, 400, 200); 50 g2d.setColor(Color.black); 51 52 //Paint something 53 paintSome(g2d, pageNum); 54 55 return bimg; 56 } 57 58 /** 59 * Creates a bitmap file. We paint a few things on a bitmap and then save the bitmap using 60 * an ImageWriter. 61 * @param outputFile the target file 62 * @param format the target format (a MIME type, ex. "image/png") 63 * @throws IOException In case of an I/O error 64 */ generateBitmapUsingJava2D(File outputFile, String format)65 public void generateBitmapUsingJava2D(File outputFile, String format) 66 throws IOException { 67 //String compression = "CCITT T.6"; 68 String compression = "PackBits"; 69 70 OutputStream out = new java.io.FileOutputStream(outputFile); 71 out = new java.io.BufferedOutputStream(out); 72 try { 73 74 ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(format); 75 ImageWriterParams params = new ImageWriterParams(); 76 params.setCompressionMethod(compression); 77 params.setResolution(72); 78 79 if (writer.supportsMultiImageWriter()) { 80 MultiImageWriter multiWriter = writer.createMultiImageWriter(out); 81 multiWriter.writeImage(createAnImage(compression, 1), params); 82 multiWriter.writeImage(createAnImage(compression, 2), params); 83 multiWriter.close(); 84 } else { 85 throw new UnsupportedOperationException("multi-page images not supported for " 86 + format); 87 } 88 89 } finally { 90 IOUtils.closeQuietly(out); 91 } 92 } 93 94 /** 95 * Command-line interface 96 * @param args command-line arguments 97 */ main(String[] args)98 public static void main(String[] args) { 99 try { 100 File targetDir; 101 if (args.length >= 1) { 102 targetDir = new File(args[0]); 103 } else { 104 targetDir = new File("."); 105 } 106 if (!targetDir.exists()) { 107 System.err.println("Target Directory does not exist: " + targetDir); 108 } 109 File outputFile = new File(targetDir, "eps-example2.tif"); 110 ImageWriterExample2 app = new ImageWriterExample2(); 111 app.generateBitmapUsingJava2D(outputFile, "image/tiff"); 112 System.out.println("File written: " + outputFile.getCanonicalPath()); 113 } catch (Exception e) { 114 e.printStackTrace(); 115 } 116 } 117 118 } 119