Example usage for java.awt.image BufferedImage getHeight

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

Introduction

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

Prototype

public int getHeight() 

Source Link

Document

Returns the height of the BufferedImage .

Usage

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

private static BufferedImage ensureJpgCompatibility(BufferedImage image) {
    BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = imageRGB.createGraphics();
    g2.drawImage(image, 0, 0, imageRGB.getWidth(), imageRGB.getHeight(), Color.WHITE, null);
    g2.dispose();/*from   w w  w.  j ava 2  s.co  m*/
    return imageRGB;
}

From source file:com.vaadin.testbench.screenshot.ImageUtil.java

/**
 * Clones the given BufferedImage/*from  w  ww  .  ja v  a 2s .  c  o  m*/
 * 
 * @param sourceImage
 *            The image to copy
 * @return A copy of sourceImage
 */
public static BufferedImage cloneImage(BufferedImage sourceImage) {
    // This method could likely be optimized but the gain is probably
    // small
    final int w = sourceImage.getWidth();
    final int h = sourceImage.getHeight();

    BufferedImage newImage = new BufferedImage(w, h, TYPE_INT_RGB);

    Graphics2D g = (Graphics2D) newImage.getGraphics();
    g.drawImage(sourceImage, 0, 0, w, h, null);
    g.dispose();

    return newImage;
}

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 w  ww  .  ja  v  a 2s.  co  m*/

    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:common.utils.ImageUtils.java

/**
  * resize input image to new dinesions(only smaller) into rez parameter
  * @param image input image for scaling
 * @param rez resulting image. must have required width and height
 * @throws IOException//from  w ww .  j a v a2 s . c om
  */
public static void getScaledImageDimmension(BufferedImage image, BufferedImage rez) throws IOException {
    Graphics2D g2 = rez.createGraphics();
    if (rez.getHeight() > image.getHeight() || rez.getWidth() > image.getWidth()) {
        //rez image is bigger no resize
        g2.drawImage(image, 0, 0, null);
        return;
    }
    //1-st getting first side to resize (width or height)
    double scale_factor;
    if (getScaling(image.getHeight(), rez.getHeight()) > getScaling(image.getWidth(), rez.getWidth())) {
        //resize height
        scale_factor = getScaling(image.getHeight(), rez.getHeight());
        int width = (int) (scale_factor * image.getWidth());
        //cut width
        int x = (rez.getWidth() - width) / 2;
        g2.drawImage(image.getScaledInstance(width, rez.getHeight(), Image.SCALE_SMOOTH), x, 0, null);
        //System.out.println("resizing height: h="+image.getHeight()+"/"+rez.getHeight()+"; x="+x);
    } else {
        //resize width
        scale_factor = getScaling(image.getWidth(), rez.getWidth());
        int height = (int) (scale_factor * image.getHeight());
        //cut height
        int y = (rez.getHeight() - height) / 2;
        g2.drawImage(image.getScaledInstance(rez.getWidth(), height, Image.SCALE_SMOOTH), 0, y, null);
        //System.out.println("resizing width: w="+image.getWidth()+"/"+rez.getWidth()+"; y="+y);
    }
    g2.dispose();
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img//from   ww  w .  j a v a  2s  .  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.github.zhanhb.ckfinder.connector.utils.ImageUtils.java

/**
 * check if image size isn't bigger then biggest allowed.
 *
 * @param part servlet part/* w ww  .j  a  v  a 2  s.c o  m*/
 * @param context ckfinder context.
 * @return true if image size isn't bigger then biggest allowed.
 * @throws IOException when IO Exception occurs during reading image.
 */
public static boolean checkImageSize(InputStreamSource part, CKFinderContext context) throws IOException {
    ImageProperties image = context.getImage();
    final int maxWidth = image.getMaxWidth();
    final int maxHeight = image.getMaxHeight();
    if (maxHeight == 0 && maxWidth == 0) {
        return true;
    }
    BufferedImage bi;
    try (InputStream stream = part.getInputStream()) {
        bi = ImageIO.read(stream);
    }
    if (bi != null) {
        log.debug("image size: {} {}", bi.getWidth(), bi.getHeight());
        return (maxHeight == 0 || bi.getHeight() <= maxHeight) && (maxWidth == 0 || bi.getWidth() <= maxWidth);
    }
    return false;
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public final static BufferedImage reduceImage(BufferedImage image, int percent) {
    if (percent >= 100)
        return image;
    float fPercent = (float) percent / 100;
    float newWidth = (float) (image.getWidth()) * fPercent;
    float newHeight = (float) (image.getHeight()) * fPercent;
    return reduceImage(image, (int) newWidth, (int) newHeight);
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img/*from w ww  .j a  v  a  2  s. co m*/
 * @param destFile
 * @param sharpen
 * @param sharpenLevel
 * @throws IOException
 */
public static void writeBMP(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.OPAQUE); // Remove alpha-channel from buffered image.

    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("bmp").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:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img/*from   w  ww  .  ja  v  a  2 s  .  c om*/
 * @param destFile
 * @param quality
 * @param sharpen
 * @param sharpenLevel
 * @throws IOException
 */
public static void writeJpeg(Image img, String destFile, float quality, 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.OPAQUE); // Remove alpha-channel from buffered image.

    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("jpeg").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality);
        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

/**
 * ???/*from w  w  w  . jav  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;
}