图片按固定宽度压缩成RGB格式

 public static void generalSmallImg(String path, String pathSmall, int nw) throws IOException{
          File fi = new File(path); //大图文件  
          File fo = new File(pathSmall); //将要转换出的小图文件  
        if(!fo.getParentFile().exists()){  
        fo.getParentFile().mkdirs();  
        }
          BufferedImage bis = ImageIO.read(fi); //读取图片  
          int w = bis.getWidth();  
          int h = bis.getHeight();  
          int nh = (nw*h)/w ;  
          
          Image img = ImageIO.read(fi);
          BufferedImage bi = new BufferedImage(nw, nh, BufferedImage.TYPE_INT_RGB);
          Graphics g = bi.getGraphics();
          g.drawImage(img, 0, 0, nw, nh, Color.LIGHT_GRAY, null);
          g.dispose();
          // 将图片保存并加上后缀

          ImageIO.write(bi,"jpeg",fo);

}

猜你喜欢

转载自blog.csdn.net/mengda_lei/article/details/80409039