Java BufferedImage Crop divideImage(BufferedImage bi, int rows, int cols, int width, int height)

Here you can find the source of divideImage(BufferedImage bi, int rows, int cols, int width, int height)

Description

divide Image

License

Open Source License

Declaration

public static List<BufferedImage> divideImage(BufferedImage bi, int rows, int cols, int width, int height) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;

import java.awt.image.*;

import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class Main {

    public static List<BufferedImage> divideImage(Object obj, int rows, int cols) {
        BufferedImage bi;/*  www . java 2  s . co m*/

        if (obj instanceof Image) {
            bi = toBufferedImage((Image) obj);
        } else if (obj instanceof ImageIcon) {
            bi = toBufferedImage(((ImageIcon) obj).getImage());
        } else {
            bi = (BufferedImage) obj;
        }

        int altoBi = bi.getHeight() / rows;
        int anchoBi = bi.getWidth() / cols;

        List<BufferedImage> imagenes = new ArrayList<>();
        BufferedImage subImagen;

        for (int f = 0; f < rows; f++) {
            for (int c = 0; c < cols; c++) {
                subImagen = bi.getSubimage(c * anchoBi, f * altoBi, anchoBi, altoBi);
                imagenes.add(subImagen);
            }
        }

        return imagenes;
    }

    public static List<BufferedImage> divideImage(BufferedImage bi, int rows, int cols, int width, int height) {
        int x, y;
        List<BufferedImage> imagenes = new ArrayList<>();

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                x = j * width;
                y = i * height;
                imagenes.add(bi.getSubimage(x, y, width, height));
            }
        }

        return imagenes;
    }

    /**
     * Convierte un objeto Image a un BufferedImage
     *
     * @param img El objeto Image a convertir
     * @return El BufferedImage obtenido
     */
    public static BufferedImage toBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }
        // Crea un buffered image con trasparencia
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        // Dibuja la imagen en el buffered image
        Graphics2D bGr = bimage.createGraphics();
        bGr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();
        // Retorna el buffered image
        return bimage;
    }

    /**
     * Obtiene el alto de una Image
     */
    public static int getHeight(Image imagen) {
        int height = new ImageIcon(imagen).getIconHeight();
        return height;
    }

    /**
     * Obtiene el alto de un IconImage
     */
    public static int getHeight(ImageIcon imagen) {
        return imagen.getIconHeight();
    }

    /**
     * Obtiene el ancho de una Image
     */
    public static int getWidth(Image imagen) {
        int width = new ImageIcon(imagen).getIconWidth();
        return width;
    }

    /**
     * Obtiene el ancho de un IconImage
     */
    public static int getWidth(ImageIcon imagen) {
        return imagen.getIconWidth();
    }
}

Related

  1. cropImageRelative(BufferedImage image, double leftCropFactor, double rightCropFactor, double topCropFactor, double bottomCropFactor)
  2. croppedCopy(BufferedImage image, int startX, int startY, int endX, int endY)
  3. cropScale(BufferedImage source, int width, int height)
  4. cropToAspectRatio(BufferedImage image, float aspect)
  5. cropToBeSameSize(BufferedImage image1, BufferedImage image2)
  6. doCrop(BufferedImage bufferedImage, int cropX, int cropY, int width, int height)
  7. drawCropped(JPanel contentPane, ActionListener listener, BufferedImage img, int type, int sx1, int sy1, int sx2, int sy2, int x, int y, int scale)
  8. ImageCrop(String sourcePath, String descPath, int cropX, int cropY, int width, int height)
  9. imgUtilFastCrop(BufferedImage src, int x, int y, int width, int height)