圖片縮放的Java類

by57 9年前發布 | 2K 次閱讀 Java

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;

import com.jhlabs.image.AbstractBufferedImageOp;

/**

  • Scales an image using the area-averaging algorithm, which can't be done with AffineTransformOp. */ public class MyScaleFilter extends AbstractBufferedImageOp {

    private int width; private int height;

    /**

    • Construct a ScaleFilter. */ public MyScaleFilter() { this(32, 32); }

      /**

    • Construct a ScaleFilter.
    • @param width the width to scale to
    • @param height the height to scale to */ public MyScaleFilter( int width, int height ) { this.width = width; this.height = height; }

      public BufferedImage filter( BufferedImage src, BufferedImage dst ) { if ( dst == null ) {

       ColorModel dstCM = src.getColorModel();
       dst = new BufferedImage(dstCM, 
           dstCM.createCompatibleWritableRaster( width, height ), 
           dstCM.isAlphaPremultiplied(), null);
      

      }

      Image scaleImage = src.getScaledInstance( width, height, Image.SCALE_SMOOTH ); Graphics2D g = dst.createGraphics(); g.drawImage( scaleImage, 0, 0, width, height, null ); g.dispose();

      return dst; }

      public String toString() { return "Distort/Scale"; }

}</pre>

 本文由用戶 by57 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!