java图片大小处理(缩放&切割&类型转换&色彩转换)

  1. import java.io.*;  
  2. import java.awt.*;  
  3. import java.awt.image.*;  
  4. import java.awt.Graphics;  
  5. import java.awt.color.ColorSpace;  
  6. import javax.imageio.ImageIO;  
  7. public class ChangeImageSize  
  8. ...{  
  9.     /** *//** 
  10.      * 缩放图像 
  11.      * @param srcImageFile 源图像文件地址 
  12.      * @param result       缩放后的图像地址 
  13.      * @param scale        缩放比例 
  14.      * @param flag         缩放选择:true 放大; false 缩小; 
  15.      */  
  16.     public static void scale(String srcImageFile, String result, int scale, boolean flag)  
  17.     ...{  
  18.         try  
  19.         ...{  
  20.             BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件  
  21.             int width = src.getWidth(); // 得到源图宽  
  22.             int height = src.getHeight(); // 得到源图长  
  23.             if (flag)  
  24.             ...{  
  25.                 // 放大  
  26.                 width = width * scale;  
  27.                 height = height * scale;  
  28.             }  
  29.             else  
  30.             ...{  
  31.                 // 缩小  
  32.                 width = width / scale;  
  33.                 height = height / scale;  
  34.             }  
  35.             Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);  
  36.             BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  37.             Graphics g = tag.getGraphics();  
  38.             g.drawImage(image, 00null); // 绘制缩小后的图  
  39.             g.dispose();  
  40.             ImageIO.write(tag, "JPEG"new File(result));// 输出到文件流  
  41.         }  
  42.         catch (IOException e)  
  43.         ...{  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.     /** *//** 
  48.      * 图像切割 
  49.      * @param srcImageFile 源图像地址 
  50.      * @param descDir      切片目标文件夹 
  51.      * @param destWidth    目标切片宽度 
  52.      * @param destHeight   目标切片高度 
  53.      */  
  54.     public static void cut(String srcImageFile, String descDir, int destWidth, int destHeight)  
  55.     ...{  
  56.         try  
  57.         ...{  
  58.             Image img;  
  59.             ImageFilter cropFilter;  
  60.             // 读取源图像  
  61.             BufferedImage bi = ImageIO.read(new File(srcImageFile));  
  62.             int srcWidth = bi.getHeight(); // 源图宽度  
  63.             int srcHeight = bi.getWidth(); // 源图高度  
  64.             if (srcWidth > destWidth && srcHeight > destHeight)  
  65.             ...{  
  66.                 Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);  
  67.                 destWidth = 200// 切片宽度  
  68.                 destHeight = 150// 切片高度  
  69.                 int cols = 0// 切片横向数量  
  70.                 int rows = 0// 切片纵向数量  
  71.                 // 计算切片的横向和纵向数量  
  72.                 if (srcWidth % destWidth == 0)  
  73.                 ...{  
  74.                     cols = srcWidth / destWidth;  
  75.                 }  
  76.                 else  
  77.                 ...{  
  78.                     cols = (int) Math.floor(srcWidth / destWidth) + 1;  
  79.                 }  
  80.                 if (srcHeight % destHeight == 0)  
  81.                 ...{  
  82.                     rows = srcHeight / destHeight;  
  83.                 }  
  84.                 else  
  85.                 ...{  
  86.                     rows = (int) Math.floor(srcHeight / destHeight) + 1;  
  87.                 }  
  88.                 // 循环建立切片  
  89.                 // 改进的想法:是否可用多线程加快切割速度  
  90.                 for (int i = 0; i < rows; i++)  
  91.                 ...{  
  92.                     for (int j = 0; j < cols; j++)  
  93.                     ...{  
  94.                         // 四个参数分别为图像起点坐标和宽高  
  95.                         // 即: CropImageFilter(int x,int y,int width,int height)  
  96.                         cropFilter = new CropImageFilter(j * 200, i * 150, destWidth, destHeight);  
  97.                         img = Toolkit.getDefaultToolkit(), .createImage(  
  98.                                         new FilteredImageSource(image.getSource(), cropFilter));  
  99.                         BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);  
  100.                         Graphics g = tag.getGraphics();  
  101.                         g.drawImage(img, 00null); // 绘制缩小后的图  
  102.                         g.dispose();  
  103.                         // 输出为文件  
  104.                         ImageIO.write(tag, "JPEG"new File(descDir + "pre_map_" + i + "_" + j + ".jpg"));  
  105.                     }  
  106.                 }  
  107.             }  
  108.         }  
  109.         catch (Exception e)  
  110.         ...{  
  111.             e.printStackTrace();  
  112.         }  
  113.     }  
  114.     /** *//** 
  115.      * 图像类型转换 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X) 
  116.      */  
  117.     public static void convert(String source, String result)  
  118.     ...{  
  119.         try  
  120.         ...{  
  121.             File f = new File(source);  
  122.             f.canRead();  
  123.             f.canWrite();  
  124.             BufferedImage src = ImageIO.read(f);  
  125.             ImageIO.write(src, "JPG"new File(result));  
  126.         }  
  127.         catch (Exception e)  
  128.         ...{  
  129.             // TODO Auto-generated catch block  
  130.             e.printStackTrace();  
  131.         }  
  132.     }  
  133.     /** *//** 
  134.      * 彩色转为黑白 
  135.      * @param source 
  136.      * @param result 
  137.      */  
  138.     public static void gray(String source, String result)  
  139.     ...{  
  140.         try  
  141.         ...{  
  142.             BufferedImage src = ImageIO.read(new File(source));  
  143.             ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);  
  144.             ColorConvertOp op = new ColorConvertOp(cs, null);  
  145.             src = op.filter(src, null);  
  146.             ImageIO.write(src, "JPEG"new File(result));  
  147.         }  
  148.         catch (IOException e)  
  149.         ...{  
  150.             e.printStackTrace();  
  151.         }  
  152.     }  
  153.     /** *//** 
  154.      * @param args 
  155.      */  
  156.     public static void main(String[] args)  
  157.     ...{  
  158.         scale("D:/100CASIO/CIMG0001.JPG","C:/Documents and Settings/ibm/桌面/image.jpg",10,false);  
  159.     }  
  160. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值