java图片缩放不失真(收集)

  1. import java.awt.image.BufferedImage;   
  2. import java.io.ByteArrayInputStream;   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.IOException;   
  6. import java.io.InputStream;   
  7. import javax.imageio.ImageIO;   
  8. import com.mortennobel.imagescaling.ResampleOp;   
  9. import org.apache.log4j.Logger;   
  10.   
  11. /**  
  12.  * 图片缩放工具类  
  13.  * @author sunnymoon  
  14.  */  
  15. public class MyImage {   
  16.     private final Logger log = Logger.getLogger(this.getClass());   
  17.     /**  
  18.      * 接收输入流输生成图片  
  19.      * @param input  
  20.      * @param writePath  
  21.      * @param width  
  22.      * @param height  
  23.      * @param format  
  24.      * @return  
  25.      */  
  26.     public boolean resizeImage(InputStream input, String writePath,   
  27.             Integer width, Integer height, String format) {   
  28.         try {   
  29.             BufferedImage inputBufImage = ImageIO.read(input);   
  30.             log.info("转前图片高度和宽度:" + inputBufImage.getHeight() + ":"+ inputBufImage.getWidth());   
  31.             ResampleOp resampleOp = new ResampleOp(width, height);// 转换   
  32.             BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,   
  33.                     null);   
  34.             ImageIO.write(rescaledTomato, format, new File(writePath));   
  35.             log.info("转后图片高度和宽度:" + rescaledTomato.getHeight() + ":"+ rescaledTomato.getWidth());   
  36.             return true;   
  37.         } catch (IOException e) {   
  38.             e.printStackTrace();   
  39.             return false;   
  40.         }   
  41.   
  42.     }   
  43.   
  44.     /**  
  45.      * 接收File输出图片  
  46.      * @param file  
  47.      * @param writePath  
  48.      * @param width  
  49.      * @param height  
  50.      * @param format  
  51.      * @return  
  52.      */  
  53.     public boolean resizeImage(File file, String writePath, Integer width,   
  54.             Integer height, String format) {   
  55.         try {   
  56.             BufferedImage inputBufImage = ImageIO.read(file);   
  57.             inputBufImage.getType();   
  58.             log.info("转前图片高度和宽度:" + inputBufImage.getHeight() + ":"+ inputBufImage.getWidth());   
  59.             ResampleOp resampleOp = new ResampleOp(width, height);// 转换   
  60.             BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,   
  61.                     null);   
  62.             ImageIO.write(rescaledTomato, format, new File(writePath));   
  63.             log.info("转后图片高度和宽度:" + rescaledTomato.getHeight() + ":"+ rescaledTomato.getWidth());   
  64.             return true;   
  65.         } catch (IOException e) {   
  66.             e.printStackTrace();   
  67.             return false;   
  68.         }   
  69.   
  70.     }   
  71.   
  72.     /**  
  73.      * 接收字节数组生成图片  
  74.      * @param RGBS  
  75.      * @param writePath  
  76.      * @param width  
  77.      * @param height  
  78.      * @param format  
  79.      * @return  
  80.      */  
  81.     public boolean resizeImage(byte[] RGBS, String writePath, Integer width,   
  82.             Integer height, String format) {   
  83.         InputStream input = new ByteArrayInputStream(RGBS);   
  84.         return this.resizeImage(input, writePath, width, height, format);   
  85.     }   
  86.   
  87.     public byte[] readBytesFromIS(InputStream is) throws IOException {   
  88.         int total = is.available();   
  89.         byte[] bs = new byte[total];   
  90.         is.read(bs);   
  91.         return bs;   
  92.     }   
  93.        
  94.     //测试:只测试了字节流的方式,其它的相对简单,没有一一测试   
  95.     public static void main(String[] args) throws IOException {   
  96.            
  97.            
  98.         int width = 150;   
  99.         int height = 150;   
  100.         File inputFile = new File("F://from.jpg");   
  101.         File outFile = new File("F://to.jpg");   
  102.         String outPath = outFile.getAbsolutePath();   
  103.         MyImage myImage = new MyImage();   
  104.         InputStream input = new FileInputStream(inputFile);   
  105.         byte[] byteArrayImage=myImage.readBytesFromIS(input);   
  106.         input.read(byteArrayImage);   
  107.         myImage.resizeImage(byteArrayImage, outPath, width, height, "jpg");   
  108.     }   
  109. }  

你可能感兴趣的:(java图片缩放不失真(收集))