List of usage examples for java.awt.image BufferedImage getWidth
public int getWidth()
From source file:com.buddycloud.mediaserver.business.util.ImageUtils.java
public static boolean isSquare(BufferedImage img) { return img.getHeight() == img.getWidth(); }
From source file:business.Reciept.java
private static model.Image createImage(MultipartFile multiPartFile) throws Exception { byte[] byteArray = multiPartFile.getBytes(); BufferedImage image = ImageIO.read(multiPartFile.getInputStream()); int height = image.getHeight(); int width = image.getWidth(); return new model.Image(byteArray, multiPartFile.getContentType(), height, width); }
From source file:ImageUtils.java
/** * Scales down an image into a box of maxSideLenght x maxSideLength. * @param image the image to scale down. It remains untouched. * @param maxSideLength the maximum side length of the scaled down instance. Has to be > 0. * @return the scaled image, the/*from w ww . j a v a 2 s.c o m*/ */ public static BufferedImage scaleImage(BufferedImage image, int maxSideLength) { assert (maxSideLength > 0); double originalWidth = image.getWidth(); double originalHeight = image.getHeight(); double scaleFactor = 0.0; if (originalWidth > originalHeight) { scaleFactor = ((double) maxSideLength / originalWidth); } else { scaleFactor = ((double) maxSideLength / originalHeight); } // create smaller image BufferedImage img = new BufferedImage((int) (originalWidth * scaleFactor), (int) (originalHeight * scaleFactor), BufferedImage.TYPE_INT_RGB); // fast scale (Java 1.4 & 1.5) Graphics g = img.getGraphics(); g.drawImage(image, 0, 0, img.getWidth(), img.getHeight(), null); return img; }
From source file:Main.java
public static BufferedImage replaceColor(BufferedImage src, int sourceRGB, int replaceRGB) { for (int y = 0; y < src.getHeight(); y++) { for (int x = 0; x < src.getWidth(); x++) { int rawRGB = src.getRGB(x, y); int rgb = rawRGB & 0xffffff; int alpha = rawRGB & 0xff000000; if (rgb == sourceRGB) { src.setRGB(x, y, alpha | replaceRGB); }/*from w w w . ja v a 2 s . c om*/ } } return src; }
From source file:GraphicsUtil.java
public static void drawCentered(Graphics g, BufferedImage img, Point2D location, ImageObserver observer) { drawCentered(g, img, location, img.getWidth(), img.getHeight(), observer); }
From source file:app.utils.ImageUtilities.java
public static Image readImageFromFile(File imgFile) throws IOException { String imgName = imgFile.getName(); BufferedImage img = ImageIO.read(imgFile); Pixel[][] pixels = new Pixel[img.getWidth()][img.getHeight()]; for (int x = 0; x < img.getWidth(); x++) { for (int y = 0; y < img.getHeight(); y++) { int redValue = new Color(img.getRGB(x, y)).getRed(); int greenValue = new Color(img.getRGB(x, y)).getGreen(); int blueValue = new Color(img.getRGB(x, y)).getBlue(); pixels[x][y] = new Pixel(redValue, greenValue, blueValue); }//w ww.ja v a 2 s . co m } return new Image(imgName, pixels, img.getRaster().getNumDataElements()); }
From source file:ImageUtil.java
public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException { int x = src.getWidth() / 2 - width / 2; int y = src.getHeight() / 2 - height / 2; // System.out.println("---" + src.getWidth() + " - " + src.getHeight() + " - " + x + " - " + y); BufferedImage clipping = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//src.getType()); Graphics2D area = (Graphics2D) clipping.getGraphics().create(); area.drawImage(src, 0, 0, clipping.getWidth(), clipping.getHeight(), x, y, x + clipping.getWidth(), y + clipping.getHeight(), null); area.dispose();/*w w w .j a v a2 s. c o m*/ return clipping; }
From source file:Main.java
public static int imageDifference(BufferedImage imgA, BufferedImage imgB) { int dHeight = Math.abs(imgA.getHeight() - imgB.getHeight()); int dWidth = Math.abs(imgA.getWidth() - imgB.getWidth()); int diff = 3 * 255 * dHeight * dWidth; for (int y = 0; y < Math.min(imgA.getHeight(), imgB.getHeight()); y++) { for (int x = 0; x < Math.min(imgA.getWidth(), imgB.getWidth()); x++) { final int colourA = imgA.getRGB(x, y); final int colourB = imgB.getRGB(x, y); diff += Math.abs((int) ((colourA & 0x00ff0000) >> 16) - (int) ((colourB & 0x00ff0000) >> 16)); diff += Math.abs((int) ((colourA & 0x0000ff00) >> 8) - (int) ((colourB & 0x0000ff00) >> 8)); diff += Math.abs((int) (colourA & 0x000000ff) - (int) (colourB & 0x000000ff)); }//from www . ja v a 2 s . c o m } return diff; }
From source file:com.buddycloud.mediaserver.business.util.ImageUtils.java
public static BufferedImage cropMaximumSquare(BufferedImage img) { int smallerSide = img.getHeight() <= img.getWidth() ? img.getHeight() : img.getWidth(); int x = img.getWidth() - smallerSide; int y = img.getHeight() - smallerSide; final BufferedImage croppedImg = Scalr.crop(img, x, y, smallerSide, smallerSide); return croppedImg; }
From source file:ImageUtil.java
public static ByteArrayOutputStream smartCrop(ByteArrayInputStream bais, int width, int height) throws IOException { BufferedImage src = ImageIO.read(bais); Float scale;//from w w w . jav a 2s .c o m if (src.getWidth() > src.getHeight()) { scale = Float.valueOf(height) / Float.valueOf(src.getHeight()); if (src.getWidth() * scale < width) { scale = Float.valueOf(width) / Float.valueOf(src.getWidth()); } } else { scale = Float.valueOf(width) / Float.valueOf(src.getWidth()); if (src.getHeight() * scale < height) { scale = Float.valueOf(height) / Float.valueOf(src.getHeight()); } } // // System.out.println("--- " + src.getWidth() + " - " + width); // System.out.println("--- " + src.getHeight() + " - " + height); // System.out.println("--- " + scale + " -- " + Float.valueOf(src.getWidth() * scale).intValue() + " -- " + Float.valueOf(src.getHeight() * scale).intValue()); // BufferedImage temp = scale(src, Float.valueOf(src.getWidth() * scale).intValue(), Float.valueOf(src.getHeight() * scale).intValue()); temp = crop(temp, width, height); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(temp, "JPG", baos); return baos; }