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:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Calculate the SSIM between two files//from   w  w w. j av  a 2s  . co m
 * @param pOne first image to compare
 * @param pTwo second image to compare
 * @param pHeatMapFilename ssim heat map image filename (can be null)
 * @param pMin list for return value - ssim minimum (can be null)
 * @param pVariance list for return value - ssim variance (can be null)
 * @return calculated ssim
 */
public static double calcSSIM(final File pOne, final File pTwo, final String pHeatMapFilename,
        List<Double> pMin, List<Double> pVariance) {

    BufferedImage imageOne = null;
    try {
        imageOne = Imaging.getBufferedImage(pOne);
    } catch (IOException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] oneA = imageOne.getRGB(0, 0, imageOne.getWidth(), imageOne.getHeight(), null, 0,
            imageOne.getWidth());
    final int width = imageOne.getWidth();
    final int height = imageOne.getHeight();
    final boolean greyscale = (imageOne.getType() == BufferedImage.TYPE_BYTE_GRAY
            || imageOne.getType() == BufferedImage.TYPE_USHORT_GRAY);
    imageOne = null;

    BufferedImage imageTwo = null;
    try {
        imageTwo = Imaging.getBufferedImage(pTwo);
    } catch (IOException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] twoA = imageTwo.getRGB(0, 0, imageTwo.getWidth(), imageTwo.getHeight(), null, 0,
            imageTwo.getWidth());
    imageTwo = null;

    final double ssim = calcSSIM(oneA, twoA, width, height, greyscale, pHeatMapFilename, pMin, pVariance);

    return ssim;
}

From source file:brut.androlib.res.decoder.Res9patchStreamDecoder.java

@Override
public void decode(InputStream in, OutputStream out) throws AndrolibException {
    try {/*from   ww w .  jav  a  2  s .  c om*/
        byte[] data = IOUtils.toByteArray(in);

        BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
        int w = im.getWidth(), h = im.getHeight();

        ImageTypeSpecifier its = ImageTypeSpecifier.createFromRenderedImage(im);
        BufferedImage im2 = its.createBufferedImage(w + 2, h + 2);

        im2.getRaster().setRect(1, 1, im.getRaster());
        NinePatch np = getNinePatch(data);
        drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
        drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);

        int[] xDivs = np.xDivs;
        for (int i = 0; i < xDivs.length; i += 2) {
            drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
        }

        int[] yDivs = np.yDivs;
        for (int i = 0; i < yDivs.length; i += 2) {
            drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
        }

        ImageIO.write(im2, "png", out);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    } catch (NullPointerException ex) {
        // In my case this was triggered because a .png file was
        // containing a html document instead of an image.
        // This could be more verbose and try to MIME ?
        throw new AndrolibException(ex);
    }
}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Scale an image and return the dimensions (width and height) as int array
 *
 * @param original  original file/*www  .  jav a 2  s  .  c  om*/
 * @param scaled    scaled file
 * @param extension extension
 * @param width     desired width
 * @param height    desired height
 * @return actual width ([0]) and height ([1]) of scaled image
 * @throws FxApplicationException on errors
 */
public static int[] scale(File original, File scaled, String extension, int width, int height)
        throws FxApplicationException {
    if (HEADLESS && FxMediaImageMagickEngine.IM_AVAILABLE && ".GIF".equals(extension)) {
        //native headless engine can't handle gif transparency ... so if we have IM we use it, else
        //transparent pixels will be black
        return FxMediaImageMagickEngine.scale(original, scaled, extension, width, height);
    }
    BufferedImage bi;
    try {
        bi = ImageIO.read(original);
    } catch (Exception e) {
        LOG.info("Failed to read " + original.getName() + " using ImageIO, trying sanselan");
        try {
            bi = Sanselan.getBufferedImage(original);
        } catch (Exception e1) {
            throw new FxApplicationException(LOG, "ex.media.readFallback.error", original.getName(), extension,
                    e.getMessage(), e1.getMessage());
        }
    }
    BufferedImage bi2 = scale(bi, width, height);

    String eMsg;
    boolean fallback;
    try {
        fallback = !ImageIO.write(bi2, extension.substring(1), scaled);
        eMsg = "No ImageIO writer found.";
    } catch (Exception e) {
        eMsg = e.getMessage();
        fallback = true;
    }
    if (fallback) {
        try {
            Sanselan.writeImage(bi2, scaled, getImageFormatByExtension(extension), new HashMap());
        } catch (Exception e1) {
            throw new FxApplicationException(LOG, "ex.media.write.error", scaled.getName(), extension,
                    eMsg + ", " + e1.getMessage());
        }
    }
    return new int[] { bi2.getWidth(), bi2.getHeight() };
}

From source file:de.anycook.upload.UploadHandler.java

/**
 * speichert eine grosse Version des Bildes
 *
 * @param image    BufferedImage//from   w ww . j a  va  2s.  c o  m
 * @param filename Name der zu erzeugenden Datei
 */
private void saveOriginalImage(BufferedImage image, String filename) throws IOException {
    int height = image.getHeight();
    int width = image.getWidth();

    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    newImage.getGraphics().drawImage(image, 0, 0, null);

    imageSaver.save("original/", filename, newImage);

}

From source file:org.xlrnet.tibaija.tools.fontgen.FontgenApplication.java

private Symbol importFile(Path path, Font font, String fontIdentifier) throws IOException, ImageReadException {
    LOGGER.info("Importing file {} ...", path.toAbsolutePath());

    BufferedImage image = Imaging.getBufferedImage(Files.newInputStream(path));
    int width = image.getWidth();
    int height = image.getHeight();
    int finalWidth = width / 2;
    int finalHeight = height / 2;

    if (width % 2 != 0 || height % 2 != 0) {
        LOGGER.warn("Width and height must be multiple of 2");
        return null;
    }// w  ww .  ja va  2  s  . com

    Symbol symbol = new Symbol();
    PixelState[][] pixelStates = new PixelState[finalHeight][finalWidth];
    Raster imageData = image.getData();

    for (int y = 0; y < finalHeight; y++) {
        for (int x = 0; x < finalWidth; x++) {
            int sample = imageData.getSample(x * 2, y * 2, 0);
            PixelState pixelState = sample == 0 ? PixelState.ON : PixelState.OFF;
            pixelStates[y][x] = pixelState;
        }
    }

    symbol.setData(pixelStates);
    return symbol;
}

From source file:org.lightadmin.core.web.util.ImageResourceControllerSupport.java

protected BufferedImage resizeImage(BufferedImage sourceImage, int width, int height) {
    final int currentWidth = sourceImage.getWidth();
    final int currentHeight = sourceImage.getHeight();

    float ratio = ((float) currentHeight / (float) currentWidth);

    if (width <= 0) {
        width = (int) (height / ratio);
    }/*  w ww. jav  a2 s  .  co  m*/

    if (height <= 0) {
        height = (int) (width * ratio);
    }

    return resize(sourceImage, SPEED, AUTOMATIC, width, height, OP_ANTIALIAS);
}

From source file:de.anycook.upload.UploadHandler.java

/**
 * speichert eine grosse Version des Bildes
 *
 * @param image    BufferedImage/*from  www .  j  av a  2 s  .  c  o  m*/
 * @param filename Name der zu erzeugenden Datei
 */
private void saveBigImage(BufferedImage image, String filename) throws IOException {
    int height = image.getHeight();
    int width = image.getWidth();

    int newWidth = bigWidth;
    int newHeight = new Double(((double) height / (double) width) * newWidth).intValue();

    if (bigHeight > -1 && newHeight > bigHeight) {
        newHeight = bigHeight;
        newWidth = new Double(((double) width / (double) height) * newHeight).intValue();
    }

    BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
    newImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0,
            null);

    imageSaver.save("big/", filename, newImage);

}

From source file:com.epam.ta.reportportal.core.user.impl.EditUserHandler.java

private void validatePhoto(MultipartFile file) throws IOException {
    expect(file.getSize() < MAX_PHOTO_SIZE, equalTo(true)).verify(BINARY_DATA_CANNOT_BE_SAVED,
            "Image size should be less than 1 mb");
    MediaType mediaType = new AutoDetectParser().getDetector().detect(TikaInputStream.get(file.getBytes()),
            new Metadata());
    String subtype = mediaType.getSubtype();
    expect(ImageFormat.fromValue(subtype), notNull()).verify(BINARY_DATA_CANNOT_BE_SAVED,
            "Image format should be " + ImageFormat.getValues());
    BufferedImage read = ImageIO.read(file.getInputStream());
    expect((read.getHeight() <= MAX_PHOTO_HEIGHT) && (read.getWidth() <= MAX_PHOTO_WIDTH), equalTo(true))
            .verify(BINARY_DATA_CANNOT_BE_SAVED, "Image size should be 300x500px or less");
}

From source file:de.anycook.upload.UploadHandler.java

/**
 * speichert eine kleine Version des Bildes
 *
 * @param image    BufferedImage// w ww  . ja v a2  s  . c  o  m
 * @param filename Name der zu erzeugenden Datei
 */
private void saveSmallImage(BufferedImage image, String filename) throws IOException {
    int height = image.getHeight();
    int width = image.getWidth();
    double imageRatio = (double) width / (double) height;

    int xtranslate = 0;
    int ytranslate = 0;

    if (imageRatio > 1) {
        xtranslate = (width - height) / 2;
    } else {
        ytranslate = (height - width) / 2;
    }

    BufferedImage tempImage = image.getSubimage(xtranslate, ytranslate, width - xtranslate * 2,
            height - ytranslate * 2);
    BufferedImage newImage = new BufferedImage(smallSize, smallSize, BufferedImage.TYPE_INT_RGB);
    newImage.getGraphics().drawImage(tempImage.getScaledInstance(smallSize, smallSize, Image.SCALE_SMOOTH), 0,
            0, null);

    imageSaver.save("small/", filename, newImage);
}

From source file:RotateImage45Degrees.java

private AffineTransform findTranslation(AffineTransform at, BufferedImage bi) {
    Point2D p2din, p2dout;/*  www . ja  v  a 2  s .c  o  m*/

    p2din = new Point2D.Double(0.0, 0.0);
    p2dout = at.transform(p2din, null);
    double ytrans = p2dout.getY();

    p2din = new Point2D.Double(0, bi.getHeight());
    p2dout = at.transform(p2din, null);
    double xtrans = p2dout.getX();

    AffineTransform tat = new AffineTransform();
    tat.translate(-xtrans, -ytrans);
    return tat;
}