Here you can find the source of splitImageIntoTiles(final BufferedImage imageWithTiles, final int numberOfTilesAcross, final int numberOfTilesDown)
Parameter | Description |
---|---|
imageWithTiles | the source image, divided into tiles along the x and y direction. Should have a size that is a multiple of the number of tiles in each direction, so that all tiles get the same size. |
numberOfTilesAcross | number of columns of tiles in the source image |
numberOfTilesDown | number of rows of tiles in the source image |
public static BufferedImage[] splitImageIntoTiles(final BufferedImage imageWithTiles, final int numberOfTilesAcross, final int numberOfTilesDown)
//package com.java2s; /*// www . j a v a2s.c om * GeoTools - The Open Source Java GIS Toolkit * http://geotools.org * * (C) 2007-2008, Open Source Geospatial Foundation (OSGeo) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import java.awt.image.BufferedImage; public class Main { /** * Creates subimages for each tile in the specified source image. * The returned subimages share the same image data with the source image. * * @param imageWithTiles the source image, divided into tiles along the x and y direction. * Should have a size that is a multiple of the number of tiles in each direction, so that all tiles get the same size. * @param numberOfTilesAcross number of columns of tiles in the source image * @param numberOfTilesDown number of rows of tiles in the source image * * @return an array with the tiles, ordered by rows from left to right. */ public static BufferedImage[] splitImageIntoTiles(final BufferedImage imageWithTiles, final int numberOfTilesAcross, final int numberOfTilesDown) { final int tileWidth = imageWithTiles.getWidth() / numberOfTilesAcross; final int tileHeight = imageWithTiles.getHeight() / numberOfTilesDown; final int totalNumberOfTiles = numberOfTilesDown * numberOfTilesAcross; final BufferedImage[] tiles = new BufferedImage[totalNumberOfTiles]; for (int y = 0; y < numberOfTilesDown; y++) { for (int x = 0; x < numberOfTilesAcross; x++) { tiles[x + y * numberOfTilesAcross] = imageWithTiles.getSubimage(x * tileWidth, y * tileHeight, tileWidth, tileHeight); } } return tiles; } }