Example usage for java.awt.image RasterFormatException getMessage

List of usage examples for java.awt.image RasterFormatException getMessage

Introduction

In this page you can find the example usage for java.awt.image RasterFormatException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.iish.visualmets.services.ImageTransformation.java

public BufferedImage CropImage(BufferedImage img, String crop) {
    BufferedImage clipped = null;
    boolean coordinates_okay = false;

    if (!crop.trim().equals("")) {
        coordinates_okay = true;/*from  w  ww .j  ava 2  s.co  m*/

        int[] coordinates = { 0, 0, img.getWidth(), img.getHeight() };

        // split crop value in parts
        String[] crop_coordinates = crop.split(",");

        // if still okay, check if each part is a integer
        if (coordinates_okay) {
            for (int i = 0; i < crop_coordinates.length && i < 4; i++) {
                if (!crop_coordinates[i].trim().equals("")) {
                    if (!Pattern.matches("^-?\\d*$", crop_coordinates[i].trim())) {
                        coordinates_okay = false;
                    } else {
                        coordinates[i] = parseInt(crop_coordinates[i].trim());
                    }
                }
            }
        }

        // als coordinaten negatief, dan vanuit gaan dat het van de 'andere' kant is
        if (coordinates_okay) {
            if (coordinates[0] < 0) {
                coordinates[0] = img.getWidth() + coordinates[0];
            }

            if (coordinates[1] < 0) {
                coordinates[1] = img.getHeight() + coordinates[1];
            }

            if (coordinates[2] < 0) {
                coordinates[2] = img.getWidth() + coordinates[2];
            }

            if (coordinates[3] < 0) {
                coordinates[3] = img.getHeight() + coordinates[3];
            }
        }

        // alle coordinaten moeten op dit moment positieve getallen binnen de image grootte/breedte zijn
        if (coordinates_okay) {
            if (coordinates[0] > img.getWidth() || coordinates[0] < 0) {
                coordinates_okay = false;
            }

            if (coordinates[1] > img.getHeight() || coordinates[1] < 0) {
                coordinates_okay = false;
            }

            if (coordinates[2] > img.getWidth() || coordinates[2] < 0) {
                coordinates_okay = false;
            }

            if (coordinates[3] > img.getHeight() || coordinates[3] < 0) {
                coordinates_okay = false;
            }
        }

        // controleer of de linker/boven waarde kleiner is dan de rechter/onder coordinaat
        if (coordinates_okay) {
            if (coordinates[0] >= coordinates[2]) {
                coordinates_okay = false;
            }
            if (coordinates[1] >= coordinates[3]) {
                coordinates_okay = false;
            }
        }

        if (coordinates_okay) {
            // if still okay, then get cropped image
            try {
                int w = coordinates[2] - coordinates[0];
                int h = coordinates[3] - coordinates[1];

                clipped = img.getSubimage(coordinates[0], coordinates[1], w, h);
            } catch (RasterFormatException rfe) {
                System.out.println("raster format error: " + rfe.getMessage());
                coordinates_okay = false;
            }
        }
    }

    if (!coordinates_okay) {
        clipped = img;
    }

    return clipped;
}