JAVA CHAT SCALE2

package img; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.color.ColorSpace; 
import java.awt.image.BufferedImage; 
import java.awt.image.ColorConvertOp; 
import java.awt.image.CropImageFilter; 
import java.awt.image.FilteredImageSource; 
import java.awt.image.ImageFilter; 
import java.awt.image.ImageProducer; 
import java.io.File; 
import java.io.IOException; 
 
import javax.imageio.ImageIO; 
 
public class TestSplit { 
 
 
  /**
   * 缩放图像
   * 
   * @param srcImageFile
   *            源图像文件地址
   * @param result
   *            缩放后的图像地址
   * @param scale
   *            缩放比例
   * @param flag
   *            缩放选择:true 放大; false 缩小;
   */ 
  public static void scale(String srcImageFile, String result, int scale, 
    boolean flag) { 
   try { 
    BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件 
    int width = src.getWidth(); // 得到源图宽 
    int height = src.getHeight(); // 得到源图长 
    if (flag) { 
     // 放大 
     width = width * scale; 
     height = height * scale; 
    } else { 
     // 缩小 
     width = width / scale; 
     height = height / scale; 
    } 
    Image image = src.getScaledInstance(width, height, 
      Image.SCALE_DEFAULT); 
    BufferedImage tag = new BufferedImage(width, height, 
      BufferedImage.TYPE_INT_RGB); 
    Graphics g = tag.getGraphics(); 
    g.drawImage(image, 0, 0, null); // 绘制缩小后的图 
    g.dispose(); 
    ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
  } 
   
  /**
   * 图像切割
   * 
   * @param srcImageFile
   *            源图像地址
   * @param descDir
   *            切片目标文件夹
   * @param destWidth
   *            目标切片宽度
   * @param destHeight
   *            目标切片高度
   */ 
  public static void cut(String srcImageFile, final String descDir, final int destWidth, 
            final int destHeight) { 
      try {          
            // 读取源图像 
            BufferedImage bi = ImageIO.read(new File(srcImageFile)); 
            final int srcWidth = bi.getHeight(); // 源图宽度 
            final int srcHeight = bi.getWidth(); // 源图高度 
            //如果源图片的宽度和长度都小于目标图片,不用处理。 
            if (srcWidth <= destWidth && srcHeight <= destHeight) return; 
             
            // 计算切片的横向和纵向数量 
            int temp = srcWidth / destWidth; 
            int cols = srcWidth % destWidth == 0 ? temp : temp+1; // 切片横向数量 
            temp = srcHeight / destHeight; 
            int rows = srcHeight % destHeight == 0 ? temp : temp+1 ;  //切片纵向数量 
              
            // 循环建立切片 
            // 改进的想法:是否可用多线程加快切割速度 
            final Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); 
            for (int i = 0; i < rows; i++) { 
              for (int j = 0; j < cols; j++) { 
                  long bg = System.currentTimeMillis(); 
                         
 
                         
                            // 四个参数分别为图像起点坐标和宽高 
                              // 即: CropImageFilter(int x,int y,int width,int height) 
                              ImageFilter cropFilter = new CropImageFilter(j * destWidth, i * destHeight, Math.min(destWidth,srcWidth - j * destWidth), Math.min(srcHeight - i * destHeight, destHeight)); 
                               
                              ImageProducer imgProducer = image.getSource(); 
                               
                              Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(imgProducer, cropFilter)); 
                               
                              BufferedImage tag = new BufferedImage(Math.min(destWidth, srcWidth - j * destWidth), Math.min(srcHeight - i * destHeight, destHeight), BufferedImage.TYPE_INT_RGB); 
                              Graphics g = tag.getGraphics(); 
                              g.drawImage(img, 0, 0, null); // 绘制小图 
                               
                              g.dispose(); 
                              // 输出为文件 
                              String small_pic_name_path = descDir+"map_" + i + "_" + j + ".jpg"; 
                             
                            ImageIO.write(tag, "JPEG", new File(small_pic_name_path)); 
                            System.out.println("Thread"+Thread.currentThread().getId()+"/t切割第/t"+i+"_"+j+"/t张图片,耗时:/t"+(System.currentTimeMillis()-bg)+" 毫秒"); 
                             
                        } 
                  } 
                  
               
                         
           }catch (Exception e) { 
            e.printStackTrace(); 
          } 
  } 
  /**
   * 图像类型转换
      * GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
      */ 
  public static void convert(String source, String result) { 
   try { 
    File f = new File(source); 
    f.canRead(); 
    f.canWrite(); 
    BufferedImage src = ImageIO.read(f); 
    ImageIO.write(src, "JPG", new File(result)); 
   } catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
  } 
  /**
   * 彩色转为黑白
   * 
   * @param source
   * @param result
   */ 
  public static void gray(String source, String result) { 
   try { 
    BufferedImage src = ImageIO.read(new File(source)); 
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); 
    ColorConvertOp op = new ColorConvertOp(cs, null); 
    src = op.filter(src, null); 
    ImageIO.write(src, "JPEG", new File(result)); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
  } 
  /**
   * @param args
   * 3X2 4X3 4X4 5X4
   */ 
  public static void main(String[] args) { 
//    if(1==1){ 
//        return; 
//    } 
      //cut("ss.jpg", "./ss/", 256, 256); 
      PictureCutter.cut("54.jpg", "./ss/", 256, 256); 
//    Properties p= System.getProperties(); 
//    System.out.println(p); 
  } 
  public static void cut1(String srcImageFile, final String descDir, final int destWidth, 
            final int destHeight) { 
      try {          
            // 读取源图像 
            BufferedImage bi = ImageIO.read(new File(srcImageFile)); 
            final int srcWidth = bi.getHeight(); // 源图宽度 
            final int srcHeight = bi.getWidth(); // 源图高度 
            //如果源图片的宽度和长度都小于目标图片,不用处理。 
            if (srcWidth <= destWidth && srcHeight <= destHeight) return; 
             
            // 计算切片的横向和纵向数量 
            int temp = srcWidth / destWidth; 
            int cols = srcWidth % destWidth == 0 ? temp : temp+1; // 切片横向数量 
            temp = srcHeight / destHeight; 
            int rows = srcHeight % destHeight == 0 ? temp : temp+1 ;  //切片纵向数量 
              
            // 循环建立切片 
            // 改进的想法:是否可用多线程加快切割速度 
            final Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); 
            for (int i = 0; i < rows; i++) { 
              for (int j = 0; j < cols; j++) { 
                  long bg = System.currentTimeMillis(); 
                         
 
                         
                            // 四个参数分别为图像起点坐标和宽高 
                              // 即: CropImageFilter(int x,int y,int width,int height) 
                              ImageFilter cropFilter = new CropImageFilter(j * destWidth, i * destHeight, Math.min(destWidth,srcWidth - j * destWidth), Math.min(srcHeight - i * destHeight, destHeight)); 
                               
                              ImageProducer imgProducer = image.getSource(); 
                               
                              Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(imgProducer, cropFilter)); 
                               
                              BufferedImage tag = new BufferedImage(Math.min(destWidth, srcWidth - j * destWidth), Math.min(srcHeight - i * destHeight, destHeight), BufferedImage.TYPE_INT_RGB); 
                              Graphics g = tag.getGraphics(); 
                               
                              g.drawImage(img, 0, 0, null); // 绘制小图 
                               
                              g.dispose(); 
                              // 输出为文件 
                              String small_pic_name_path = descDir+"map_" + i + "_" + j + ".jpg"; 
                             
                            ImageIO.write(tag, "JPEG", new File(small_pic_name_path)); 
                            System.out.println("Thread"+Thread.currentThread().getId()+"/t切割第/t"+i+"_"+j+"/t张图片,耗时:/t"+(System.currentTimeMillis()-bg)+" 毫秒"); 
                             
                        } 
                  } 
                  
               
                         
           }catch (Exception e) { 
            e.printStackTrace(); 
          } 

   
  public static void perform(){ 
      PictureCutter.cut("ss.jpg", "./ss/", 256, 256); 
      long begin = System.currentTimeMillis(); 
       cut("1.jpg", "./s2/", 128, 128); 
       long mid = System.currentTimeMillis(); 
       PictureCutter.cut("1.jpg", "./s1/", 128, 128); 
       long end = System.currentTimeMillis(); 
       System.out.println(mid-begin); 
       System.out.println(end-mid); 
  } 
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值