swing如何处理图片 我想把一张比较大的 质量比较好的图片处理一下处理后 图片的大小可以制定 图片质量降低也无所谓 就是想压缩图片 让他更小

解决方案 »

  1.   

    你的需求和Swing没什么关系,下面的代码使用了ImageIO编码压缩一个图像文件。
    public boolean write(File file, BufferedImage image, int quality, IIOWriteProgressListener progressListener) {
            try {
                String suffix = Utilities.getFileSuffix(file);
                
                if(suffix == null){
                    return false;
                }
                
                Iterator it = ImageIO.getImageWritersBySuffix(suffix);
                if (it.hasNext()) {                FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(file);
                    ImageWriter iw = (ImageWriter) it.next();                ImageWriteParam iwp = iw.getDefaultWriteParam();
                    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                    iwp.setCompressionQuality((float) quality / 100f);                iw.setOutput(fileImageOutputStream);
                    iw.addIIOWriteProgressListener(progressListener);
                    iw.write(null, new javax.imageio.IIOImage(image, null, null), iwp);
                    iw.dispose();                fileImageOutputStream.flush();
                    fileImageOutputStream.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                return false;
            }        return true;
        }
      

  2.   


    // Utilities.getFileSuffix(file);
    public static String getFileSuffix(File file) {
            if (file == null) {
                return null;
            }        String fileName = file.getName();
            
            String suffix = "";
            int i = fileName.lastIndexOf('.');
            if (i > 0 && i < fileName.length() - 1) {
                suffix = fileName.substring(i + 1);
            }
            
            return suffix;
        }
      

  3.   

    package handler;import java.awt.AlphaComposite;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;import javax.media.jai.Interpolation;
    import javax.media.jai.JAI;
    import javax.media.jai.ParameterBlockJAI;
    import javax.media.jai.PlanarImage;
    import javax.media.jai.TiledImage;import com.sun.media.jai.codec.JPEGEncodeParam;
    public class ImageHandler {
     public static final int OVERLAY_TOP_LEFT = 1 << 1;
        public static final int OVERLAY_TOP_RIGHT = 1 << 2;
        public static final int OVERLAY_CENTER = 1 << 3;
        public static final int OVERLAY_BOTTOM_LEFT = 1 << 4;
        public static final int OVERLAY_BOTTOM_RIGHT = 1 << 5;
        
        PlanarImage img;
        
        public ImageHandler() {
            
        }
        
        /**
         *@param amount The amount to scale by (i.e. 2.0F means "twice as big")
         *
         *Scales the image by the given amount
         */
        public void scale(float amount) {
            ParameterBlockJAI pb = new ParameterBlockJAI("scale");
            pb.addSource(this.img);
            pb.setParameter("xScale", amount); //x Scale Factor
            pb.setParameter("yScale", amount); //y Scale Factor
            pb.setParameter("xTrans", 0.0F);      //x Translate amount
            pb.setParameter("yTrans", 0.0F);      //y Translate amount
       //     pb.setParameter("interpolation", new InterpolationNearest());
            pb.setParameter("interpolation", Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
            this.img = JAI.create("scale", pb, null);
        }
            
        
        /**
         *@param width The width this image should be scaled to as a max
         *
         *Scales the image uniformly by calculating the scale value generated by using the width as a maximum width
         */
        public void scaleX(float width) {
            float scaleFactor;
            scaleFactor = width / (float)img.getWidth();
            scale(scaleFactor);
        }
        
        /**
         *@param height The height this image should be scaled to as a max
         *
         *Scales the image uniformly by calculating the scale value generated by using the height as a maximum height.
         */
        public void scaleY(float height) {
            float scaleFactor;
            scaleFactor = height / (float)img.getHeight();
            scale(scaleFactor);
        }
        
        /**
         *Resizes the image to fit inside the width and height given. Not guaranteed to be the exact dimensions
         *given, just fit within them.
         *@param width The maximum width to fit
         *@param height The maximum height to fit
         */
        public void scaleToFit(int width, int height) {
            if(img.getWidth() > img.getHeight()) {
                scaleX((float)width);
            } else {
                scaleY((float)height);
            }
        }
        
        /**
         *Loads the image from a path and filename
         *
         *@param filepath The oath and filename where the file can be found
         */
        public void loadImage(String filepath) {
            //Loads the image from the given path and filename
            this.img = (PlanarImage)JAI.create("fileload", filepath);
        }
        
        /**
         *Saves the image to the given path and filename
         *
         *@param filepath the path and filename to save the image to.
         */
        public void saveImage(String filepath) {
            //Saves the image to the given path and filename as a JPEG image
            saveImage(filepath, "JPEG");
        }
        
        /**
         *Saves the image to the given path and filename using the given codec
         *
         *@param filepath the path and filename to save the image to.
         *@param type The JAI-defined codec type to save as.
         */
        public void saveImage(String filepath, String type) {
            //Saves the image to the given path and filename
         JPEGEncodeParam encodeParam = new JPEGEncodeParam();
         encodeParam.setQuality(0.6F); // Resolution      encodeParam.setHorizontalSubsampling(0, 1);
         encodeParam.setHorizontalSubsampling(1, 2);
         encodeParam.setHorizontalSubsampling(2, 2);
         encodeParam.setVerticalSubsampling(0, 1);
         encodeParam.setVerticalSubsampling(1, 1);
         encodeParam.setVerticalSubsampling(2, 1);
         encodeParam.setRestartInterval(64);
         try {
    OutputStream out  = new FileOutputStream(new File(filepath));
    JAI.create("encode", img, out, type, encodeParam);
    out.flush();
    out.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }catch (IOException e) {
    e.printStackTrace();
    }
          //  JAI.create("filestore", img, filepath, type, null);
        }
        
        /**
         *@param radians the amount to rotate by in radians
         *
         *Rotates the image by the given amount. Use degreesToRadians to convert degrees.
         */
      

  4.   

    package handler;import java.awt.AlphaComposite;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;import javax.media.jai.Interpolation;
    import javax.media.jai.JAI;
    import javax.media.jai.ParameterBlockJAI;
    import javax.media.jai.PlanarImage;
    import javax.media.jai.TiledImage;import com.sun.media.jai.codec.JPEGEncodeParam;
    public class ImageHandler {
     public static final int OVERLAY_TOP_LEFT = 1 << 1;
        public static final int OVERLAY_TOP_RIGHT = 1 << 2;
        public static final int OVERLAY_CENTER = 1 << 3;
        public static final int OVERLAY_BOTTOM_LEFT = 1 << 4;
        public static final int OVERLAY_BOTTOM_RIGHT = 1 << 5;
        
        PlanarImage img;
        
        public ImageHandler() {
            
        }
        
        /**
         *@param amount The amount to scale by (i.e. 2.0F means "twice as big")
         *
         *Scales the image by the given amount
         */
        public void scale(float amount) {
            ParameterBlockJAI pb = new ParameterBlockJAI("scale");
            pb.addSource(this.img);
            pb.setParameter("xScale", amount); //x Scale Factor
            pb.setParameter("yScale", amount); //y Scale Factor
            pb.setParameter("xTrans", 0.0F);      //x Translate amount
            pb.setParameter("yTrans", 0.0F);      //y Translate amount
       //     pb.setParameter("interpolation", new InterpolationNearest());
            pb.setParameter("interpolation", Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
            this.img = JAI.create("scale", pb, null);
        }
            
        
        /**
         *@param width The width this image should be scaled to as a max
         *
         *Scales the image uniformly by calculating the scale value generated by using the width as a maximum width
         */
        public void scaleX(float width) {
            float scaleFactor;
            scaleFactor = width / (float)img.getWidth();
            scale(scaleFactor);
        }
        
        /**
         *@param height The height this image should be scaled to as a max
         *
         *Scales the image uniformly by calculating the scale value generated by using the height as a maximum height.
         */
        public void scaleY(float height) {
            float scaleFactor;
            scaleFactor = height / (float)img.getHeight();
            scale(scaleFactor);
        }
        
        /**
         *Resizes the image to fit inside the width and height given. Not guaranteed to be the exact dimensions
         *given, just fit within them.
         *@param width The maximum width to fit
         *@param height The maximum height to fit
         */
        public void scaleToFit(int width, int height) {
            if(img.getWidth() > img.getHeight()) {
                scaleX((float)width);
            } else {
                scaleY((float)height);
            }
        }
        
        /**
         *Loads the image from a path and filename
         *
         *@param filepath The oath and filename where the file can be found
         */
        public void loadImage(String filepath) {
            //Loads the image from the given path and filename
            this.img = (PlanarImage)JAI.create("fileload", filepath);
        }
        
        /**
         *Saves the image to the given path and filename
         *
         *@param filepath the path and filename to save the image to.
         */
        public void saveImage(String filepath) {
            //Saves the image to the given path and filename as a JPEG image
            saveImage(filepath, "JPEG");
        }
        
        /**
         *Saves the image to the given path and filename using the given codec
         *
         *@param filepath the path and filename to save the image to.
         *@param type The JAI-defined codec type to save as.
         */
        public void saveImage(String filepath, String type) {
            //Saves the image to the given path and filename
         JPEGEncodeParam encodeParam = new JPEGEncodeParam();
         encodeParam.setQuality(0.6F); // Resolution      encodeParam.setHorizontalSubsampling(0, 1);
         encodeParam.setHorizontalSubsampling(1, 2);
         encodeParam.setHorizontalSubsampling(2, 2);
         encodeParam.setVerticalSubsampling(0, 1);
         encodeParam.setVerticalSubsampling(1, 1);
         encodeParam.setVerticalSubsampling(2, 1);
         encodeParam.setRestartInterval(64);
         try {
    OutputStream out  = new FileOutputStream(new File(filepath));
    JAI.create("encode", img, out, type, encodeParam);
    out.flush();
    out.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }catch (IOException e) {
    e.printStackTrace();
    }
          //  JAI.create("filestore", img, filepath, type, null);
        }
        
        /**
         *@param radians the amount to rotate by in radians
         *
         *Rotates the image by the given amount. Use degreesToRadians to convert degrees.
         */
      

  5.   

     public void rotate(float radians) {
            
        }
        
        /**
         *@param degrees the angle to convert
         *
         *Converts degrees into radians.
         */
        public static float degreesToRadians(float degrees) {
            return (float)((degrees*Math.PI)/180.0F);
        }
        
        /**
         *Overlays the image specified by filepath on the current image.
         *
         *@param filepath The path and file to the image to use as the overlay
         *@param alpha the amount of transparency to apply to the overlay
         */
        public void overlay(String filepath, float alpha, int align) {
            BufferedImage bi = ((PlanarImage)JAI.create("fileload", filepath)).getAsBufferedImage();
            overlay(bi, alpha, align);
        }
        
        /**
         *Overlays the image specified by filepath on the current image.
         *
         *@param bi The image to overlay on the current image.
         *@param alpha The amount of transparency to apply to the overlay.
         */ 
        public void overlay(BufferedImage bi, float alpha, int align) {
            AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
            int overX, overY;
            /*
            //The following will resize the overlay image before applying it.
            //CURRENTLY NOT WORKING
            if(bi.getWidth() > this.img.getWidth()) {
                bi = (BufferedImage)bi.getScaledInstance(this.img.getWidth(), (int)((float)bi.getHeight()*((float)this.img.getWidth()/(float)bi.getWidth())), java.awt.Image.SCALE_DEFAULT);
            }
            if(bi.getHeight() > this.img.getHeight()) {
                bi = (BufferedImage)bi.getScaledInstance((int)((float)bi.getWidth()*((float)this.img.getHeight()/(float)bi.getHeight())), this.img.getHeight(), java.awt.Image.SCALE_DEFAULT);
            }*/
            if(align == OVERLAY_BOTTOM_RIGHT){
                overX = this.img.getMaxX() - bi.getWidth();
                overY = this.img.getMaxY() - bi.getHeight();
            } else if(align == OVERLAY_BOTTOM_LEFT) {
                overX = 0;
                overY = this.img.getMaxY() - bi.getHeight();
            } else if(align == OVERLAY_TOP_RIGHT) {
                overX = this.img.getMaxX() - bi.getWidth();
                overY = 0;
            } else if(align == OVERLAY_CENTER) {
                overX = this.img.getWidth()/2 - bi.getWidth()/2;
                overY = this.img.getHeight()/2 - bi.getHeight()/2; 
            } else {
                overX = 0;
                overY = 0;
            }
                
            TiledImage ti = new TiledImage(this.img, false);
            Graphics2D g2 = ti.createGraphics();
            g2.setComposite(ac);
            g2.drawImage(bi, overX, overY, null);
            this.img = ti;
        }
        
        public void overlayText(String text, float alpha, int align, Font f, java.awt.Color col) {
            TiledImage ti = new TiledImage(this.img, false);
            Graphics2D g2 = ti.createGraphics();
            AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
            
            int overX, overY;
            java.awt.font.FontRenderContext frc = g2.getFontRenderContext();
            java.awt.font.TextLayout layout = new java.awt.font.TextLayout(text, f, frc);
            java.awt.geom.Rectangle2D rect = layout.getBounds();
            
            if(align == OVERLAY_BOTTOM_RIGHT){
                overX = this.img.getMaxX() - (int)rect.getWidth();
                overY = this.img.getMaxY() - (int)rect.getHeight();
            } else if(align == OVERLAY_BOTTOM_LEFT) {
                overX = 0;
                overY = this.img.getMaxY() - (int)rect.getHeight();
            } else if(align == OVERLAY_TOP_RIGHT) {
                overX = this.img.getMaxX() - (int)rect.getWidth();
                overY = 0;
            } else if(align == OVERLAY_CENTER) {
                overX = this.img.getWidth()/2 - (int)rect.getWidth()/2;
                overY = this.img.getHeight()/2 - (int)rect.getHeight()/2; 
            } else {
                overX = 0;
                overY = 0;
            }
            g2.setComposite(ac);
            g2.setFont(f);
            g2.setColor(col);
            g2.drawString(text, overX, overY);
            this.img = ti;
        }
        
        public BufferedImage getCurrentImage() {
            return img.getAsBufferedImage();
        }
    }eg: 
    File bigPic = 
    File smallPic = 
    ImageHandler ih = new ImageHandler();
    ih.loadImage(bigPic.getPath());
    ih.scaleToFit(150,150);
    ih.saveImage(smallPic.getPath());