`
chun521521
  • 浏览: 278117 次
  • 性别: Icon_minigender_1
  • 来自: 长春
社区版块
存档分类
最新评论

图片缩放

 
阅读更多

图片缩放


package com.jit;

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import com.sun.image.codec.jpeg.*;

public class ImageZoom {
 private float resizeTimes = 0.3f;
 private float quality = 0.9f;
 
 public static void main(String[] args) {
  ImageZoom test = new ImageZoom();
 
  String inputFoler = "F:\\ziji\\11\\";
  File[] f1 = new File(inputFoler).listFiles();
  for(File f0: f1){
   String fileName = f0.getName();//F:\\ziji\\11\\S70626-055354.jpg
   String newFileName = inputFoler + "2017"+fileName.substring(2, 6)+".jpg";
   test.zoomImage(inputFoler+ fileName, newFileName);
   f0 = null;
  }
 }

 public void zoomImage(String src, String dest) {
  System.out.println("src=" + src + ",dest=" + dest);
  BufferedImage image = null;
  FileOutputStream os = null;
  try {
   File srcfile = new File(src);
   BufferedImage im = ImageIO.read(srcfile);

   /* 调整后的图片的宽度和高度 */
   int toWidth = (int) (im.getWidth() * resizeTimes);
   int toHeight = (int) (im.getHeight() * resizeTimes);

   /* 新生成结果图片 */
   image = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
   image.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
  
   /* 输出到文件流 */
   os = new FileOutputStream(dest);
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
   JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(image);
   /* 压缩质量 */
   jep.setQuality(quality, true);
   encoder.encode(image, jep);
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   try {
    os.close();
    os = null;
   
    image.flush();
    image = null;
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

}

 


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics