灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2339回复:0

切分和组合图片(一)

楼主#
更多 发布于:2013-05-12 12:51
切分和组合图片
切割图片
1. load 要切分的图片
2. 确定要切分成多少块
3. 计算小图片的高度和宽度
4. 切分图片
5. 保存图片
要切分图片:
 
 
 

[java]
File file = new File("btg.jpg"); //
项目目录下有名为btg.jpg的图片    
      FileInputStream fis = new
FileInputStream(file);  
      BufferedImage image = ImageIO.read(fis);
//把文件读到图片缓冲流中  
 
      int rows = 4; //定义图片要切分成多少块    
      int cols
= 4;  
      int chunks = rows * cols;  
 
      int chunkWidth =
image.getWidth() / cols; // 计算每一块小图片的高度和宽度  
      int chunkHeight =
image.getHeight() / rows;  
      int count = 0;  
      BufferedImage
imgs[] = new BufferedImage[chunks];  
      for (int x = 0; x < rows;
x++) {  
          for (int y = 0; y < cols; y++) {  
              
//初始化BufferedImage  
              imgs[count] = new
BufferedImage(chunkWidth, chunkHeight,
image.getType());  
 
              //画出每一小块图片  
              
Graphics2D gr = imgs[count++].createGraphics();  
              
gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight *
x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight,
null);  
              gr.dispose();  
          }  
      
}  
      System.out.println("切分完成");  
 
      
//保存小图片到文件中  
      for (int i = 0; i < imgs.length; i++)
{  
          ImageIO.write(imgs, "jpg", new File("img" + i +
".jpg"));  
      }  
      System.out.println("小图片创建完成");  
  File file = new File("btg.jpg"); // 项目目录下有名为btg.jpg的图片
        
FileInputStream fis = new FileInputStream(file);
        BufferedImage image
= ImageIO.read(fis); //把文件读到图片缓冲流中
 
        int rows = 4;
//定义图片要切分成多少块
        int cols = 4;
        int chunks = rows *
cols;
 
        int chunkWidth = image.getWidth() / cols; //
计算每一块小图片的高度和宽度
        int chunkHeight = image.getHeight() /
rows;
        int count = 0;
        BufferedImage imgs[] = new
BufferedImage[chunks];
        for (int x = 0; x < rows; x++)
{
            for (int y = 0; y < cols; y++) {
                
//初始化BufferedImage
                imgs[count] = new
BufferedImage(chunkWidth, chunkHeight,
image.getType());
 
                //画出每一小块图片
                
Graphics2D gr = imgs[count++].createGraphics();
                
gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight *
x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight,
null);
                gr.dispose();
            }
        
}
        System.out.println("切分完成");
 
        
//保存小图片到文件中
        for (int i = 0; i < imgs.length; i++)
{
            ImageIO.write(imgs, "jpg", new File("img" + i +
".jpg"));
        }
        System.out.println("小图片创建完成");
切分完成:
 
 

喜欢0 评分0
游客

返回顶部