Java Image Cut cut2(String srcImageFile, String descDir, int rows, int cols)

Here you can find the source of cut2(String srcImageFile, String descDir, int rows, int cols)

Description

cut

License

Apache License

Declaration

public static final void cut2(String srcImageFile, String descDir, int rows, int cols) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Graphics;

import java.awt.Image;
import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;

import javax.imageio.ImageIO;

public class Main {
    public static final void cut2(String srcImageFile, String descDir, int rows, int cols) {
        try {//from   w  w  w  .j  av a 2 s  .  c om
            if ((rows <= 0) || (rows > 20)) {
                rows = 2;
            }
            if ((cols <= 0) || (cols > 20)) {
                cols = 2;
            }
            BufferedImage bi = ImageIO.read(new File(srcImageFile));
            int srcWidth = bi.getHeight();
            int srcHeight = bi.getWidth();
            if ((srcWidth > 0) && (srcHeight > 0)) {
                Image image = bi.getScaledInstance(srcWidth, srcHeight, 1);
                int destWidth = srcWidth;
                int destHeight = srcHeight;
                if (srcWidth % cols == 0) {
                    destWidth = srcWidth / cols;
                } else {
                    destWidth = (int) Math.floor(srcWidth / cols) + 1;
                }
                if (srcHeight % rows == 0) {
                    destHeight = srcHeight / rows;
                } else {
                    destHeight = (int) Math.floor(srcWidth / rows) + 1;
                }
                for (int i = 0; i < rows; i++) {
                    for (int j = 0; j < cols; j++) {
                        ImageFilter cropFilter = new CropImageFilter(j * destWidth, i * destHeight, destWidth,
                                destHeight);
                        Image img = Toolkit.getDefaultToolkit()
                                .createImage(new FilteredImageSource(image.getSource(), cropFilter));
                        BufferedImage tag = new BufferedImage(destWidth, destHeight, 1);
                        Graphics g = tag.getGraphics();
                        g.drawImage(img, 0, 0, null);
                        g.dispose();

                        ImageIO.write(tag, "JPEG", new File(descDir + "_r" + i + "_c" + j + ".jpg"));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. cropImage(Image image, int x, int y, int width, int height)
  2. cropImage(Image img, int x, int y, int w, int h)
  3. cropImage(Image originalImage, int left, int top, int right, int bottom)
  4. cropImage(URL url, float x, float y, float w, float h, OutputStream out)
  5. cut(String srcImageFile, String result, int x, int y, int width, int height)
  6. cutImage(BufferedImage image, int posX, int posY, int width, int height)
  7. cutImage(BufferedImage img, int w, int h)
  8. cutImage(File file, int x, int y, int width, int heigth)
  9. cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH)