Java Image divideImage(Object obj, int rows, int cols)

Here you can find the source of divideImage(Object obj, int rows, int cols)

Description

divide Image

License

Open Source License

Declaration

public static List<BufferedImage> divideImage(Object obj, int rows, int cols) 

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;/*from w w  w.  j  av  a  2  s  . com*/

        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. createTiledImage(Image img, int width, int height)
  2. crop(Image source, int x1, int y1, int x2, int y2)
  3. displayImage(Image image)
  4. displayImage(Image img)
  5. displayImage(Image img, String mesg, int locx, int locy, int sizex, int sizey)
  6. ensureImageLoaded(Image img)
  7. findImagefromClasspath(Class relToThisClass, String relImageName)
  8. generateImageVisualizationPanel(String path, int width, int height, int scaling)
  9. getChatLogoImage(int width, int height)