Example usage for java.awt.image BufferedImage createGraphics

List of usage examples for java.awt.image BufferedImage createGraphics

Introduction

In this page you can find the example usage for java.awt.image BufferedImage createGraphics.

Prototype

public Graphics2D createGraphics() 

Source Link

Document

Creates a Graphics2D , which can be used to draw into this BufferedImage .

Usage

From source file:com.alvermont.terraj.stargen.ui.UIUtils.java

/**
 * Scale an image to a particular X and Y size in pixels
 * // ww  w  . jav a  2s  .com
 * @param image The image to be scaled
 * @param pixelsX The desired horizontal size in pixels
 * @param pixelsY The desired vertical size in pixels
 * @return A new image scaled to the requested dimensions
 */
public static BufferedImage scaleImage(BufferedImage image, int pixelsX, int pixelsY) {
    int actualWidth = image.getWidth();
    int actualHeight = image.getHeight();

    // work out the scale factor

    double sx = (float) (pixelsX) / actualWidth;
    double sy = (float) (pixelsY) / actualHeight;

    BufferedImage nbi = new BufferedImage(pixelsX, pixelsY, image.getType());

    Graphics2D g2 = nbi.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance(sx, sy);

    g2.setTransform(at);

    g2.drawImage(image, 0, 0, null);

    return nbi;
}

From source file:imageLines.ImageHelpers.java

public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int w, h;// w  w w.  j  a  v  a 2 s . c o m
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:com.reydentx.core.common.PhotoUtils.java

public static byte[] scaleBufferedImage(boolean isPNG, BufferedImage originalImage, int x, int y, int width,
        int height) {
    byte[] imageFinal = null;
    try {/*from  ww w. j a  v  a2 s.c  o  m*/
        if (originalImage != null) {
            int type = (originalImage.getType() == 0) ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
            BufferedImage resizedImage = new BufferedImage(width, height, type);
            Graphics2D g = resizedImage.createGraphics();
            ByteArrayOutputStream outstream = new ByteArrayOutputStream();
            g.drawImage(originalImage, x, y, width, height, null);
            g.dispose();

            if (isPNG) {
                ImageIO.write(resizedImage, "png", outstream);
            } else {
                ImageIO.write(resizedImage, "jpg", outstream);
            }
            imageFinal = outstream.toByteArray();
        }
    } catch (IOException ex) {
    }
    return imageFinal;
}

From source file:de.bamamoto.mactools.png2icns.Scaler.java

public static BufferedImage resize(BufferedImage source, int width, int height) {

    double xScale = ((double) width) / (double) source.getWidth();
    double yScale = ((double) height) / (double) source.getHeight();
    BufferedImage result = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getDefaultConfiguration()/*  ww w. j  ava2s  .c om*/
            .createCompatibleImage(width, height, source.getColorModel().getTransparency());
    Graphics2D newImage = null;
    try {
        newImage = result.createGraphics();
        newImage.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        newImage.drawRenderedImage(source, AffineTransform.getScaleInstance(xScale, yScale));
    } finally {
        if (newImage != null) {
            newImage.dispose();
        }
    }
    return result;
}

From source file:com.mirth.connect.server.util.DICOMUtil.java

private static String returnOtherImageFormat(MessageObject message, String format, boolean autoThreshold) {
    // use new method for jpegs
    if (format.equalsIgnoreCase("jpg") || format.equalsIgnoreCase("jpeg")) {
        return new String(Base64.encodeBase64Chunked(dicomToJpg(1, message, autoThreshold)));
    }/*www . ja v a  2 s  .c  om*/

    byte[] rawImage = Base64.decodeBase64(getDICOMRawData(message).getBytes());
    ByteArrayInputStream bais = new ByteArrayInputStream(rawImage);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        DICOM dicom = new DICOM(bais);
        dicom.run(message.getType());
        BufferedImage image = new BufferedImage(dicom.getWidth(), dicom.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.createGraphics();
        graphics.drawImage(dicom.getImage(), 0, 0, null);
        graphics.dispose();
        ImageIO.write(image, format, baos);
        return new String(Base64.encodeBase64Chunked(baos.toByteArray()));
    } catch (IOException e) {
        logger.error("Error Converting DICOM image", e);
    } finally {
        IOUtils.closeQuietly(bais);
        IOUtils.closeQuietly(baos);
    }

    return StringUtils.EMPTY;
}

From source file:com.alvermont.terraj.stargen.ui.UIUtils.java

/**
 * Combine a list of images horizontally into one new image.
 * //from  w w  w. j av  a  2 s.  com
 * @param images The list of images to be combined
 * @return A new image, as wide horizontally as the sum of the input images,
 * containing all the input images
 */
public static BufferedImage combineImagesHorizontal(List<BufferedImage> images) {
    int imageType = -1;
    int height = 0;
    int width = 0;

    // first work out the sizing and image type, we assume the images
    // are all compatible.

    for (BufferedImage image : images) {
        if (imageType == -1) {
            imageType = image.getType();
        }

        width += image.getWidth();

        height = Math.max(height, image.getHeight());
    }

    // create the new image and clear it to black

    BufferedImage bi = new BufferedImage(width, height, imageType);

    Graphics2D g = bi.createGraphics();
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, width, height);

    // merge the images into the new one

    int xpos = 0;

    for (BufferedImage image : images) {
        int ypos = (height - image.getHeight()) / 2;

        g.drawImage(image, xpos, ypos, null);

        xpos += image.getWidth();
    }

    return bi;
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img//from w  ww  .  ja  va2  s. c o  m
 * @param destFile
 * @param sharpen
 * @param sharpenLevel
 * @throws IOException
 */
public static void writePng(Image img, String destFile, boolean sharpen, float sharpenLevel)
        throws IOException {
    sharpenMatrix = calculeSharpenMatrix(sharpenLevel);
    BufferedImage imageRGBSharpen = null;
    IIOImage iioImage = null;
    BufferedImage image = SwingFXUtils.fromFXImage(img, null); // Get buffered image.
    BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.BITMASK);
    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    if (sharpen) {
        imageRGBSharpen = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }
    ImageWriter writer = null;
    FileImageOutputStream output = null;
    try {
        writer = ImageIO.getImageWritersByFormatName("png").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        output = new FileImageOutputStream(new File(destFile));
        writer.setOutput(output);
        if (sharpen) {
            iioImage = new IIOImage(imageRGBSharpen, null, null);
        } else {
            iioImage = new IIOImage(imageRGB, null, null);
        }
        writer.write(null, iioImage, param);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (output != null) {
            output.close();
        }
    }
    graphics.dispose();
}

From source file:com.liusoft.dlog4j.util.ImageUtils.java

/**
 * ???/*ww w.j  a  v a2s.  c o m*/
 * ?????
 * 3: 180
 * 6: 90
 * 8: 27090
 * @param img_fn
 * @param orient
 * @throws IOException 
 */
public static boolean rotateImage(String img_fn, int orient, String dest_fn) throws IOException {
    double radian = 0;
    switch (orient) {
    case 3:
        radian = 180.0;
        break;
    case 6:
        radian = 90.0;
        break;
    case 8:
        radian = 270.0;
        break;
    default:
        return false;
    }
    BufferedImage old_img = (BufferedImage) ImageIO.read(new File(img_fn));
    int width = old_img.getWidth();
    int height = old_img.getHeight();

    BufferedImage new_img = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = new_img.createGraphics();

    AffineTransform origXform = g2d.getTransform();
    AffineTransform newXform = (AffineTransform) (origXform.clone());
    // center of rotation is center of the panel
    double xRot = 0;
    double yRot = 0;
    switch (orient) {
    case 3:
        xRot = width / 2.0;
        yRot = height / 2.0;
    case 6:
        xRot = height / 2.0;
        yRot = xRot;
        break;
    case 8:
        xRot = width / 2.0;
        yRot = xRot;
        break;
    default:
        return false;
    }
    newXform.rotate(Math.toRadians(radian), xRot, yRot);

    g2d.setTransform(newXform);
    // draw image centered in panel
    g2d.drawImage(old_img, 0, 0, null);
    // Reset to Original
    g2d.setTransform(origXform);

    FileOutputStream out = new FileOutputStream(dest_fn);
    try {
        ImageIO.write(new_img, "JPG", out);
    } finally {
        out.close();
    }
    return true;
}

From source file:net.sf.mcf2pdf.mcfelements.util.ImageUtil.java

public static BufferedImage readImage(File imageFile) throws IOException {
    int rotation = getImageRotation(imageFile);
    BufferedImage img = ImageIO.read(imageFile);

    if (rotation == 0) {
        return img;
    }/*from   ww  w  .j a  v a  2 s .c om*/

    boolean swapXY = rotation != 180;

    BufferedImage rotated = new BufferedImage(swapXY ? img.getHeight() : img.getWidth(),
            swapXY ? img.getWidth() : img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = rotated.createGraphics();
    g2d.translate((rotated.getWidth() - img.getWidth()) / 2, (rotated.getHeight() - img.getHeight()) / 2);
    g2d.rotate(Math.toRadians(rotation), img.getWidth() / 2, img.getHeight() / 2);

    g2d.drawImage(img, 0, 0, null);
    g2d.dispose();

    return rotated;
}

From source file:com.smash.revolance.ui.model.helper.ImageHelper.java

public static BufferedImage eraseRect(BufferedImage image, int x, int y, int w, int h, double xScale,
        double yScale) {
    x = (int) (x * xScale);
    w = (int) (w * xScale);

    y = (int) (y * yScale);
    h = (int) (h * yScale);

    Graphics2D g = image.createGraphics();
    g.setColor(new Color(0, 0, 0, 0));
    g.fillRect(x, y, w, h);//from   w ww  .  ja  va  2s  .  co m
    g.dispose();
    return image;
}