Example usage for java.awt Graphics2D drawImage

List of usage examples for java.awt Graphics2D drawImage

Introduction

In this page you can find the example usage for java.awt Graphics2D drawImage.

Prototype

public abstract boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as is currently available.

Usage

From source file:business.model.CaptchaModel.java

/**
 *
 * @param baseImage//from ww  w.j a va 2s . c  o  m
 * @return
 */
private static BufferedImage getDistortedImage(BufferedImage baseImage) {
    BufferedImage distortedImage = new BufferedImage(baseImage.getWidth(), baseImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D graph = (Graphics2D) distortedImage.getGraphics();

    ShadowFilter shadowFilter = new ShadowFilter();
    shadowFilter.setRadius(10);
    shadowFilter.setDistance(5);
    shadowFilter.setOpacity(1);

    Random rand = new Random();

    RippleFilter rippleFilter = new RippleFilter();
    rippleFilter.setWaveType(RippleFilter.SINE);
    rippleFilter.setXAmplitude(1.6f);
    rippleFilter.setYAmplitude(1.0f);
    rippleFilter.setXWavelength(5);
    rippleFilter.setYWavelength(2);
    rippleFilter.setEdgeAction(TransformFilter.BILINEAR);

    BufferedImage effectImage = rippleFilter.filter(baseImage, null);
    effectImage = shadowFilter.filter(effectImage, null);

    graph.drawImage(effectImage, 0, 0, null, null);
    graph.dispose();

    // draw lines over the image and/or text
    //      noiseProducer.makeNoise(distortedImage, .1f, .1f, .25f, .25f);
    return distortedImage;
}

From source file:au.org.ala.biocache.web.WMSController.java

/**
 * Method that produces the downloadable map integrated in AVH/OZCAM/Biocache.
 *
 * @param requestParams/*w w  w .j a  v  a2 s .co m*/
 * @param format
 * @param extents
 * @param widthMm
 * @param pointRadiusMm
 * @param pradiusPx
 * @param pointColour
 * @param pointOpacity
 * @param baselayer
 * @param scale
 * @param dpi
 * @param outlinePoints
 * @param outlineColour
 * @param fileName
 * @param request
 * @param response
 * @throws Exception
 */
@RequestMapping(value = { "/webportal/wms/image", "/mapping/wms/image" }, method = RequestMethod.GET)
public void generatePublicationMap(SpatialSearchRequestParams requestParams,
        @RequestParam(value = "format", required = false, defaultValue = "jpg") String format,
        @RequestParam(value = "extents", required = true) String extents,
        @RequestParam(value = "widthmm", required = false, defaultValue = "60") Double widthMm,
        @RequestParam(value = "pradiusmm", required = false, defaultValue = "2") Double pointRadiusMm,
        @RequestParam(value = "pradiuspx", required = false) Integer pradiusPx,
        @RequestParam(value = "pcolour", required = false, defaultValue = "FF0000") String pointColour,
        @RequestParam(value = "popacity", required = false, defaultValue = "0.8") Double pointOpacity,
        @RequestParam(value = "baselayer", required = false, defaultValue = "world") String baselayer,
        @RequestParam(value = "scale", required = false, defaultValue = "off") String scale,
        @RequestParam(value = "dpi", required = false, defaultValue = "300") Integer dpi,
        @RequestParam(value = "outline", required = true, defaultValue = "false") boolean outlinePoints,
        @RequestParam(value = "outlineColour", required = true, defaultValue = "#000000") String outlineColour,
        @RequestParam(value = "fileName", required = false) String fileName, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    String[] bb = extents.split(",");

    double long1 = Double.parseDouble(bb[0]);
    double lat1 = Double.parseDouble(bb[1]);
    double long2 = Double.parseDouble(bb[2]);
    double lat2 = Double.parseDouble(bb[3]);

    if (lat1 <= -90) {
        lat1 = -89.999;
    }
    if (lat2 >= 90) {
        lat2 = 89.999;
    }

    int pminx = convertLngToPixel(long1);
    int pminy = convertLatToPixel(lat1);
    int pmaxx = convertLngToPixel(long2);
    int pmaxy = convertLatToPixel(lat2);

    int width = (int) ((dpi / 25.4) * widthMm);
    int height = (int) Math.round(width * ((pminy - pmaxy) / (double) (pmaxx - pminx)));

    if (height * width > MAX_IMAGE_PIXEL_COUNT) {
        String errorMessage = "Image size in pixels " + width + "x" + height + " exceeds "
                + MAX_IMAGE_PIXEL_COUNT + " pixels.  Make the image smaller";
        response.sendError(response.SC_NOT_ACCEPTABLE, errorMessage);
        throw new Exception(errorMessage);
    }

    int pointSize = -1;
    if (pradiusPx != null) {
        pointSize = (int) pradiusPx;
    } else {
        pointSize = (int) ((dpi / 25.4) * pointRadiusMm);
    }

    double[] boundingBox = transformBbox4326To900913(Double.parseDouble(bb[0]), Double.parseDouble(bb[1]),
            Double.parseDouble(bb[2]), Double.parseDouble(bb[3]));

    //"http://biocache.ala.org.au/ws/webportal/wms/reflect?
    //q=macropus&ENV=color%3Aff0000%3Bname%3Acircle%3Bsize%3A3%3Bopacity%3A1
    //&BBOX=12523443.0512,-2504688.2032,15028131.5936,0.33920000120997&WIDTH=256&HEIGHT=256");
    String speciesAddress = baseWsUrl + "/ogc/wms/reflect?" + "ENV=color%3A" + pointColour
            + "%3Bname%3Acircle%3Bsize%3A" + pointSize + "%3Bopacity%3A" + pointOpacity + "&BBOX="
            + boundingBox[0] + "," + boundingBox[1] + "," + boundingBox[2] + "," + boundingBox[3] + "&WIDTH="
            + width + "&HEIGHT=" + height + "&OUTLINE=" + outlinePoints + "&OUTLINECOLOUR=" + outlineColour
            + "&" + request.getQueryString();

    URL speciesURL = new URL(speciesAddress);
    BufferedImage speciesImage = ImageIO.read(speciesURL);

    //"http://spatial.ala.org.au/geoserver/wms/reflect?
    //LAYERS=ALA%3Aworld&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=
    //&FORMAT=image%2Fjpeg&SRS=EPSG%3A900913&BBOX=12523443.0512,-1252343.932,13775787.3224,0.33920000004582&WIDTH=256&HEIGHT=256"
    String layout = "";
    if (!scale.equals("off")) {
        layout += "layout:scale";
    }
    String basemapAddress = geoserverUrl + "/wms/reflect?" + "LAYERS=ALA%3A" + baselayer
            + "&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=" + "&FORMAT=image%2Fpng&SRS=EPSG%3A900913" //specify the mercator projection
            + "&BBOX=" + boundingBox[0] + "," + boundingBox[1] + "," + boundingBox[2] + "," + boundingBox[3]
            + "&WIDTH=" + width + "&HEIGHT=" + height + "&OUTLINE=" + outlinePoints + "&format_options=dpi:"
            + dpi + ";" + layout;

    BufferedImage basemapImage = ImageIO.read(new URL(basemapAddress));

    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D combined = (Graphics2D) img.getGraphics();

    combined.drawImage(basemapImage, 0, 0, Color.WHITE, null);
    //combined.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, pointOpacity.floatValue()));
    combined.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
    combined.drawImage(speciesImage, null, 0, 0);
    combined.dispose();

    //if filename supplied, force a download
    if (fileName != null) {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Description", "File Transfer");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setHeader("Content-Transfer-Encoding", "binary");
    } else if (format.equalsIgnoreCase("png")) {
        response.setContentType("image/png");
    } else {
        response.setContentType("image/jpeg");
    }

    if (format.equalsIgnoreCase("png")) {
        OutputStream os = response.getOutputStream();
        ImageIO.write(img, format, os);
        os.close();
    } else {
        //handle jpeg + BufferedImage.TYPE_INT_ARGB
        BufferedImage img2;
        Graphics2D c2;
        (c2 = (Graphics2D) (img2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)).getGraphics())
                .drawImage(img, 0, 0, Color.WHITE, null);
        c2.dispose();
        OutputStream os = response.getOutputStream();
        ImageIO.write(img2, format, os);
        os.close();
    }
}