java实现图片固定长宽的裁剪(不缩放)

java实现图片固定长宽的裁剪(不缩放)

将aa文件夹中的所有图片裁剪成固定长宽的图片 (不缩放)

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class DeepSearchDir {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        File dir=new File("E:\\aa");
        listDir(dir,1);
    }

    private static void listDir(File dir,int level) throws IOException {
    	String dirString=dir.toString();
        File files[]=dir.listFiles();
        level++;
        for(File f:files){
            if(f.isDirectory()){   	
                listDir(f,level);       
            }
            else {       
            	 BufferedImage bufferedimage=ImageIO.read(f);
                 int width = bufferedimage.getWidth();
                 int height = bufferedimage.getHeight();
                 //目标将图片裁剪成 宽500,高300
                 if (width > 500) {
                                                                     /*开始x坐标              开始y坐标             结束x坐标                     结束y坐标*/
                     bufferedimage=ImgUtils.cropImage(bufferedimage,(int) ((width - 500) / 2),0,(int) (width - (width-500) / 2),(int) (height)
                             );
                     if (height > 300) {
                         bufferedimage=ImgUtils.cropImage(bufferedimage,0,(int) ((height - 300) / 2),500,(int) (height - (height - 300) / 2)
                                 );
                     }
                 }else{
                     if (height > 300) {
                         bufferedimage=ImgUtils.cropImage(bufferedimage,0,(int) ((height - 300) / 2),(int) (width),(int) (height - (height - 300) / 2)
                                 );
                     }
                 }
                 ImageIO.write(bufferedimage, "jpg", f);    //输出裁剪图片
             }
            }
        }
   
    /**
     * 裁剪图片方法
     * @param bufferedImage 图像源
     * @param startX 裁剪开始x坐标
     * @param startY 裁剪开始y坐标
     * @param endX 裁剪结束x坐标
     * @param endY 裁剪结束y坐标
     * @return
     */
    public static BufferedImage cropImage(BufferedImage Image, int startX, int startY, int endX, int endY) {
           int width = Image.getWidth();
           int height = Image.getHeight();
           if (startX == -1) {
               startX = 0;
           }
           if (startY == -1) {
               startY = 0;
           }
           if (endX == -1) {
               endX = width - 1;
           }
           if (endY == -1) {
               endY = height - 1;
           }
           BufferedImage result = new BufferedImage(endX - startX, endY - startY, 4);
           for (int x = startX; x < endX; ++x) {
               for (int y = startY; y < endY; ++y) {
                   int rgb = Image.getRGB(x, y);
                   result.setRGB(x - startX, y - startY, rgb);
               }
           }
           return result;
       }
      }

猜你喜欢

转载自blog.csdn.net/qq_33271461/article/details/85252110
今日推荐