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:ImageOpByRomain.java

/**
 * <p>//  ww  w  .j a  va2  s. c  o m
 * Returns a thumbnail of a source image.
 * </p>
 * <p>
 * This method offers a good trade-off between speed and quality. The result
 * looks better than
 * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when the
 * new size is less than half the longest dimension of the source image, yet
 * the rendering speed is almost similar.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int)
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @param image
 *            the source image
 * @param newWidth
 *            the width of the thumbnail
 * @param newHeight
 *            the height of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newWidth</code> is larger than the width of
 *             <code>image</code> or if code>newHeight</code> is larger
 *             than the height of <code>image or if one the dimensions is not
 *             &gt; 0</code>
 */
public static BufferedImage createThumbnail(BufferedImage image, int newWidth, int newHeight) {
    int width = image.getWidth();
    int height = image.getHeight();

    if (newWidth >= width || newHeight >= height) {
        throw new IllegalArgumentException(
                "newWidth and newHeight cannot" + " be greater than the image" + " dimensions");
    } else if (newWidth <= 0 || newHeight <= 0) {
        throw new IllegalArgumentException("newWidth and newHeight must" + " be greater than 0");
    }

    BufferedImage thumb = image;

    do {
        if (width > newWidth) {
            width /= 2;
            if (width < newWidth) {
                width = newWidth;
            }
        }

        if (height > newHeight) {
            height /= 2;
            if (height < newHeight) {
                height = newHeight;
            }
        }

        BufferedImage temp = createCompatibleImage(image, width, height);
        Graphics2D g2 = temp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
        g2.dispose();

        thumb = temp;
    } while (width != newWidth || height != newHeight);

    return thumb;
}

From source file:ImageOpByRomain.java

/**
 * <p>/*from w w w .jav a2  s. c  om*/
 * Returns a thumbnail of a source image. <code>newSize</code> defines the
 * length of the longest dimension of the thumbnail. The other dimension is
 * then computed according to the dimensions ratio of the original picture.
 * </p>
 * <p>
 * This method favors speed over quality. When the new size is less than half
 * the longest dimension of the source image,
 * {@link #createThumbnail(BufferedImage, int)} or
 * {@link #createThumbnail(BufferedImage, int, int)} should be used instead to
 * ensure the quality of the result without sacrificing too much performance.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int, int)
 * @param image
 *            the source image
 * @param newSize
 *            the length of the largest dimension of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newSize</code> is larger than the largest dimension
 *             of <code>image</code> or &lt;= 0
 */
public static BufferedImage createThumbnailFast(BufferedImage image, int newSize) {
    float ratio;
    int width = image.getWidth();
    int height = image.getHeight();

    if (width > height) {
        if (newSize >= width) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image width");
        } else if (newSize <= 0) {
            throw new IllegalArgumentException("newSize must" + " be greater than 0");
        }

        ratio = (float) width / (float) height;
        width = newSize;
        height = (int) (newSize / ratio);
    } else {
        if (newSize >= height) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image height");
        } else if (newSize <= 0) {
            throw new IllegalArgumentException("newSize must" + " be greater than 0");
        }

        ratio = (float) height / (float) width;
        height = newSize;
        width = (int) (newSize / ratio);
    }

    BufferedImage temp = createCompatibleImage(image, width, height);
    Graphics2D g2 = temp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
    g2.dispose();

    return temp;
}

From source file:org.smigo.constraints.CommonsMultipartFileImageValidator.java

public boolean isValid(CommonsMultipartFile file, ConstraintValidatorContext constraintContext) {
    log.debug("Validating:" + file.getOriginalFilename() + " contenttype:" + file.getContentType()
            + " storagedescrip:" + file.getStorageDescription());
    if (file.isEmpty())
        return true;
    try {/*w w  w.j av a 2  s . com*/
        BufferedImage image = ImageIO.read(file.getInputStream());
        if (image.getHeight() == height && image.getWidth() == width)
            return true;
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:TextureByReference.java

public static void flipImage(BufferedImage bImage) {
    int width = bImage.getWidth();
    int height = bImage.getHeight();
    int[] rgbArray = new int[width * height];
    bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
    int[] tempArray = new int[width * height];
    int y2 = 0;// www  .j a  v  a 2 s .c om
    for (int y = height - 1; y >= 0; y--) {
        for (int x = 0; x < width; x++) {
            tempArray[y2 * width + x] = rgbArray[y * width + x];
        }
        y2++;
    }
    bImage.setRGB(0, 0, width, height, tempArray, 0, width);
}

From source file:TextureByReference.java

public static BufferedImage convertImage(BufferedImage bImage, int type) {
    int width = bImage.getWidth();
    int height = bImage.getHeight();
    BufferedImage newImage = new BufferedImage(width, height, type);
    int[] rgbArray = new int[width * height];
    bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
    newImage.setRGB(0, 0, width, height, rgbArray, 0, width);
    return newImage;
}

From source file:MainClass.java

public BufferedImage emboss(BufferedImage src) {
    int width = src.getWidth();
    int height = src.getHeight();

    BufferedImage dst;//from w ww.j a  v a  2 s.c o  m
    dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) {
            int upperLeft = 0;
            int lowerRight = 0;

            if (i > 0 && j > 0)
                upperLeft = src.getRGB(j - 1, i - 1);

            if (i < height - 1 && j < width - 1)
                lowerRight = src.getRGB(j + 1, i + 1);

            int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255);

            int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255);

            int blueDiff = (lowerRight & 255) - (upperLeft & 255);

            int diff = redDiff;
            if (Math.abs(greenDiff) > Math.abs(diff))
                diff = greenDiff;
            if (Math.abs(blueDiff) > Math.abs(diff))
                diff = blueDiff;

            int grayColor = 128 + diff;

            if (grayColor > 255)
                grayColor = 255;
            else if (grayColor < 0)
                grayColor = 0;

            int newColor = (grayColor << 16) + (grayColor << 8) + grayColor;

            dst.setRGB(j, i, newColor);
        }

    return dst;
}

From source file:Main.java

ScreenCaptureRectangle(final BufferedImage screen) {
    BufferedImage screenCopy = new BufferedImage(screen.getWidth(), screen.getHeight(), screen.getType());
    JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
    JScrollPane screenScroll = new JScrollPane(screenLabel);

    screenScroll.setPreferredSize(new Dimension(300, 300));

    repaint(screen, screenCopy);/*from   w ww. j a v a  2 s  .c  o  m*/
    screenLabel.repaint();

    screenLabel.addMouseMotionListener(new MouseMotionAdapter() {
        Point start = new Point();

        @Override
        public void mouseMoved(MouseEvent me) {
            start = me.getPoint();
            repaint(screen, screenCopy);
            screenLabel.repaint();
        }

        @Override
        public void mouseDragged(MouseEvent me) {
            Point end = me.getPoint();
            captureRect = new Rectangle(start, new Dimension(end.x - start.x, end.y - start.y));
            repaint(screen, screenCopy);
            screenLabel.repaint();
        }
    });
    JOptionPane.showMessageDialog(null, screenScroll);
}

From source file:ImageOpByRomain.java

/**
 * <p>/*from  w  ww .  j a  v a 2s.  c o  m*/
 * Returns a new <code>BufferedImage</code> using the same color model as
 * the image passed as a parameter. The returned image is only compatible with
 * the image passed as a parameter. This does not mean the returned image is
 * compatible with the hardware.
 * </p>
 * 
 * @param image
 *            the reference image from which the color model of the new image
 *            is obtained
 * @return a new <code>BufferedImage</code>, compatible with the color
 *         model of <code>image</code>
 */
public static BufferedImage createColorModelCompatibleImage(BufferedImage image) {
    ColorModel cm = image.getColorModel();
    return new BufferedImage(cm, cm.createCompatibleWritableRaster(image.getWidth(), image.getHeight()),
            cm.isAlphaPremultiplied(), null);
}

From source file:gr.iti.mklab.reveal.forensics.util.Util.java

public static int[][][] getRGBArray(BufferedImage imageIn) {
    // possible 10-fold speed increase in:
    // http://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image
    // (but ensure all major bases are covered)
    int ImW = imageIn.getWidth();
    int ImH = imageIn.getHeight();
    Color tmpColor;/*ww  w.  jav a 2 s.c  o m*/
    int[][][] rgbValues = new int[3][ImW][ImH];

    for (int ii = 0; ii < ImW; ii++) {
        for (int jj = 0; jj < ImH; jj++) {
            tmpColor = new Color(imageIn.getRGB(ii, jj));
            rgbValues[0][ii][jj] = tmpColor.getRed();
            rgbValues[1][ii][jj] = tmpColor.getGreen();
            rgbValues[2][ii][jj] = tmpColor.getBlue();
        }
    }
    return rgbValues;
}

From source file:edu.umn.cs.spatialHadoop.operations.HeatMapPlot.java

private static <S extends Shape> void plotHeatMapLocal(Path inFile, Path outFile, OperationsParams params)
        throws IOException {
    int imageWidth = params.getInt("width", 1000);
    int imageHeight = params.getInt("height", 1000);

    Shape shape = params.getShape("shape", new Point());
    Shape plotRange = params.getShape("rect", null);

    boolean keepAspectRatio = params.is("keep-ratio", true);

    InputSplit[] splits;//from  w  ww . j  a  va 2 s.c  o  m
    FileSystem inFs = inFile.getFileSystem(params);
    FileStatus inFStatus = inFs.getFileStatus(inFile);
    if (inFStatus != null && !inFStatus.isDir()) {
        // One file, retrieve it immediately.
        // This is useful if the input is a hidden file which is automatically
        // skipped by FileInputFormat. We need to plot a hidden file for the case
        // of plotting partition boundaries of a spatial index
        splits = new InputSplit[] { new FileSplit(inFile, 0, inFStatus.getLen(), new String[0]) };
    } else {
        JobConf job = new JobConf(params);
        ShapeInputFormat<Shape> inputFormat = new ShapeInputFormat<Shape>();
        ShapeInputFormat.addInputPath(job, inFile);
        splits = inputFormat.getSplits(job, 1);
    }

    boolean vflip = params.is("vflip");

    Rectangle fileMBR;
    if (plotRange != null) {
        fileMBR = plotRange.getMBR();
    } else {
        fileMBR = FileMBR.fileMBR(inFile, params);
    }

    if (keepAspectRatio) {
        // Adjust width and height to maintain aspect ratio
        if (fileMBR.getWidth() / fileMBR.getHeight() > (double) imageWidth / imageHeight) {
            // Fix width and change height
            imageHeight = (int) (fileMBR.getHeight() * imageWidth / fileMBR.getWidth());
        } else {
            imageWidth = (int) (fileMBR.getWidth() * imageHeight / fileMBR.getHeight());
        }
    }

    // Create the frequency map
    int radius = params.getInt("radius", 5);
    FrequencyMap frequencyMap = new FrequencyMap(imageWidth, imageHeight);

    for (InputSplit split : splits) {
        ShapeRecordReader<Shape> reader = new ShapeRecordReader<Shape>(params, (FileSplit) split);
        Rectangle cell = reader.createKey();
        while (reader.next(cell, shape)) {
            Rectangle shapeBuffer = shape.getMBR();
            if (shapeBuffer == null)
                continue;
            shapeBuffer = shapeBuffer.buffer(radius, radius);
            if (plotRange == null || shapeBuffer.isIntersected(plotRange)) {
                Point centerPoint = shapeBuffer.getCenterPoint();
                int cx = (int) Math.round((centerPoint.x - fileMBR.x1) * imageWidth / fileMBR.getWidth());
                int cy = (int) Math.round((centerPoint.y - fileMBR.y1) * imageHeight / fileMBR.getHeight());
                frequencyMap.addPoint(cx, cy, radius);
            }
        }
        reader.close();
    }

    // Convert frequency map to an image with colors
    NASAPoint.setColor1(params.getColor("color1", Color.BLUE));
    NASAPoint.setColor2(params.getColor("color2", Color.RED));
    NASAPoint.gradientType = params.getGradientType("gradient", NASAPoint.GradientType.GT_HUE);
    String valueRangeStr = params.get("valuerange");
    MinMax valueRange = null;
    if (valueRangeStr != null) {
        String[] parts = valueRangeStr.contains("..") ? valueRangeStr.split("\\.\\.", 2)
                : valueRangeStr.split(",", 2);
        valueRange = new MinMax(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
    }

    boolean skipZeros = params.getBoolean("skipzeros", false);
    BufferedImage image = frequencyMap.toImage(valueRange, skipZeros);

    if (vflip) {
        AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
        tx.translate(0, -image.getHeight());
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        image = op.filter(image, null);
    }
    FileSystem outFs = outFile.getFileSystem(params);
    OutputStream out = outFs.create(outFile, true);
    ImageIO.write(image, "png", out);
    out.close();

}