001/* 002 * Copyright (c) 2009 The openGion Project. 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, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.hayabusa.servlet; 017 018import org.opengion.fukurou.util.Closer ; 019 020import java.awt.Color; 021import java.awt.Font; 022import java.awt.Graphics2D; 023import java.awt.image.BufferedImage; 024import java.util.Iterator; 025import javax.imageio.ImageIO; 026import javax.imageio.ImageWriter; 027import javax.imageio.stream.ImageOutputStream; 028 029import java.io.File; 030import java.io.IOException; 031import javax.servlet.ServletException; 032import javax.servlet.ServletConfig; 033import javax.servlet.http.HttpServlet; 034import javax.servlet.http.HttpServletRequest; 035import javax.servlet.http.HttpServletResponse; 036import javax.servlet.ServletOutputStream; 037 038 039/** 040 * 画像イメージに、文字列を動的に合成作成する、サーブレットです。 041 * 042 * 画像イメージを読取り、そこに、引数のテキスト文字列を合成します。 043 * 元は、googleMap のマーカーに、マーカー番号を合成する為に作られました。 044 * 045 * 一般的なサーブレットと同様に、デプロイメント・ディスクリプタ WEB-INF/web.xml に、 046 * servlet 要素と そのマッピング(servlet-mapping)を定義する必要があります。 047 * 048 * <servlet> 049 * <servlet-name>makeImage</servlet-name> 050 * <servlet-class>org.opengion.hayabusa.servlet.MakeImage</servlet-class> 051 * </servlet> 052 * 053 * <servlet-mapping> 054 * <servlet-name>makeImage</servlet-name> 055 * <url-pattern>/jsp/makeImage</url-pattern> 056 * </servlet-mapping> 057 * 058 * 一般には、http://サーバー:ポート/システムID/jsp/makeImage?text=番号 059 * 形式のURL でアクセスします。 060 * 061 * @og.rev 3.8.1.1 (2005/11/21) 新規追加 062 * @og.group その他機能 063 * 064 * @version 0.9.0 2000/10/17 065 * @author Kazuhiko Hasegawa 066 * @since JDK1.1, 067 */ 068public class MakeImage extends HttpServlet { 069 private static final long serialVersionUID = 400020050131L ; 070 071 private static final String FORM_NAME = "png" ; // jpg,BMP,bmp,JPG,wbmp,jpeg,png,PNG,JPEG,WBMP,GIF,gif 072 private String imageFile = null; 073 074 /** 075 * GET メソッドが呼ばれたときに実行します。 076 * 077 * 処理は、doPost へ振りなおしています。 078 * 079 * @param request HttpServletRequestオブジェクト 080 * @param response HttpServletResponseオブジェクト 081 * 082 * @og.rev 3.8.1.2 (2005/12/19) 半角カナ-全角カナ変換機能の追加 083 * 084 * @throws ServletException サーブレット関係のエラーが発生した場合、throw されます。 085 * @throws IOException 入出力エラーが発生したとき 086 */ 087 @Override 088 public void doGet( final HttpServletRequest request, final HttpServletResponse response ) 089 throws ServletException, IOException { 090 doPost( request,response ); 091 } 092 093 /** 094 * POST メソッドが呼ばれたときに実行します。 095 * 096 * @param request HttpServletRequestオブジェクト 097 * @param response HttpServletResponseオブジェクト 098 * 099 * @throws ServletException サーブレット関係のエラーが発生した場合、throw されます。 100 * @throws IOException 入出力エラーが発生したとき 101 */ 102 @Override 103 public void doPost( final HttpServletRequest request, final HttpServletResponse response ) 104 throws ServletException, IOException { 105 106 String text = request.getParameter( "text" ); 107 108 // contentTypeを出力 109 response.setContentType( "image/" + FORM_NAME ); 110 111 ServletOutputStream out = null; 112 try { 113 out = response.getOutputStream(); 114 BufferedImage img = createImage( text ); 115 // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 116 // encoder.encode( img ); 117 // out.flush(); 118 119 Iterator<ImageWriter> ite = ImageIO.getImageWritersByFormatName( FORM_NAME ); // 4.3.3.6 (2008/11/15) Generics警告対応 120 ImageWriter writer = ite.next(); // 4.3.3.6 (2008/11/15) Generics警告対応 121 ImageOutputStream ios = ImageIO.createImageOutputStream( out ); 122 writer.setOutput( ios ); 123 writer.write( img ); 124 out.flush(); 125 ios.close(); 126 127 // ImageIO.write( img,FORM_NAME,new File( "G:/webapps/gf/jsp/GF7010/test" + FORM_NAME ) ); 128 } 129 finally { 130 Closer.ioClose( out ); // 4.0.0 (2006/01/31) close 処理時の IOException を無視 131 } 132 } 133 134 /** 135 * イメージの合成処理を行います。 136 * 137 * @param text 合成するテキスト 138 * 139 * @return イメージの合成されたBufferedImageオブジェクト 140 * @throws IOException 入出力エラーが発生したとき 141 */ 142 private BufferedImage createImage( final String text ) throws IOException { 143 // イメージの作成 144 // BufferedImage image = new BufferedImage(25, 25, BufferedImage.TYPE_INT_ARGB); 145 146 BufferedImage image = ImageIO.read( new File( imageFile ) ); 147 Graphics2D gph = (Graphics2D)image.getGraphics(); 148 149 int xsp = (text.length() == 1) ? 8 : 2 ; 150 151 // gph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 152 // RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 153 // gph.setColor(new Color(255,255,255)); 154 // gph.fillRect(0,0,25,25); 155 gph.setFont(new Font("Serif", Font.BOLD, 14)); 156 gph.setColor(new Color(0,0,255)); 157 gph.drawString(text, xsp, 15); 158 // gph.setColor(new Color(0,255,0)); 159 // gph.drawOval(2,2,22,22); 160 161 gph.dispose(); 162 163 return image; 164 } 165 166 /** 167 * Servlet の 初期値設定を行います。 168 * 169 * WEB-INF/web.xml ファイルで、<servlet> タグ内で初期値設定を行います。 170 * <init-param> 171 * <param-name>imageFile</param-name> 172 * <param-value>G:/webapps/gf/jsp/GF7010/mark.png</param-value> 173 * </init-param> 174 * 175 * @param config ServletConfigオブジェクト 176 */ 177 @Override 178 public void init( final ServletConfig config ) throws ServletException { 179 super.init( config ); 180 181 imageFile = config.getInitParameter("imageFile"); 182 } 183 184 /** 185 * PNGイメージの透過色指定を行います。 186 * 187 * 引数のファイル(PNG)を読取り、白色を透過色に変換後、セーブします。 188 * ただし、PNG形式で透過をサポートしているのは、IE7,Firefox,opera 等で、 189 * IE6 は未サポート(グレーになる)です。 190 * 191 * Usage: java org.opengion.hayabusa.servlet.MakeImage IN_FILE OUT_FILE 192 * 193 * @param args コマンド引数配列 194 * @throws IOException 入出力エラーが発生したとき 195 */ 196 public static void main( final String[] args ) throws IOException { 197 198 BufferedImage org = ImageIO.read( new File( args[0] ) ); 199 200 int wd = org.getWidth(); 201 int ht = org.getHeight(); 202 BufferedImage dst = new BufferedImage(wd, ht, BufferedImage.TYPE_INT_ARGB); 203 for(int y=0; y<ht; y++) { 204 for(int x=0; x<wd; x++) { 205 if(org.getRGB(x, y) == 0xFFFFFFFF) { //白 206 dst.setRGB(x, y, 0); //透明 207 } 208 else { 209 dst.setRGB(x, y, org.getRGB(x, y)); 210 } 211 } 212 } 213 ImageIO.write( dst,"png",new File( args[1] ) ); 214 } 215}