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.aimluck.eip.fileupload.util.FileuploadUtils.java

/**
 * ???/*from  w ww  .ja  v a 2 s .c om*/
 * 
 * @param imgfile
 * @param width
 * @param height
 * @return
 */
public static BufferedImage shrinkAndTrimImage(BufferedImage imgfile, int width, int height) {
    int iwidth = imgfile.getWidth();
    int iheight = imgfile.getHeight();
    double ratio = Math.max((double) width / (double) iwidth, (double) height / (double) iheight);

    int shrinkedWidth;
    int shrinkedHeight;

    if ((iwidth <= width) || (iheight < height)) {
        shrinkedWidth = iwidth;
        shrinkedHeight = iheight;
    } else {
        shrinkedWidth = (int) (iwidth * ratio);
        shrinkedHeight = (int) (iheight * ratio);
    }

    // ??
    Image targetImage = imgfile.getScaledInstance(shrinkedWidth, shrinkedHeight, Image.SCALE_AREA_AVERAGING);

    int w_size = targetImage.getWidth(null);
    int h_size = targetImage.getHeight(null);
    if (targetImage.getWidth(null) < width) {
        w_size = width;
    }
    if (targetImage.getHeight(null) < height) {
        h_size = height;
    }
    BufferedImage tmpImage = new BufferedImage(w_size, h_size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = tmpImage.createGraphics();
    g.setBackground(Color.WHITE);
    g.setColor(Color.WHITE);
    // ??????????????
    g.fillRect(0, 0, w_size, h_size);
    int diff_w = 0;
    int diff_h = 0;
    if (width > shrinkedWidth) {
        diff_w = (width - shrinkedWidth) / 2;
    }
    if (height > shrinkedHeight) {
        diff_h = (height - shrinkedHeight) / 2;
    }
    g.drawImage(targetImage, diff_w, diff_h, null);

    int _iwidth = tmpImage.getWidth();
    int _iheight = tmpImage.getHeight();
    BufferedImage _tmpImage;
    if (_iwidth > _iheight) {
        int diff = _iwidth - width;
        _tmpImage = tmpImage.getSubimage(diff / 2, 0, width, height);
    } else {
        int diff = _iheight - height;
        _tmpImage = tmpImage.getSubimage(0, diff / 2, width, height);
    }
    return _tmpImage;
}

From source file:com.simiacryptus.mindseye.applications.ArtistryUtil.java

/**
 * Paint low res./*w  w  w.ja  v  a2s . co  m*/
 *
 * @param canvas the canvas
 * @param scale  the scale
 */
public static void paint_LowRes(final Tensor canvas, final int scale) {
    BufferedImage originalImage = canvas.toImage();
    canvas.set(Tensor
            .fromRGB(TestUtil.resize(TestUtil.resize(originalImage, originalImage.getWidth() / scale, true),
                    originalImage.getWidth(), originalImage.getHeight())));
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
    BufferedImage returnValue = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2D = returnValue.createGraphics();
    g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    AffineTransform at = AffineTransform.getScaleInstance((double) width / image.getWidth(),
            (double) height / image.getHeight());
    g2D.drawRenderedImage(image, at);//from ww w.j  a va 2  s. c  o  m
    return returnValue;
}

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

/**
 * Extract magical image properties used by the getBlock function.
 * //from  w ww . ja v a2 s. c  o m
 * @param image
 *            a BufferedImage
 * @return an ImageProperties descriptor object
 */
public static final ImageProperties getImageProperties(final BufferedImage image) {
    final int imageType = image.getType();
    ImageProperties p = new ImageProperties();
    p.image = image;
    p.raster = image.getRaster();
    p.alpha = imageType == TYPE_INT_ARGB || imageType == BufferedImage.TYPE_4BYTE_ABGR;
    boolean rgb = imageType == TYPE_INT_ARGB || imageType == TYPE_INT_RGB;
    boolean bgr = imageType == BufferedImage.TYPE_INT_BGR || imageType == BufferedImage.TYPE_3BYTE_BGR
            || imageType == BufferedImage.TYPE_4BYTE_ABGR;
    p.width = image.getWidth();
    p.height = image.getHeight();
    p.fallback = !(rgb || bgr);
    return p;
}

From source file:com.google.zxing.web.DecodeServlet.java

private static void processStream(InputStream is, ServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    BufferedImage image;
    try {/*from w ww .  j ava 2  s .c om*/
        image = ImageIO.read(is);
    } catch (IOException | CMMException | IllegalArgumentException ioe) {
        log.info(ioe.toString());
        // Have seen these in some logs
        response.sendRedirect("badimage.jspx");
        return;
    }
    if (image == null) {
        response.sendRedirect("badimage.jspx");
        return;
    }
    if (image.getHeight() <= 1 || image.getWidth() <= 1 || image.getHeight() * image.getWidth() > MAX_PIXELS) {
        log.info("Dimensions out of bounds: " + image.getWidth() + 'x' + image.getHeight());
        response.sendRedirect("badimage.jspx");
        return;
    }

    processImage(image, request, response);
}

From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java

public static JSONObject createThumbnail(File projectDir, File pyramidDir, String imageName,
        BufferedImage refImage) throws IOException, JSONException {
    final Path projectScope = projectDir.toPath();
    final BufferedImage tmbImage = toThumbnailSize(refImage);

    final File thumbnailDir = new File(pyramidDir.getParentFile(), ".thumbnail");
    thumbnailDir.mkdir();//from w ww .  ja  va 2 s. co m
    final File refFile = File.createTempFile(imageName + ".", ".jpg", thumbnailDir);
    final File tmbFile = tmbImage != refImage ? File.createTempFile(imageName + ".tmb.", ".jpg", thumbnailDir)
            : refFile;

    final JSONObject thumbnail = new JSONObject();
    thumbnail.put("scope", "Project");
    thumbnail.put("width", tmbImage.getWidth());
    thumbnail.put("height", tmbImage.getHeight());
    thumbnail.put("src", toRef(projectScope, tmbFile.toPath()));
    ImageIO.write(refImage, "JPEG", refFile);
    if (tmbFile != refFile) {
        thumbnail.put("ref", toRef(projectScope, refFile.toPath()));
        ImageIO.write(tmbImage, "JPEG", tmbFile);
    }
    return thumbnail;
}

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

/**
 * Rotate an image using the requested angle
 *
 * @param bufferedImage imeg to rotate/*from  w w w .j  a v  a 2  s .c om*/
 * @param angle         angle to rotate
 * @return BufferedImage containing the rotation
 */
@SuppressWarnings({ "UnusedAssignment" })
private static BufferedImage rotate(BufferedImage bufferedImage, int angle) {
    angle = angle % 360;
    if (angle == 0)
        return bufferedImage;
    if (angle < 0)
        angle += 360;
    switch (angle) {
    case 90:
        BufferedImageOp rot90 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI / 2.0,
                bufferedImage.getHeight() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img90 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot90.filter(bufferedImage, img90);
    case 180:
        BufferedImageOp rot180 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,
                bufferedImage.getWidth() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img180 = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
                bufferedImage.getType());
        return rot180.filter(bufferedImage, img180);
    case 270:
        BufferedImageOp rot270 = new AffineTransformOp(AffineTransform.getRotateInstance(-Math.PI / 2.0,
                bufferedImage.getWidth() / 2.0, bufferedImage.getWidth() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img270 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot270.filter(bufferedImage, img270);
    default:
        //rotate using a non-standard angle (have to draw a box around the image that can fit it)
        int box = (int) Math.sqrt(bufferedImage.getHeight() * bufferedImage.getHeight()
                + bufferedImage.getWidth() * bufferedImage.getWidth());
        BufferedImage imgFree = new BufferedImage(box, box, bufferedImage.getType());
        BufferedImage imgRet = new BufferedImage(box, box, bufferedImage.getType());
        Graphics2D g = imgFree.createGraphics();
        if (bufferedImage.getTransparency() == Transparency.OPAQUE) {
            //draw a white background on opaque images since they dont support transparency
            g.setBackground(Color.WHITE);
            g.clearRect(0, 0, box, box);
            Graphics2D gr = imgRet.createGraphics();
            gr.setBackground(Color.WHITE);
            gr.clearRect(0, 0, box, box);
            gr = null;
        }
        g.drawImage(bufferedImage, box / 2 - bufferedImage.getWidth() / 2,
                box / 2 - bufferedImage.getHeight() / 2, bufferedImage.getWidth(), bufferedImage.getHeight(),
                null);
        g = null;
        AffineTransform at = new AffineTransform();
        at.rotate(angle * Math.PI / 180.0, box / 2.0, box / 2.0);
        BufferedImageOp bio;
        bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        return bio.filter(imgFree, imgRet);
    }
}

From source file:FileUtils.java

/**
 * Convenience method that returns a scaled instance of the
 * provided {@code BufferedImage}./*from   w  ww.java  2  s.  c  o  m*/
 *
 * @param img           the original image to be scaled
 * @param targetWidth   the desired width of the scaled instance,
 *                      in pixels
 * @param targetHeight  the desired height of the scaled instance,
 *                      in pixels
 * @param hint          one of the rendering hints that corresponds to
 *                      {@code RenderingHints.KEY_INTERPOLATION} (e.g.
 *                      {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
 *                      {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
 *                      {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
 * @param higherQuality if true, this method will use a multi-step
 *                      scaling technique that provides higher quality than the usual
 *                      one-step technique (only useful in downscaling cases, where
 *                      {@code targetWidth} or {@code targetHeight} is
 *                      smaller than the original dimensions, and generally only when
 *                      the {@code BILINEAR} hint is specified)
 * @return a scaled version of the original {@code BufferedImage}
 */
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    final int type = img.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int w;
    int h;
    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;
            }
        }

        final BufferedImage tmp = new BufferedImage(w, h, type);
        final 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.android.hierarchyviewerlib.device.DeviceBridge.java

private static boolean readLayer(DataInputStream in, PsdFile psd) {
    try {/*from  w  w w. j a  va 2  s. c  om*/
        if (in.read() == 2) {
            return false;
        }
        String name = in.readUTF();
        boolean visible = in.read() == 1;
        int x = in.readInt();
        int y = in.readInt();
        int dataSize = in.readInt();

        byte[] data = new byte[dataSize];
        int read = 0;
        while (read < dataSize) {
            read += in.read(data, read, dataSize - read);
        }

        ByteArrayInputStream arrayIn = new ByteArrayInputStream(data);
        BufferedImage chunk = ImageIO.read(arrayIn);

        // Ensure the image is in the right format
        BufferedImage image = new BufferedImage(chunk.getWidth(), chunk.getHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(chunk, null, 0, 0);
        g.dispose();

        psd.addLayer(name, image, new Point(x, y), visible);

        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:view.FramePrincipal.java

private static BufferedImage createBufferedImage(String fileName) {
    BufferedImage image = null;
    try {/*from   w ww. j av a  2  s  .c o  m*/
        image = ImageIO.read(new File(fileName));
    } catch (IOException e) {
        return null;
    }

    int sizeX = image.getWidth();
    int sizeY = image.getHeight();

    BufferedImage result = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_RGB);
    Graphics g = result.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return result;
}