Example usage for java.awt.image BufferedImage getType

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

Introduction

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

Prototype

public int getType() 

Source Link

Document

Returns the image type.

Usage

From source file:org.uiautomation.ios.utils.InstrumentsGeneratedImage.java

private BufferedImage rotate() {
    int rotateDegrees = orientation.getRotationInDegree();
    boolean flip = orientation == Orientation.LANDSCAPE
            || orientation == Orientation.UIA_DEVICE_ORIENTATION_LANDSCAPERIGHT;
    try {// w  w  w.  ja  va2 s .co  m
        final BufferedImage originalImage = ImageIO.read(source);

        // no rotation needed.
        if (rotateDegrees == 0) {
            return originalImage;
        }

        // need to rotate.
        final BufferedImage rotated;
        int width;
        int height;
        if (flip) {
            width = originalImage.getHeight();
            height = originalImage.getWidth();
        } else {
            width = originalImage.getWidth();
            height = originalImage.getHeight();
        }
        rotated = new BufferedImage(width, height, originalImage.getType());

        // Rotate the image and then move it back up to the origin through a translation call, since it'll pivot around
        // the center point which will cause non-square images to offset by the different in height and width.
        final Graphics2D graphics = rotated.createGraphics();
        graphics.rotate(Math.toRadians(rotateDegrees), rotated.getWidth() / 2, rotated.getHeight() / 2);
        graphics.translate((rotated.getWidth() - originalImage.getWidth()) / 2,
                (rotated.getHeight() - originalImage.getHeight()) / 2);
        graphics.drawImage(originalImage, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
        return rotated;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.uva.itast.blended.omr.BufferedImageUtil.java

/**
 * Rotates 90, -90 deegrees//ww w. j  a v a  2  s  . c  o m
 * @param imageToRotate
 * @param degrees 90 or -90 rotation.
 * @return
 */
public static BufferedImage rotateImage(BufferedImage imageToRotate, float degrees) {
    if (degrees != 90.0 && degrees != -90.0)
        throw new IllegalArgumentException("Only implemented +90 and -90 rotation");

    BufferedImage rotatedImage = new BufferedImage(imageToRotate.getHeight(null), imageToRotate.getWidth(null),
            imageToRotate.getType());

    Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
    g2d.rotate(Math.toRadians(degrees));
    g2d.drawImage(imageToRotate, 0, -rotatedImage.getWidth(null), null);

    return rotatedImage;
}

From source file:org.xsystem.sql2.http.impl.ImgHelper.java

public static void resaize(String format, int IMG_SIZE, InputStream sorce, OutputStream target)
        throws IOException {
    BufferedImage originalImage = ImageIO.read(sorce);
    int type = format.equalsIgnoreCase("gif") || (originalImage.getType() == 0) ? BufferedImage.TYPE_INT_ARGB
            : originalImage.getType();/* w w w  .j a  v  a2 s  .  co  m*/
    Dimension new_dim = //new Dimension(IMG_SIZE, IMG_SIZE);
            getScaledDimension(new Dimension(originalImage.getWidth(), originalImage.getHeight()),
                    new Dimension(IMG_SIZE, IMG_SIZE));
    BufferedImage resizedImage = new BufferedImage((int) new_dim.getWidth(), (int) new_dim.getHeight(), type);
    Graphics2D g2 = resizedImage.createGraphics();
    g2.drawImage(originalImage, 0, 0, (int) new_dim.getWidth(), (int) new_dim.getHeight(), null);
    g2.dispose();
    ImageIO.write(resizedImage, format, target);
}

From source file:org.yes.cart.service.domain.impl.ImageServiceImpl.java

/**
 * {@inheritDoc}// w ww  . j  av  a2s .co  m
 */
public byte[] resizeImage(final String filename, final byte[] content, final String width, final String height,
        final boolean cropToFit) {

    try {

        final InputStream bis = new ByteArrayInputStream(content);

        final BufferedImage originalImg = ImageIO.read(bis);
        final String codec = getCodecFromFilename(filename);
        final boolean supportsAlpha = hasAlphaSupport(codec);

        int x = NumberUtils.toInt(width);
        int y = NumberUtils.toInt(height);
        int originalX = originalImg.getWidth();
        int originalY = originalImg.getHeight();

        boolean doCropToFit = cropToFit || x < forceCropToFitOnSize || y < forceCropToFitOnSize;

        final int imageType = originalImg.getType();

        final Image resizedImg;
        //            final BufferedImage resizedImg;
        final int padX, padY;
        if (doCropToFit) {
            // crop the original to best fit of target size
            int[] cropDims = cropImageToCenter(x, y, originalX, originalY);
            padX = 0;
            padY = 0;

            final BufferedImage croppedImg = originalImg.getSubimage(cropDims[0], cropDims[1], cropDims[2],
                    cropDims[3]);
            resizedImg = croppedImg.getScaledInstance(x, y, Image.SCALE_SMOOTH);

            //                final BufferedImage croppedImg = originalImg.getSubimage(cropDims[0], cropDims[1], cropDims[2], cropDims[3]);
            //                resizedImg = new BufferedImage(y, x, imageType);
            //                Graphics2D graphics = resizedImg.createGraphics();
            //                graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            //                graphics.drawImage(croppedImg, 0, 0, x, y, null);
        } else {
            int[] scaleDims = scaleImageToCenter(x, y, originalX, originalY);
            padX = scaleDims[0];
            padY = scaleDims[1];

            resizedImg = originalImg.getScaledInstance(scaleDims[2], scaleDims[3], Image.SCALE_SMOOTH);

            //                resizedImg = new BufferedImage(scaleDims[3], scaleDims[2], imageType);
            //                Graphics2D graphics = resizedImg.createGraphics();
            //                graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            //                graphics.drawImage(originalImg, 0, 0, scaleDims[2], scaleDims[3], null);
        }

        // base canvas
        final BufferedImage resizedImgFinal = new BufferedImage(x, y, imageType);

        // fill base color
        final Graphics2D graphics = resizedImgFinal.createGraphics();
        graphics.setPaint(supportsAlpha ? alphaBorder : defaultBorder);
        graphics.fillRect(0, 0, x, y);

        // insert scaled image
        graphics.drawImage(resizedImg, padX, padY, null);

        final ByteArrayOutputStream bos = new ByteArrayOutputStream();

        ImageIO.write(resizedImgFinal, codec, bos);

        return bos.toByteArray();

    } catch (Exception exp) {
        ShopCodeContext.getLog(this).error("Unable to resize image " + filename, exp);
    }

    return new byte[0];

}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage rgb2gray(BufferedImage img) {
    if (img.getType() == BufferedImage.TYPE_INT_ARGB || img.getType() == BufferedImage.TYPE_INT_RGB) {
        int w, h;
        w = img.getWidth();/*from w w w.j  a v  a2 s  .c om*/
        h = img.getHeight();
        BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);

        //----------------------------v3.0----------------------
        ColorModel cm = img.getColorModel();
        int pixel, gr;
        int r, g, b;
        WritableRaster raster = out.getRaster();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);
                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                gr = (int) Math.round(((double) r + (double) g + (double) b) / 3.0);
                raster.setSample(x, y, 0, gr);
            }
        }
        return out;
    }
    return img;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage[] split2RGBA(BufferedImage img) {
    int w, h, a, r, g, b, pixel;

    if (img.getType() == BufferedImage.TYPE_INT_ARGB) {
        w = img.getWidth();//  ww  w.j  av  a2  s . c  o  m
        h = img.getHeight();
        BufferedImage[] out = new BufferedImage[4];
        WritableRaster[] rasters = new WritableRaster[4];
        for (int i = 0; i < 4; i++) {
            out[i] = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
            rasters[i] = out[i].getRaster();
        }

        ColorModel cm = img.getColorModel();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);

                a = cm.getAlpha(pixel);
                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                rasters[3].setSample(x, y, 0, a);
                rasters[0].setSample(x, y, 0, r);
                rasters[1].setSample(x, y, 0, g);
                rasters[2].setSample(x, y, 0, b);
            }
        }
        return out;
    } else {
        return null;
    }
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage[] split2RGB(BufferedImage img) {

    //--------------------v2.0--------------------------------------
    int w, h, r, g, b, pixel;

    if (img.getType() == BufferedImage.TYPE_INT_RGB) {
        w = img.getWidth();//from   w  w  w.  j a v a2s  . c o  m
        h = img.getHeight();
        BufferedImage[] out = new BufferedImage[3];
        WritableRaster[] rasters = new WritableRaster[3];
        for (int i = 0; i < 3; i++) {
            out[i] = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
            rasters[i] = out[i].getRaster();
        }

        ColorModel cm = img.getColorModel();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);

                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                rasters[0].setSample(x, y, 0, r);
                rasters[1].setSample(x, y, 0, g);
                rasters[2].setSample(x, y, 0, b);
            }
        }
        return out;
    } else {
        return null;
    }
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage compensateLensDistortion(BufferedImage img, double k) {
    if (img == null) {
        return null;
    }//from ww  w  .  j av  a  2s.  co  m

    int w = img.getWidth();
    int h = img.getHeight();
    BufferedImage out = new BufferedImage(w, h, img.getType());

    int x0 = (int) Math.floor(w / 2) + 1;
    int y0 = (int) Math.floor(h / 2) + 1;

    double ru, theta, ww, rd;
    int xd, yd;

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            ru = Math.sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0));
            theta = Math.atan2(y - y0, x - x0);
            ww = Math.pow(ru / (2 * k) + Math.sqrt((ru * ru) / (4 * k * k) + 1 / (27 * k * k * k)), 1.0 / 3.0);
            rd = ww - 1 / (3 * k * ww);

            //nearest neighbour---------------------------------------
            xd = (int) Math.round(rd * Math.cos(theta)) + x0;
            yd = (int) Math.round(rd * Math.sin(theta)) + y0;
            if (xd >= 0 && yd >= 0 && xd < w && yd < h) {
                //piksel nowy x,y = piksel stary xd,yd
                out.setRGB(x, y, img.getRGB(xd, yd));
            }
            //---------------------------------------------------------
        }
    }
    return out;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage compensateLensDistortion2(BufferedImage img, double k) {
    if (img == null) {
        return null;
    }//from www .  jav a 2  s .c  om

    int w = img.getWidth();
    int h = img.getHeight();
    //BufferedImage out = new BufferedImage(w, h, img.getType());

    int x0 = (int) Math.floor(w / 2) + 1;
    int y0 = (int) Math.floor(h / 2) + 1;

    double ru, theta, ww, rd;
    int xd, yd;

    double rdmax = Math.sqrt((w - x0) * (w - x0) + (h - y0) * (h - y0));
    double rumax = rdmax * (1 + k * rdmax * rdmax);
    //System.out.println("rdmax="+rdmax);
    //System.out.println("rumax="+rumax);
    double thetamax = Math.atan2(h - y0, w - x0);

    int xmax = (int) Math.round(rumax * Math.cos(thetamax)) * 2;
    int ymax = (int) Math.round(rumax * Math.sin(thetamax)) * 2;
    //System.out.println("xmax="+xmax);
    //System.out.println("ymax="+ymax);

    BufferedImage out = new BufferedImage(xmax, ymax, img.getType());

    int newx0 = (int) Math.floor(xmax / 2) + 1;
    int newy0 = (int) Math.floor(ymax / 2) + 1;

    int dx = (int) ((xmax - w) / 2) - 1;
    int dy = (int) ((ymax - h) / 2) - 1;

    for (int x = 0; x < xmax; x++) {
        for (int y = 0; y < ymax; y++) {
            ru = Math.sqrt((x - newx0) * (x - newx0) + (y - newy0) * (y - newy0));
            theta = Math.atan2(y - newy0, x - newx0);
            ww = Math.pow(ru / (2 * k) + Math.sqrt((ru * ru) / (4 * k * k) + 1 / (27 * k * k * k)), 1.0 / 3.0);
            rd = ww - 1 / (3 * k * ww);

            //nearest neighbour---------------------------------------
            xd = (int) Math.round(rd * Math.cos(theta)) + x0;
            yd = (int) Math.round(rd * Math.sin(theta)) + y0;

            if (xd >= 0 && yd >= 0 && xd < w && yd < h) {
                //piksel nowy x,y = piksel stary xd,yd
                out.setRGB(x, y, img.getRGB(xd, yd));
            }
            //---------------------------------------------------------
        }
    }
    return out;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage cylindricalMapping(BufferedImage img, double f) {
    if (img == null) {
        return null;
    }/*  w  ww  . j a v a 2 s  .  c  o  m*/

    int w = img.getWidth();
    int h = img.getHeight();
    BufferedImage out = new BufferedImage(w, h, img.getType());
    //System.out.println("w:"+w+", h:"+h);

    int x0 = (int) Math.floor(w / 2) + 1;
    int y0 = (int) Math.floor(h / 2) + 1;

    double tmax = Math.atan2((double) (w - x0), f);
    double tmin = Math.atan2(-((double) x0), f);
    double tstep = (tmax - tmin) / ((double) w);

    double vmax = ((double) (h - y0)) / f;
    double vmin = (-(double) y0) / f;
    double vstep = (vmax - vmin) / ((double) h);

    double theta, tan, cos;
    int x, y;

    for (int t = 0; t < w; t++) {
        theta = tmin + (double) t * tstep;
        tan = Math.tan(theta);
        cos = Math.cos(theta);
        x = (int) Math.round(f * tan) + x0;
        for (int v = 0; v < h; v++) {
            //nearest neighbour---------------------------------------
            //x = (int)Math.round(f*tan) + x0;
            y = (int) Math.round((vmin + (double) v * vstep) * f / cos) + y0;
            if (x >= 0 && y >= 0 && x < w && y < h) {
                //piksel nowy x,y = piksel stary xd,yd
                out.setRGB(t, v, img.getRGB(x, y));
            }
            //---------------------------------------------------------
        }
    }
    return out;
}