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.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Flip the image horizontal/*  w ww. j a  v  a  2s .  com*/
 *
 * @param bufferedImage original image
 * @return horizontally flipped image
 */
private static BufferedImage flipHorizontal(BufferedImage bufferedImage) {
    AffineTransform at = AffineTransform.getTranslateInstance(bufferedImage.getWidth(), 0);
    at.scale(-1.0, 1.0);
    BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
            bufferedImage.getType());
    return biOp.filter(bufferedImage, imgRes);
}

From source file:ImageUtils.java

/**
 * Creates a <code>BufferedImage</code> from an <code>Image</code>. This method can
 * function on a completely headless system. This especially includes Linux and Unix systems
 * that do not have the X11 libraries installed, which are required for the AWT subsystem to
 * operate. The resulting image will be smoothly scaled using bilinear filtering.
 * //from  w  w w .  j a v a 2  s  .c o  m
 * @param source The image to convert
 * @param w The desired image width
 * @param h The desired image height
 * @return The converted image
 * @param type  int
 */
public static BufferedImage createHeadlessSmoothBufferedImage(BufferedImage source, int type, int width,
        int height) {
    if (type == ImageUtils.IMAGE_PNG && hasAlpha(source)) {
        type = BufferedImage.TYPE_INT_ARGB;
    } else {
        type = BufferedImage.TYPE_INT_RGB;
    }

    BufferedImage dest = new BufferedImage(width, height, type);

    int sourcex;
    int sourcey;

    double scalex = (double) width / source.getWidth();
    double scaley = (double) height / source.getHeight();

    int x1;
    int y1;

    double xdiff;
    double ydiff;

    int rgb;
    int rgb1;
    int rgb2;

    for (int y = 0; y < height; y++) {
        sourcey = y * source.getHeight() / dest.getHeight();
        ydiff = scale(y, scaley) - sourcey;

        for (int x = 0; x < width; x++) {
            sourcex = x * source.getWidth() / dest.getWidth();
            xdiff = scale(x, scalex) - sourcex;

            x1 = Math.min(source.getWidth() - 1, sourcex + 1);
            y1 = Math.min(source.getHeight() - 1, sourcey + 1);

            rgb1 = getRGBInterpolation(source.getRGB(sourcex, sourcey), source.getRGB(x1, sourcey), xdiff);
            rgb2 = getRGBInterpolation(source.getRGB(sourcex, y1), source.getRGB(x1, y1), xdiff);

            rgb = getRGBInterpolation(rgb1, rgb2, ydiff);

            dest.setRGB(x, y, rgb);
        }
    }

    return dest;
}

From source file:com.silverpeas.gallery.MediaHelper.java

/**
 * Sets the resolution of a photo./*from  w  ww. j a v a2s  .com*/
 * @param image
 * @param photo
 */
private static void setResolution(final BufferedImage image, final Photo photo) {
    if (image == null) {
        photo.setDefinition(Definition.fromZero());
    } else {
        photo.setDefinition(Definition.of(image.getWidth(), image.getHeight()));
    }
}

From source file:com.simiacryptus.util.Util.java

/**
 * Resize buffered image./*from www  .  j a v  a  2s  . com*/
 *
 * @param image the image
 * @return the buffered image
 */
@Nullable
public static BufferedImage resize(@Nullable final BufferedImage image) {
    if (null == image)
        return image;
    final int width = Math.min(image.getWidth(), 800);
    if (width == image.getWidth())
        return image;
    final int height = image.getHeight() * width / image.getWidth();
    @javax.annotation.Nonnull
    final BufferedImage rerender = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = rerender.getGraphics();
    @javax.annotation.Nonnull
    final RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    ((Graphics2D) gfx).setRenderingHints(hints);
    gfx.drawImage(image, 0, 0, rerender.getWidth(), rerender.getHeight(), null);
    return rerender;
}

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

private static BufferedImage rescaleBorder(BufferedImage image, int targetWidth, int targetHeight)
        throws IOException {
    if (targetWidth == 0) {
        targetWidth = 1;/*from   w  ww.  ja  va  2 s.  c o  m*/
    }
    if (targetHeight == 0) {
        targetHeight = 1;
    }

    if (targetHeight > 1 && targetWidth > 1) {
        throw new IOException();
    }

    int w = image.getWidth();
    int h = image.getHeight();
    int[] data = image.getRGB(0, 0, w, h, null, 0, w);
    int[] newData = new int[targetWidth * targetHeight];

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            newData[y * targetWidth + x] = 0x00;
        }
    }

    List<Integer> startPositions = new ArrayList<Integer>();
    List<Integer> endPositions = new ArrayList<Integer>();

    boolean inBlock = false;
    if (targetHeight == 1) {
        for (int x = 0; x < w; x++) {
            if ((0xff000000 & data[x]) != 0) {
                if (!inBlock) {
                    inBlock = true;
                    startPositions.add(x);
                }
            } else if (inBlock) {
                endPositions.add(x - 1);
                inBlock = false;
            }
        }
        if (inBlock) {
            endPositions.add(w - 1);
        }
    } else {
        for (int y = 0; y < h; y++) {
            if ((0xff000000 & data[y]) != 0) {
                if (!inBlock) {
                    inBlock = true;
                    startPositions.add(y);
                }
            } else if (inBlock) {
                endPositions.add(y - 1);
                inBlock = false;
            }
        }
        if (inBlock) {
            endPositions.add(h - 1);
        }
    }
    try {
        SplineInterpolator interpolator = new SplineInterpolator();
        PolynomialSplineFunction function = interpolator.interpolate(
                new double[] { 0f, 1f, Math.max(w - 1, h - 1) },
                new double[] { 0f, 1f, Math.max(targetHeight - 1, targetWidth - 1) });
        for (int i = 0; i < startPositions.size(); i++) {
            int start = startPositions.get(i);
            int end = endPositions.get(i);
            for (int j = (int) function.value(start); j <= (int) function.value(end); j++) {
                newData[j] = 0xff000000;
            }
        }

        BufferedImage img = UIUtil.createImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        img.setRGB(0, 0, targetWidth, targetHeight, newData, 0, targetWidth);
        return img;
    } catch (Exception e) {
        Logger.getInstance(ImageUtils.class).error("resizeBorder", e);
    }

    return null;
}

From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java

public static JFrame showImage(BufferedImage image, String title, boolean fitToScreen) {
    JFrame frame = new JFrame(title != null ? title : "");
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    Image im = image;//from   ww  w. jav a 2 s. co  m

    if (fitToScreen) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int imageHeight = image.getHeight();
        int imageWidth = image.getWidth();
        if (imageHeight > screen.height || imageWidth > screen.width) {
            double hRatio = (imageHeight - screen.height) / screen.height;
            double wRatio = (imageWidth - screen.width) / screen.width;

            int w = -1;
            int h = -1;

            if (hRatio > wRatio)
                h = screen.height;
            else
                w = screen.width;
            im = image.getScaledInstance(w, h, Image.SCALE_DEFAULT);
        }
    }

    JLabel label = new JLabel(new ImageIcon(im));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    centerWindow(frame);
    frame.setVisible(true);
    return frame;
}

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

private static BufferedImage rescaleBorder(BufferedImage image, int targetWidth, int targetHeight) {
    if (targetWidth == 0) {
        targetWidth = 1;//from  ww w .  j  a va2  s  .c  o m
    }
    if (targetHeight == 0) {
        targetHeight = 1;
    }

    if (targetHeight > 1 && targetWidth > 1) {
        throw new Wrong9PatchException();
    }

    int w = image.getWidth();
    int h = image.getHeight();
    int[] data = image.getRGB(0, 0, w, h, null, 0, w);
    int[] newData = new int[targetWidth * targetHeight];

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            newData[y * targetWidth + x] = 0x00;
        }
    }

    List<Integer> startPositions = new ArrayList<Integer>();
    List<Integer> endPositions = new ArrayList<Integer>();

    boolean inBlock = false;
    if (targetHeight == 1) {
        for (int x = 0; x < w; x++) {
            if ((0xff000000 & data[x]) != 0) {
                if (!inBlock) {
                    inBlock = true;
                    startPositions.add(x);
                }
            } else if (inBlock) {
                endPositions.add(x - 1);
                inBlock = false;
            }
        }
        if (inBlock) {
            endPositions.add(w - 1);
        }
    } else {
        for (int y = 0; y < h; y++) {
            if ((0xff000000 & data[y]) != 0) {
                if (!inBlock) {
                    inBlock = true;
                    startPositions.add(y);
                }
            } else if (inBlock) {
                endPositions.add(y - 1);
                inBlock = false;
            }
        }
        if (inBlock) {
            endPositions.add(h - 1);
        }
    }
    try {
        SplineInterpolator interpolator = new SplineInterpolator();
        PolynomialSplineFunction function = interpolator.interpolate(
                new double[] { 0f, 1f, Math.max(w - 1, h - 1) },
                new double[] { 0f, 1f, Math.max(targetHeight - 1, targetWidth - 1) });
        for (int i = 0; i < startPositions.size(); i++) {
            int start = startPositions.get(i);
            int end = endPositions.get(i);
            for (int j = (int) function.value(start); j <= (int) function.value(end); j++) {
                newData[j] = 0xff000000;
            }
        }

        BufferedImage img = UIUtil.createImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        img.setRGB(0, 0, targetWidth, targetHeight, newData, 0, targetWidth);
        return img;
    } catch (Exception e) {
        Logger.getInstance(ImageUtils.class).error("resizeBorder", e);
    }

    return null;
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Rescales using predefined min,max intensities and converts to 8bit. Works for gray- and rgb color images.
 *
 * @param bi16/*from   w w  w .j a v  a 2s  .  c o m*/
 * @return
 */
public static BufferedImage convertTo8bit(BufferedImage bi16, int min, int max) {
    BufferedImage bi = null;
    if (bi16.getSampleModel().getNumBands() == 1) {
        bi16 = ImageUtils.scaleIntensities(bi16, new int[] { min }, new int[] { max });
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    } else if (bi16.getSampleModel().getNumBands() == 3) {
        throw new IllegalArgumentException("only grayscale images supported");
    } else {
        throw new IllegalArgumentException(
                "Only images with 1 band (gray-color) or 3 bands (rgb) supported. This image has "
                        + bi16.getSampleModel().getNumBands() + " bands.");
    }
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(bi16, 0, 0, null);
    g2d.dispose();
    return bi;
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Calculates and returns band histograms of a BufferedImage
 *
 * @param image//w  w w. j  a va  2s. c om
 * @return
 */
public static int[][] getChannelHistograms(BufferedImage image) {
    WritableRaster raster = image.getRaster();
    int[][] histograms = new int[raster
            .getNumBands()][(int) (Math.pow(2, image.getColorModel().getPixelSize()) - 1)];

    int width = image.getWidth();
    int height = image.getHeight();

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                histograms[band][raster.getSample(col, row, band)]++;
            }
        }
    }

    return histograms;
}

From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

private static List<RectangularRegion> findSubImageInImage(BufferedImage subImage, BufferedImage image,
        int max) {
    Map<Integer, List<Point>> rgb2offsets = new HashMap<Integer, List<Point>>();
    int sw = subImage.getWidth();
    int sh = subImage.getHeight();
    for (int x = 0; x < sw; ++x) {
        for (int y = 0; y < sh; ++y) {
            int argb = subImage.getRGB(x, y);
            int a = argb >>> 24;
            if (a == 255) {
                Integer rgb = argb & 0xFFFFFF;
                List<Point> offsets = rgb2offsets.get(rgb);
                if (offsets == null) {
                    offsets = new ArrayList<Point>();
                    rgb2offsets.put(rgb, offsets);
                }/*from  ww w.ja va 2s.c  om*/
                offsets.add(new Point(x, y));
            }
        }
    }
    List<RectangularRegion> result = new ArrayList<RectangularRegion>();
    int w = image.getWidth();
    int h = image.getHeight();
    int[][] p = new int[w][h];
    Raster raster = image.getRaster();
    if (raster.getTransferType() == DataBuffer.TYPE_BYTE) {
        byte[] bytes = (byte[]) raster.getDataElements(0, 0, w, h, null);
        int bytesPerPixel = (bytes.length / (w * h));
        ColorModel colorModel = image.getColorModel();
        byte[] buf = new byte[bytesPerPixel];
        for (int x = 0; x < w; ++x) {
            for (int y = 0; y < h; ++y) {
                System.arraycopy(bytes, (x + y * w) * bytesPerPixel, buf, 0, bytesPerPixel);
                p[x][y] = colorModel.getRGB(buf) & 0xFFFFFF;
            }
        }
    } else if (raster.getTransferType() == DataBuffer.TYPE_INT) {
        for (int x = 0; x < w; ++x) {
            p[x] = (int[]) raster.getDataElements(x, 0, 1, h, null);
        }
    } else {
        throw new RuntimeException("findSubImageInImage not implemented for image transfer type "
                + raster.getTransferType() + " yet.");
    }
    for (int x = 0; x < w; ++x) {
        for (int y = 0; y < h; ++y) {
            Iterator<Map.Entry<Integer, List<Point>>> i = rgb2offsets.entrySet().iterator();
            compareWithSubImageLoop: while (i.hasNext()) {
                Map.Entry<Integer, List<Point>> mapEntry = i.next();
                int expectedRgb = mapEntry.getKey();
                for (Point offset : mapEntry.getValue()) {
                    int xx = x + offset.x;
                    int yy = y + offset.y;
                    if (xx >= w || yy >= h || expectedRgb != p[xx][yy]) {
                        break compareWithSubImageLoop;
                    }
                }
                if (!i.hasNext()) {
                    result.add(new RectangularRegion(x, y, x + (sw - 1), y + (sh - 1)));
                    if (result.size() == max) {
                        return result;
                    }
                }
            }
        }
    }
    return result;
}