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 */ 016 package org.opengion.hayabusa.servlet; 017 018 import org.opengion.fukurou.util.Closer ; 019 020 import java.awt.Color; 021 import java.awt.Font; 022 import java.awt.Graphics2D; 023 import java.awt.image.BufferedImage; 024 import java.util.Iterator; 025 import javax.imageio.ImageIO; 026 import javax.imageio.ImageWriter; 027 import javax.imageio.stream.ImageOutputStream; 028 029 import java.io.File; 030 import java.io.IOException; 031 import javax.servlet.ServletException; 032 import javax.servlet.ServletConfig; 033 import javax.servlet.http.HttpServlet; 034 import javax.servlet.http.HttpServletRequest; 035 import javax.servlet.http.HttpServletResponse; 036 import 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 */ 068 public 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 ファイルで?lt;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 // ServletConfig config = getServletConfig(); 182 183 imageFile = config.getInitParameter("imageFile"); 184 } 185 186 /** 187 * PNGイメージの透過色?を行います? 188 * 189 * 引数のファイル(PNG)を読取り、白色を?過色に変換後?セーブします? 190 * ただし?PNG形式で透過をサポ?トして?のは、IE7,Firefox,opera 等で? 191 * IE6 は未サポ??グレーにな?です? 192 * 193 * Usage: java org.opengion.hayabusa.servlet.MakeImage IN_FILE OUT_FILE 194 * 195 * @param args コマンド引数配? 196 * @throws IOException 入出力エラーが発生したと? 197 */ 198 public static void main( final String[] args ) throws IOException { 199 200 BufferedImage org = ImageIO.read( new File( args[0] ) ); 201 202 int wd = org.getWidth(); 203 int ht = org.getHeight(); 204 BufferedImage dst = new BufferedImage(wd, ht, BufferedImage.TYPE_INT_ARGB); 205 for(int y=0; y<ht; y++) { 206 for(int x=0; x<wd; x++) { 207 if(org.getRGB(x, y) == 0xFFFFFFFF) { //白 208 dst.setRGB(x, y, 0); //透? 209 } 210 else { 211 dst.setRGB(x, y, org.getRGB(x, y)); 212 } 213 } 214 } 215 ImageIO.write( dst,"png",new File( args[1] ) ); 216 } 217 }