[Java] Java Image Filters 图像缩放质量差的问题解决 →→→→→进入此内容的聊天室

来自 , 2019-06-24, 写在 Java, 查看 110 次.
URL http://www.code666.cn/view/7eb3c8be
  1. BufferedImage bi_src = ...;
  2. ImageIO.write(new ScaleFilter(100,100), "jpeg", dest);
  3. //源代码片段来自云代码http://yuncode.net
  4.                        
  5.  
  6. public class ScaleFilter extends AbstractBufferedImageOp {
  7.  
  8.         private int width;
  9.         private int height;
  10.  
  11.     /**
  12.      * Construct a ScaleFilter.
  13.      */
  14.         public ScaleFilter() {
  15.                 this(32, 32);
  16.         }
  17.  
  18.     /**
  19.      * Construct a ScaleFilter.
  20.      * @param width the width to scale to
  21.      * @param height the height to scale to
  22.      */
  23.         public ScaleFilter( int width, int height ) {
  24.                 this.width = width;
  25.                 this.height = height;
  26.         }
  27.  
  28.     public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
  29.                 if ( dst == null ) {
  30.                         ColorModel dstCM = src.getColorModel();
  31.                         dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster( width, height ),
  32. dstCM.isAlphaPremultiplied(), null);
  33.                 }
  34.  
  35.                 Image scaleImage = src.getScaledInstance( width, height, Image.SCALE_AREA_AVERAGING );
  36.                 Graphics2D g = dst.createGraphics();
  37.                 g.drawImage( scaleImage, 0, 0, width, height, null );
  38.                 g.dispose();
  39.  
  40.         return dst;
  41.     }
  42.  
  43.         public String toString() {
  44.                 return "Distort/Scale";
  45.         }
  46.  
  47. }//源代码片段来自云代码http://yuncode.net
  48.                        

回复 "Java Image Filters 图像缩放质量差的问题解决"

这儿你可以回复上面这条便签

captcha