Java分割拼接图片

来源:互联网 发布:默认禁用网络映射 编辑:程序博客网 时间:2024/05/06 20:41
分割图片 
Java代码  收藏代码
  1. private static void splitImage() throws IOException {  
  2.       
  3.     String originalImg = "C:\\img\\split\\a380_1280x1024.jpg";  
  4.   
  5.     // 读入大图  
  6.     File file = new File(originalImg);  
  7.     FileInputStream fis = new FileInputStream(file);  
  8.     BufferedImage image = ImageIO.read(fis);  
  9.   
  10.     // 分割成4*4(16)个小图  
  11.     int rows = 4;  
  12.     int cols = 4;  
  13.     int chunks = rows * cols;  
  14.   
  15.     // 计算每个小图的宽度和高度  
  16.     int chunkWidth = image.getWidth() / cols;  
  17.     int chunkHeight = image.getHeight() / rows;  
  18.   
  19.     int count = 0;  
  20.     BufferedImage imgs[] = new BufferedImage[chunks];  
  21.     for (int x = 0; x < rows; x++) {  
  22.         for (int y = 0; y < cols; y++) {  
  23.             //设置小图的大小和类型  
  24.             imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());  
  25.   
  26.             //写入图像内容  
  27.             Graphics2D gr = imgs[count++].createGraphics();  
  28.             gr.drawImage(image, 00,   
  29.                     chunkWidth, chunkHeight,   
  30.                     chunkWidth* y, chunkHeight * x,   
  31.                     chunkWidth * y + chunkWidth,  
  32.                     chunkHeight * x + chunkHeight, null);  
  33.             gr.dispose();  
  34.         }  
  35.     }  
  36.   
  37.     // 输出小图  
  38.     for (int i = 0; i < imgs.length; i++) {  
  39.         ImageIO.write(imgs[i], "jpg"new File("C:\\img\\split\\img" + i + ".jpg"));  
  40.     }  
  41.       
  42.     System.out.println("完成分割!");  
  43. }  

 
 

拼接图片 
Java代码  收藏代码
  1. private static void mergeImage() throws IOException {  
  2.   
  3.     int rows = 2;  
  4.     int cols = 2;  
  5.     int chunks = rows * cols;  
  6.   
  7.     int chunkWidth, chunkHeight;  
  8.     int type;  
  9.   
  10.     //读入小图  
  11.     File[] imgFiles = new File[chunks];  
  12.     for (int i = 0; i < chunks; i++) {  
  13.         imgFiles[i] = new File("C:\\img\\merge\\img" + i + ".jpg");  
  14.     }  
  15.   
  16.     //创建BufferedImage  
  17.     BufferedImage[] buffImages = new BufferedImage[chunks];  
  18.     for (int i = 0; i < chunks; i++) {  
  19.         buffImages[i] = ImageIO.read(imgFiles[i]);  
  20.     }  
  21.     type = buffImages[0].getType();  
  22.     chunkWidth = buffImages[0].getWidth();  
  23.     chunkHeight = buffImages[0].getHeight();  
  24.   
  25.     //设置拼接后图的大小和类型  
  26.     BufferedImage finalImg = new BufferedImage(chunkWidth * cols, chunkHeight * rows, type);  
  27.   
  28.     //写入图像内容  
  29.     int num = 0;  
  30.     for (int i = 0; i < rows; i++) {  
  31.         for (int j = 0; j < cols; j++) {  
  32.             finalImg.createGraphics().drawImage(buffImages[num], chunkWidth * j, chunkHeight * i, null);  
  33.             num++;  
  34.         }  
  35.     }  
  36.   
  37.     //输出拼接后的图像  
  38.     ImageIO.write(finalImg, "jpeg"new File("C:\\img\\merge\\finalImg.jpg"));  
  39.   
  40.     System.out.println("完成拼接!");  
  41. }  

原创粉丝点击