Example usage for java.awt.image BufferedImage getWidth

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

Introduction

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

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the BufferedImage .

Usage

From source file:lucee.runtime.img.ImageUtil.java

public static BufferedImage createBufferedImage(BufferedImage image) {
    return createBufferedImage(image, image.getWidth(), image.getHeight());
}

From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java

/**
 * creates dimension of thumb.//w ww .  j  av  a 2  s.  c om
 *
 * @param image original image.
 * @param maxWidth max thumb width
 * @param maxHeight max thumb height
 * @return dimension of thumb image.
 */
private static Dimension createThumbDimension(final BufferedImage image, final int maxWidth,
        final int maxHeight) {
    Dimension dimension = new Dimension();
    if (image.getWidth() >= image.getHeight()) {
        if (image.getWidth() >= maxWidth) {
            dimension.width = maxWidth;
            dimension.height = Math.round(((float) maxWidth / image.getWidth()) * image.getHeight());
        } else {
            dimension.height = image.getHeight();
            dimension.width = image.getWidth();
        }
    } else {
        if (image.getHeight() >= maxHeight) {
            dimension.height = maxHeight;
            dimension.width = Math.round((((float) maxHeight / image.getHeight()) * image.getWidth()));
        } else {
            dimension.height = image.getHeight();
            dimension.width = image.getWidth();
        }
    }
    return dimension;
}

From source file:com.github.zhanhb.ckfinder.connector.utils.ImageUtils.java

/**
 * Creates image file with fixed width and height.
 *
 * @param sourceFile input file//  www.j  ava2  s .c  o  m
 * @param destFile file to save
 * @param width image width
 * @param height image height
 * @param quality image quality
 * @throws IOException when IO Exception occurs.
 */
public static void createResizedImage(Path sourceFile, Path destFile, int width, int height, float quality)
        throws IOException {
    BufferedImage image;
    try (InputStream is = Files.newInputStream(sourceFile)) {
        image = ImageIO.read(is);
    }
    if (image.getHeight() <= height && image.getWidth() <= width) {
        writeUntouchedImage(sourceFile, destFile);
    } else {
        resizeImage(image, width, height, quality, destFile);
    }
}

From source file:com.liusoft.dlog4j.util.ImageUtils.java

/**
 * ???/* w  ww  . j  av a2s. com*/
 * ?????
 * 3: 180
 * 6: 90
 * 8: 27090
 * @param img_fn
 * @param orient
 * @throws IOException 
 */
public static boolean rotateImage(String img_fn, int orient, String dest_fn) throws IOException {
    double radian = 0;
    switch (orient) {
    case 3:
        radian = 180.0;
        break;
    case 6:
        radian = 90.0;
        break;
    case 8:
        radian = 270.0;
        break;
    default:
        return false;
    }
    BufferedImage old_img = (BufferedImage) ImageIO.read(new File(img_fn));
    int width = old_img.getWidth();
    int height = old_img.getHeight();

    BufferedImage new_img = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = new_img.createGraphics();

    AffineTransform origXform = g2d.getTransform();
    AffineTransform newXform = (AffineTransform) (origXform.clone());
    // center of rotation is center of the panel
    double xRot = 0;
    double yRot = 0;
    switch (orient) {
    case 3:
        xRot = width / 2.0;
        yRot = height / 2.0;
    case 6:
        xRot = height / 2.0;
        yRot = xRot;
        break;
    case 8:
        xRot = width / 2.0;
        yRot = xRot;
        break;
    default:
        return false;
    }
    newXform.rotate(Math.toRadians(radian), xRot, yRot);

    g2d.setTransform(newXform);
    // draw image centered in panel
    g2d.drawImage(old_img, 0, 0, null);
    // Reset to Original
    g2d.setTransform(origXform);

    FileOutputStream out = new FileOutputStream(dest_fn);
    try {
        ImageIO.write(new_img, "JPG", out);
    } finally {
        out.close();
    }
    return true;
}

From source file:net.cloudkit.relaxation.CaptchaTest.java

public static void clearNoise(BufferedImage image) {
    final int width = image.getWidth();
    final int height = image.getHeight();

    /*/*from w  w w  . j av a2s  .com*/
    for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
        image.setRGB(x, y, Color.WHITE.getRGB());
    }
    }
    */
    for (int x = image.getMinX(); x < width; x++) {
        for (int y = image.getMinY(); y < height; y++) {

            int point = image.getRGB(x, y);

            // 
            int top = (y > 0) ? image.getRGB(x, y - 1) : -1;
            // ?
            int right = (x < width - 1) ? image.getRGB(x + 1, y) : -1;
            // 
            int bottom = (y < height - 1) ? image.getRGB(x, y + 1) : -1;
            // 
            int left = (x > 0) ? image.getRGB(x - 1, y) : -1;
            // ?
            int topRight = ((x < width - 1) && (y > 0)) ? image.getRGB(x + 1, y - 1) : -1;
            // 
            int topLeft = ((x > 0) && (y > 0)) ? image.getRGB(x - 1, y - 1) : -1;
            // ?
            int bottomRight = ((x < width - 1) && (y < height - 1)) ? image.getRGB(x + 1, y + 1) : -1;
            // 
            int bottomLeft = ((x > 0) && (y < height - 1)) ? image.getRGB(x - 1, y + 1) : -1;

            int i = 0;
            i = (top != -1) ? i + 1 : i;
            i = (right != -1) ? i + 1 : i;
            i = (bottom != -1) ? i + 1 : i;
            i = (left != -1) ? i + 1 : i;

            i = (topRight != -1) ? i + 1 : i;
            i = (topLeft != -1) ? i + 1 : i;
            i = (bottomRight != -1) ? i + 1 : i;
            i = (bottomLeft != -1) ? i + 1 : i;

            // System.out.println("i:" + i + ", top:" + top + ", right:" + right + ", bottom:" + bottom + ", left:" + left + ", topRight:" + topRight + ", topLeft:" + topLeft + ", bottomRight:" + bottomRight + ", bottomLeft:" + bottomLeft);
            if (i < 5) {
                image.setRGB(x, y, Color.WHITE.getRGB());
            }

            /*
            int leftNearby = 0;
            if(left != -1) {
            int secondLeft = (x > 1) ? image.getRGB(x - 2, y) : -1;
            int threeLeft = (x > 2) ? image.getRGB(x - 3, y) : -1;
                    
            leftNearby = ((left == -1) ? 0 : 1) + ((secondLeft == -1) ? 0 : 1) + ((threeLeft == -1) ? 0 : 1);
            }
                    
            int rightNearby = 0;
            if(right != -1) {
            int secondRight = (x + 1 < width - 1) ? image.getRGB(x + 2, y) : -1;
            int threeRight = (x + 2 < width - 1) ? image.getRGB(x + 3, y) : -1;
                    
            rightNearby = ((right == -1) ? 0 : 1) + ((secondRight == -1) ? 0 : 1) + ((threeRight == -1) ? 0 : 1);
            }
                    
            int topNearby = 0;
            if(top != -1) {
            int secondTop = (y > 1) ? image.getRGB(x, y - 2) : -1;
            int threeTop = (y > 2) ? image.getRGB(x, y - 3) : -1;
                    
            topNearby = ((top == -1) ? 0 : 1) + ((secondTop == -1) ? 0 : 1) + ((threeTop == -1) ? 0 : 1);
            }
                    
            int bottomNearby = 0;
            if(bottom != -1) {
            int secondBottom = (y + 1 < height - 1) ? image.getRGB(x, y + 2) : -1;
            int threeBottom = (y + 2 < height - 1) ? image.getRGB(x, y + 3) : -1;
                    
            bottomNearby = ((bottom == -1) ? 0 : 1) + ((secondBottom == -1) ? 0 : 1) + ((threeBottom == -1) ? 0 : 0);
            }
                    
            System.out.println(leftNearby + " " + rightNearby + " " + topNearby + " " + bottomNearby);
            if (leftNearby != 2 && rightNearby != 2 && topNearby != 2 && bottomNearby != 2) {
            image.setRGB(x, y, Color.WHITE.getRGB());
            }
            */

            /*
            System.out.println(point - top);
            System.out.println(point - right);
            System.out.println(point - bottom);
            System.out.println(point - left);
            System.out.println(point - topRight);
            System.out.println(point - topLeft);
            System.out.println(point - bottomRight);
            System.out.println(point - bottomLeft);
            */

            // if (point != top && point != right && point != bottom && point != left && point != topRight && point != topLeft && point != bottomRight && point != bottomLeft) {
            //     image.setRGB(x, y, Color.WHITE.getRGB());
            // }

            /*
            Color color = new Color(image.getRGB(x, y));
            if((color.getBlue() < 120) || ((color.getRed() + color.getGreen() + color.getBlue()) < 50)) { //
            image.setRGB(x, y, Color.WHITE.getRGB());
            } else if((color.getRed() + color.getGreen() + color.getBlue()) < 400) {
            image.setRGB(x, y, Color.BLACK.getRGB());
            }
            */

            Color color = new Color(image.getRGB(x, y));
            if ((color.getRed() + color.getGreen() + color.getBlue()) < 600) {
                image.setRGB(x, y, Color.BLACK.getRGB());
            } else {
                image.setRGB(x, y, Color.WHITE.getRGB());
            }
        }
    }

}

From source file:net.cloudkit.relaxation.CaptchaTest.java

public static List<BufferedImage> splitImage(BufferedImage img) throws Exception {
    final List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
    final int width = img.getWidth();
    final int height = img.getHeight();
    final List<Integer> weightList = new ArrayList<Integer>();
    for (int x = 0; x < width; ++x) {
        int count = 0;
        for (int y = 0; y < height; ++y) {
            if (isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;/*from   w  ww . j  a  v  a 2 s. c om*/
            }
        }
        weightList.add(count);
    }
    // System.out.println(weightList.size());
    for (int i = 0; i < weightList.size(); i++) {
        int length = 0;
        while (i < weightList.size() && weightList.get(i) > 0) {
            i++;
            length++;
        }
        if (length > 18) {
            subImgs.add(removeBlank(img.getSubimage(i - length, 0, length / 2, height), whiteThreshold, 0));
            subImgs.add(removeBlank(img.getSubimage(i - length / 2, 0, length / 2, height), whiteThreshold, 0));
        } else if (length > 2) {
            subImgs.add(removeBlank(img.getSubimage(i - length, 0, length, height), whiteThreshold, 0));
        }
    }

    return subImgs;
}

From source file:common.utils.ImageUtils.java

/**
 * resize input image to new dinesions(only smaller) and save it to outputStream
 * @param file_stream input image for scaling
 * @param new_width desired value of width in result image
* @param rez writes resulting image to a file
* @throws IOException//from w w  w.j av a 2s .  com
 */
public static void saveScaledImageWidth(File file_stream, int new_width, OutputStream rez) throws IOException {
    //long time = System.nanoTime();
    BufferedImage image = ImageIO.read(file_stream);
    double scale_factor = getScaling(image.getWidth(), new_width);
    saveScaledImageWidth(image, scale_factor, rez);
}

From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java

/**
 * Creates image file with fixed width and height.
 *
 * @param sourceFile input file/* w  w  w  . j  av  a2s.  com*/
 * @param destFile file to save
 * @param width image width
 * @param height image height
 * @param quality image quality
 * @throws IOException when error occurs.
 */
public static void createResizedImage(final File sourceFile, final File destFile, final int width,
        final int height, final float quality) throws IOException {

    BufferedImage image = ImageIO.read(sourceFile);
    Dimension dimension = new Dimension(width, height);
    if (image.getHeight() == dimension.height && image.getWidth() == dimension.width) {
        writeUntouchedImage(sourceFile, destFile);
    } else {
        resizeImage(image, dimension.width, dimension.height, quality, destFile);

    }

}

From source file:Main.java

private static IntBuffer getImageAsARGBIntBuffer(BufferedImage image) {
    DataBuffer buffer = image.getRaster().getDataBuffer();

    if (buffer instanceof DataBufferInt) {
        return IntBuffer.wrap(((DataBufferInt) buffer).getData());
    } else if (buffer instanceof DataBufferByte) {
        return ByteBuffer.wrap(((DataBufferByte) buffer).getData()).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
    } else {//from w ww  .  java2  s.com
        int width = image.getWidth();
        int height = image.getHeight();
        int[] pixels = new int[width * height];
        image.getRGB(0, 0, width, height, pixels, 0, width);
        return IntBuffer.wrap(pixels);
    }
}

From source file:ImageProcessing.ImageProcessing.java

public static double[] extractGrayColor(BufferedImage source) {
    //Extracts the gray value from the pixels at the source by 
    //calculating the average of the RGB value at the given pixel.
    int imageWidth = source.getWidth();
    int imageHeight = source.getHeight();
    double[] values = new double[imageWidth * imageHeight];

    for (int i = 0; i < imageHeight; i++) {
        for (int j = 0; j < imageWidth; j++) {
            int rgbValue = source.getRGB(j, i);
            Color currentPixel = new Color(rgbValue, true);
            int value = (currentPixel.getRed() + currentPixel.getGreen() + currentPixel.getBlue()) / 3;
            values[(i * imageWidth) + j] = value;
        }//  www  .  jav  a2s  . c  o  m
    }

    return values;
}