Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1471712
  • 博文数量: 254
  • 博客积分: 8696
  • 博客等级: 中将
  • 技术积分: 2961
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-03 16:46
文章分类

全部博文(254)

文章存档

2015年(4)

2014年(18)

2013年(16)

2012年(8)

2011年(25)

2010年(2)

2009年(74)

2008年(107)

分类: Java

2009-08-21 09:02:13

 
读取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 Tag
+    // TODO check 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

 
阅读(1203) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~