免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3683 | 回复: 0
打印 上一主题 下一主题

use HTTPClient for loading image from URL [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-21 09:02 |只看该作者 |倒序浏览


读取JPEG,PNG,TIFF图像格式的文件。

Author: apoth
Date: 2007-09-28 13:41:24 +0200 (Fri, 28 Sep 2007)
New Revision: 8214
--- base/trunk/src/org/deegree/framework/util/ImageUtils.java        2007-09-28 09:32:19 UTC (rev 8213)
+++ base/trunk/src/org/deegree/framework/util/ImageUtils.java        2007-09-28 11:41:24 UTC (rev 8214)
@@ -60,6 +60,8 @@
import org.apache.batik.ext.awt.image.codec.PNGImageDecoder;
import org.apache.batik.ext.awt.image.codec.tiff.TIFFDecodeParam;
import org.apache.batik.ext.awt.image.codec.tiff.TIFFImage;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.GetMethod;

import Acme.JPM.Encoders.GifEncoder;

@@ -73,30 +75,39 @@
import com.sun.media.jai.codec.TIFFEncodeParam;

/**
- * Some util methods for reading standard images
- *
+ * Some util methods for reading standard images
+ *
  * @author Andreas Poth
  * @author last edited by: $Author$
- *
+ *
  * @version $Revision$, $Date$
  */
public class ImageUtils {

     /**
      * reads an image from the passed URL using JAI mechanism
-     * @param url address of the image
-     *
+     *
+     * @param url
+     *            address of the image
+     *
      * @return read image
-     *
-     * @throws IOException
+     *
+     * @throws IOException
      */
     public static BufferedImage loadImage( URL url )
                             throws IOException {
-        InputStream is = url.openStream();
+        // LWL Anpassung
+        InputStream is = null;
+        String uri = url.toExternalForm();
+        HttpClient httpclient = new HttpClient();
+        httpclient.getHttpConnectionManager().getParams().setSoTimeout( 25000 );
+        GetMethod httpget = new GetMethod( uri );
+        httpclient.executeMethod( httpget );
+        is = httpget.getResponseBodyAsStream();
         return loadImage( is );
     }

-    //TODO check
url- at param
Tag
+    // TODO check
url- at param
Tag
     /**
      * reads an image from the passed InputStream using JAI mechanism
      *
@@ -120,12 +131,12 @@

     /**
      * reads an image from the passed file location using JAI mechanism
-     *
-     * @param fileName
-     *
+     *
+     * @param fileName
+     *
      * @return read imagey
-     *
-     * @throws IOException
+     *
+     * @throws IOException
      */
     public static BufferedImage loadImage( String fileName )
                             throws IOException {
@@ -134,32 +145,32 @@

     /**
      * reads an image from the passed file location using JAI mechanism
-     *
-     * @param file
-     *
+     *
+     * @param file
+     *
      * @return read imagey
-     *
-     * @throws IOException
+     *
+     * @throws IOException
      */
     public static BufferedImage loadImage( File file )
                             throws IOException {
-        
+
         BufferedImage img = null;
         String tmp = file.getName().toLowerCase();
-        if ( tmp.endsWith( ".tif" ) || tmp.endsWith( ".tiff" ) ) {         
+        if ( tmp.endsWith( ".tif" ) || tmp.endsWith( ".tiff" ) ) {
             InputStream is = file.toURL().openStream();
-            org.apache.batik.ext.awt.image.codec.SeekableStream fss =
-                new org.apache.batik.ext.awt.image.codec.MemoryCacheSeekableStream( is );
-            TIFFImage tiff = new TIFFImage(fss, new TIFFDecodeParam(), 0 );
+            org.apache.batik.ext.awt.image.codec.SeekableStream fss = new org.apache.batik.ext.awt.image.codec.MemoryCacheSeekableStream(
+                                                                                                                                          is );
+            TIFFImage tiff = new TIFFImage( fss, new TIFFDecodeParam(), 0 );
             img = RenderedOp.wrapRenderedImage( tiff ).getAsBufferedImage();
             fss.close();
         } else if ( tmp.endsWith( ".png" ) ) {
             InputStream is = file.toURL().openStream();
-            ImageDecoderImpl dec = new PNGImageDecoder( is, new PNGDecodeParam() );
+            ImageDecoderImpl dec = new PNGImageDecoder( is, new PNGDecodeParam() );
             img = RenderedOp.wrapRenderedImage( dec.decodeAsRenderedImage() ).getAsBufferedImage();
             is.close();
         } else {
-            img = ImageIO.read(file);
+            img = ImageIO.read( file );
         }

         return img;
@@ -170,7 +181,8 @@
      *
      * @param image
      * @param fileName
-     * @param quality just supported for jpeg (0..1)
+     * @param quality
+     *            just supported for jpeg (0..1)
      * @throws IOException
      */
     public static void saveImage( BufferedImage image, String fileName, float quality )
@@ -184,7 +196,8 @@
      *
      * @param image
      * @param file
-     * @param quality just supported for jpeg (0..1)
+     * @param quality
+     *            just supported for jpeg (0..1)
      * @throws IOException
      */
     public static void saveImage( BufferedImage image, File file, float quality )
@@ -198,8 +211,9 @@
     }

     /**
-     * write an image into the passed output stream. after writing the image the
-     * stream will be closed.
+     * write an image into the passed output stream. after writing the image the stream will be
+     * closed.
+     *
      * @param image
      * @param os
      * @param format
@@ -230,100 +244,96 @@
         }

     }
-   
+
     /**
-    *
-    *
-    * @param out
-    * @param img
-    *
-    * @throws IOException
-    */
+     *
+     *
+     * @param out
+     * @param img
+     *
+     * @throws IOException
+     */
     private static synchronized void encodeGif( OutputStream out, BufferedImage img )
-                                      throws IOException {
-       GifEncoder encoder = new GifEncoder( img, out );
-       encoder.encode();
-   }
+                            throws IOException {
+        GifEncoder encoder = new GifEncoder( img, out );
+        encoder.encode();
+    }

-   /**
-    *
-    *
-    * @param out
-    * @param img
-    *
-    * @throws IOException
-    */
-   private static synchronized void encodeBmp( OutputStream out, BufferedImage img )
-                                      throws IOException {
-       BMPEncodeParam encodeParam = new BMPEncodeParam();
+    /**
+     *
+     *
+     * @param out
+     * @param img
+     *
+     * @throws IOException
+     */
+    private static synchronized void encodeBmp( OutputStream out, BufferedImage img )
+                            throws IOException {
+        BMPEncodeParam encodeParam = new BMPEncodeParam();

-       com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "BMP", out,
-                                                                                     encodeParam );
+        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "BMP", out, encodeParam );

-       encoder.encode( img );
-   }
+        encoder.encode( img );
+    }

-   /**
-    *
-    *
-    * @param out
-    * @param img
-    *
-    * @throws IOException
-    */
-   private static synchronized void encodePng( OutputStream out, BufferedImage img )
-                                      throws IOException {
-       PNGEncodeParam encodeParam = PNGEncodeParam.getDefaultEncodeParam( img );
-      
-       if ( encodeParam instanceof PNGEncodeParam.Palette ) {
-           PNGEncodeParam.Palette p = (PNGEncodeParam.Palette)encodeParam;      
-           byte[] b = new byte[]{-127};        
-           p.setPaletteTransparency(b);
-       }
-               
-       com.sun.media.jai.codec.ImageEncoder encoder =
-           ImageCodec.createImageEncoder("PNG", out, encodeParam);
-       encoder.encode( img.getData(), img.getColorModel() );
-   }
+    /**
+     *
+     *
+     * @param out
+     * @param img
+     *
+     * @throws IOException
+     */
+    private static synchronized void encodePng( OutputStream out, BufferedImage img )
+                            throws IOException {
+        PNGEncodeParam encodeParam = PNGEncodeParam.getDefaultEncodeParam( img );

-   /**
-    *
-    *
-    * @param out
-    * @param img
-    *
-    * @throws IOException
-    */
-   private static synchronized void encodeTiff( OutputStream out, BufferedImage img )
-                                       throws IOException {
-       TIFFEncodeParam encodeParam = new TIFFEncodeParam();
+        if ( encodeParam instanceof PNGEncodeParam.Palette ) {
+            PNGEncodeParam.Palette p = (PNGEncodeParam.Palette) encodeParam;
+            byte[] b = new byte[] { -127 };
+            p.setPaletteTransparency( b );
+        }

-       com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "TIFF", out,
-                                                                                     encodeParam );
+        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "PNG", out, encodeParam );
+        encoder.encode( img.getData(), img.getColorModel() );
+    }

-       encoder.encode( img );        
-   }
+    /**
+     *
+     *
+     * @param out
+     * @param img
+     *
+     * @throws IOException
+     */
+    private static synchronized void encodeTiff( OutputStream out, BufferedImage img )
+                            throws IOException {
+        TIFFEncodeParam encodeParam = new TIFFEncodeParam();

-   /**
-    *
-    *
-    * @param out
-    * @param img
-    * @param quality
-    *
-    * @throws IOException
-    */
-   private static synchronized void encodeJpeg( OutputStream out, BufferedImage img, float quality )
-                                       throws IOException {
+        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "TIFF", out, encodeParam );

-       // encode JPEG
-       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );
-       com.sun.image.codec.jpeg.JPEGEncodeParam jpegParams = encoder.getDefaultJPEGEncodeParam(
-                                                                     img );
-       jpegParams.setQuality( quality, false );
-       encoder.setJPEGEncodeParam( jpegParams );
+        encoder.encode( img );
+    }

-       encoder.encode( img );
-   }
+    /**
+     *
+     *
+     * @param out
+     * @param img
+     * @param quality
+     *
+     * @throws IOException
+     */
+    private static synchronized void encodeJpeg( OutputStream out, BufferedImage img, float quality )
+                            throws IOException {

+        // encode JPEG
+        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );
+        com.sun.image.codec.jpeg.JPEGEncodeParam jpegParams = encoder.getDefaultJPEGEncodeParam( img );
+        jpegParams.setQuality( quality, false );
+        encoder.setJPEGEncodeParam( jpegParams );
+
+        encoder.encode( img );
+    }
+
}
\ No newline at end of file


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/65478/showart_2033356.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP