Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 239|回复: 0

[图像处理学习]如何改变Jpg图像的大小

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-31 23:57:17 | 显示全部楼层 |阅读模式
    /**运行方法:
      *java  ImageSizer  你的输入文件.jpg 你的输出文件.jpg 200 0.5
      */
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;

    // com.sun.image.codec.jpeg package is included
      //in sun and ibm sdk 1.3
    import com.sun.image.codec.jpeg.*;

    /**
      * Use this class to size images.
      *
      * @author Dan Becker
      */  
      
       
       
       

       
      


    public class ImageSizer {
    public static final MediaTracker tracker = new MediaTracker( new Component() {} );
        /** Adjusts the size of the image to the given coordinates.
          * If width or height is -1, the image aspect ration is maintained.
          */
        public static Image setSize( Image image, int width, int height ) {
           return setSize( image, width, height, java.awt.Image.SCALE_DEFAULT );
        } // setSize

        /** Adjusts the size of the image to the given coordinates.
          * If width or height is -1, the image aspect ration is maintained.
          * <p>
          * Hints are one of SCALE_DEFAULT, SCALE_FAST, SCALE_SMOOTH,
          * SCALE_REPLICATE, SCALE_AREA_AVERAGING as defined in java.awt.Image.
          */
        public static Image setSize( Image image, int width, int height, int hints ) {
           return image.getScaledInstance( width, height, hints );
        } // setSize

        /** Checks the given image for valid width and height. */
        public static void checkImage( Image image ) {
           waitForImage( image );
           int imageWidth = image.getWidth( null );
           if ( imageWidth < 1 )  
              throw new IllegalArgumentException( "image width " + imageWidth + " is out of range" );
           int imageHeight = image.getHeight( null );
           if ( imageHeight < 1 )  
              throw new IllegalArgumentException( "image height " + imageHeight + " is out of range" );
           // System.out.println( "Image size=" + imageWidth + "x" + imageHeight );
        } // checkImage

        /** Waits for given image to load. Use before querying image height/width/colors. */
        public static void waitForImage( Image image ) {
           try {
              tracker.addImage( image, 0 );
              tracker.waitForID( 0 );
              // loadStatus = tracker.statusID( 0, false );
              tracker.removeImage(image, 0);
           } catch( InterruptedException e ) { e.printStackTrace(); }
        } // waitForImage

        /** Encodes the given image at the given quality to the output stream. */
        public static void encodeJPEG( OutputStream outputStream, Image outputImage, float outputQuality )  
           throws java.io.IOException {
           int outputWidth  = outputImage.getWidth( null );
           if ( outputWidth < 1 )  
              throw new IllegalArgumentException( "output image width " + outputWidth + " is out of range" );
           int outputHeight = outputImage.getHeight( null );
           if ( outputHeight < 1 )  
              throw new IllegalArgumentException( "output image height " + outputHeight + " is out of range" );

           // Get a buffered image from the image.
           BufferedImage bi = new BufferedImage( outputWidth, outputHeight,
              BufferedImage.TYPE_INT_RGB );                                                   
           Graphics2D biContext = bi.createGraphics();
           biContext.drawImage( outputImage, 0, 0, null );
           // Note that additional drawing such as watermarks or logos can be placed here.

           // com.sun.image.codec.jpeg package is included in sun and ibm sdk 1.3
           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( outputStream );
           // The default quality is 0.75.
           JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam( bi );
           jep.setQuality( outputQuality, true );
           encoder.encode( bi, jep );
           // encoder.encode( bi );
           outputStream.flush();      
        } // encodeImage

        /** Test program */
        public static void main( String [] args )
           throws java.io.IOException {
           if (args.length < 4) {
              System.out.println( "ImageSizer - changes the size of an image." );
              System.out.println( "use: java ImageSizer inputFile outputFile outputWidth outputQuality" );
              return;
           }
           // Gather input arguments.
           String inputFileName = args[ 0 ];
           String outputFileName = args[ 1 ];
           int outputWidth = Integer.parseInt( args[ 2 ] );
           if ( outputWidth < 1 )  
              throw new IllegalArgumentException( "output width "" + args[ 2 ] +"" out of range" );
           float outputQuality = Float.parseFloat( args[ 3 ] );
           if (( outputQuality < 0.0F ) || ( outputQuality > 1.0F ))
              throw new IllegalArgumentException( "output quality "" + args[ 3 ] +"" out of range" );

           // Get input image
           Image inputImage = Toolkit.getDefaultToolkit().getImage( inputFileName );
           checkImage( inputImage );
           // System.out.println( "Input image size=" + inputImage.getWidth( null ) + "x" + inputImage.getHeight( null ) );

           // Create output image.
           Image outputImage = ImageSizer.setSize( inputImage, outputWidth, -1 );
           checkImage( outputImage );
           // System.out.println( "Output image size=" + outputImage.getWidth( null ) + "x" + outputImage.getHeight( null ) );

           // Encode JPEG file.
           FileOutputStream fos = new FileOutputStream( outputFileName );
           encodeJPEG( fos, outputImage, outputQuality );
           fos.close();
           // For some reason, the MediaTracker/ImageProducer likes to hang on.
           System.exit( 0 );
        } // main
    } // class ImageSizer

      

      
      
       
       

         
       

         
       
      



    源码下载:http://file.javaxxz.com/2014/10/31/235717187.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-18 22:09 , Processed in 0.408064 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表