List of usage examples for java.awt.image BufferedImage getHeight
public int getHeight()
From source file:net.sf.mcf2pdf.mcfelements.util.ImageUtil.java
/** * Rotates the given buffered image by the given angle, and returns a newly * created image, containing the rotated image. * * @param img Image to rotate.//from w w w. j av a 2 s . c om * @param angle Angle, in radians, by which to rotate the image. * @param drawOffset Receives the offset which is required to draw the image, * relative to the original (0,0) corner, so that the center of the image is * still on the same position. * * @return A newly created image containing the rotated image. */ public static BufferedImage rotateImage(BufferedImage img, float angle, Point drawOffset) { int w = img.getWidth(); int h = img.getHeight(); AffineTransform tf = AffineTransform.getRotateInstance(angle, w / 2.0, h / 2.0); // get coordinates for all corners to determine real image size Point2D[] ptSrc = new Point2D[4]; ptSrc[0] = new Point(0, 0); ptSrc[1] = new Point(w, 0); ptSrc[2] = new Point(w, h); ptSrc[3] = new Point(0, h); Point2D[] ptTgt = new Point2D[4]; tf.transform(ptSrc, 0, ptTgt, 0, ptSrc.length); Rectangle rc = new Rectangle(0, 0, w, h); for (Point2D p : ptTgt) { if (p.getX() < rc.x) { rc.width += rc.x - p.getX(); rc.x = (int) p.getX(); } if (p.getY() < rc.y) { rc.height += rc.y - p.getY(); rc.y = (int) p.getY(); } if (p.getX() > rc.x + rc.width) rc.width = (int) (p.getX() - rc.x); if (p.getY() > rc.y + rc.height) rc.height = (int) (p.getY() - rc.y); } BufferedImage imgTgt = new BufferedImage(rc.width, rc.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = imgTgt.createGraphics(); // create a NEW rotation transformation around new center tf = AffineTransform.getRotateInstance(angle, rc.getWidth() / 2, rc.getHeight() / 2); g2d.setTransform(tf); g2d.drawImage(img, -rc.x, -rc.y, null); g2d.dispose(); drawOffset.x += rc.x; drawOffset.y += rc.y; return imgTgt; }
From source file:ConvertUtil.java
/** * Converts the source image to 4-bit colour using the given colour map. No * transparency./*from www . j a v a 2 s . c om*/ * * @param src * the source image to convert * @param cmap * the colour map, which should contain no more than 16 entries * The entries are in the form RRGGBB (hex). * @return a copy of the source image with a 4-bit colour depth, with the * custom colour pallette */ public static BufferedImage convert4(BufferedImage src, int[] cmap) { IndexColorModel icm = new IndexColorModel(4, cmap.length, cmap, 0, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY, icm); ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(), dest.getColorModel().getColorSpace(), null); cco.filter(src, dest); return dest; }
From source file:net.cloudkit.relaxation.CaptchaTest.java
public static void clearBorder(BufferedImage image) { final int width = image.getWidth(); final int height = image.getHeight(); // image.getMinX() image.getMinY() // //from w w w.j a v a 2s . co m for (int x = 0; x < width; x++) { image.setRGB(x, 0, Color.WHITE.getRGB()); } // ? for (int y = 0; y < height; y++) { image.setRGB(width - 1, y, Color.WHITE.getRGB()); } // for (int x = 0; x < width; x++) { image.setRGB(x, height - 1, Color.WHITE.getRGB()); } // for (int y = 0; y < height; y++) { image.setRGB(0, y, Color.WHITE.getRGB()); } }
From source file:com.github.zhanhb.ckfinder.connector.utils.ImageUtils.java
/** * creates dimension of thumb.// w w w . j a va 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(BufferedImage image, int maxWidth, int maxHeight) { log.debug("image(w={},h={}), max w={}, max h={}", image.getWidth(), image.getHeight(), maxWidth, maxHeight); int width = image.getWidth(), height = image.getHeight(); long w = (long) width * maxHeight; long h = (long) height * maxWidth; if (w > h) { if (width > maxWidth) { height = (int) Math.round(1.0 * h / width); width = maxWidth; } } else if (height > maxHeight) { width = (int) Math.round(1.0 * w / height); height = maxHeight; } log.debug("didmension w={}, h={}", width, height); return new Dimension(width, height); }
From source file:Main.java
private static ByteBuffer convertImageData(BufferedImage bufferedImage) { ByteBuffer imageBuffer;//from ww w . j av a 2 s .c o m WritableRaster raster; BufferedImage texImage; // for a texture if (bufferedImage.getColorModel().hasAlpha()) { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(), bufferedImage.getHeight(), 4, null); texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>()); } else { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(), bufferedImage.getHeight(), 3, null); texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>()); } // copy the source image into the produced image Graphics g = texImage.getGraphics(); g.setColor(new Color(0f, 0f, 0f, 0f)); g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight()); g.drawImage(bufferedImage, 0, 0, null); // build a byte buffer from the temporary image // that be used by OpenGL to produce a texture. byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData(); imageBuffer = ByteBuffer.allocateDirect(data.length); imageBuffer.order(ByteOrder.nativeOrder()); imageBuffer.put(data, 0, data.length); imageBuffer.flip(); return imageBuffer; }
From source file:de.ppi.selenium.util.ScreenshotUtils.java
/** * Checks if a screenshot is complete black. * * @param var the image.//from ww w.j a v a 2 s .c om * @return true if it is black. */ private static boolean isBlack(BufferedImage var) { final int scale = 3; double[] varArr = new double[var.getWidth() * var.getHeight() * scale]; // unroll pixels for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { varArr[i * var.getWidth() + j + 0] = new Color(var.getRGB(j, i)).getRed(); varArr[i * var.getWidth() + j + 1] = new Color(var.getRGB(j, i)).getGreen(); varArr[i * var.getWidth() + j + 2] = new Color(var.getRGB(j, i)).getBlue(); } } // test if all are black for (int i = 0; i != varArr.length; i++) { if (varArr[i] != 0) { return false; } } return true; }
From source file:de.bund.bfr.jung.JungUtils.java
private static Paint mixWith(Paint paint, Color mix) { if (paint instanceof Color) { Color c = (Color) paint; return new Color((c.getRed() + mix.getRed()) / 2, (c.getGreen() + mix.getGreen()) / 2, (c.getBlue() + mix.getBlue()) / 2, (c.getAlpha() + mix.getAlpha()) / 2); } else if (paint instanceof TexturePaint) { BufferedImage texture = ((TexturePaint) paint).getImage(); BufferedImage mixed = new BufferedImage(texture.getWidth(), texture.getHeight(), texture.getType()); for (int x = 0; x < texture.getWidth(); x++) { for (int y = 0; y < texture.getHeight(); y++) { mixed.setRGB(x, y, ((Color) mixWith(new Color(texture.getRGB(x, y)), mix)).getRGB()); }// w ww . ja va 2 s.c o m } return new TexturePaint(mixed, new Rectangle(mixed.getWidth(), mixed.getHeight())); } else { return paint; } }
From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java
/** * Creates image file with fixed width and height. * * @param sourceFile input file/*from ww w . j a v a2s.c om*/ * @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:com.jaeksoft.searchlib.util.ImageUtils.java
public final static BufferedImage getSubimage(BufferedImage image, int x, int y, int width, int height) { if (width > image.getWidth() - x) width = image.getWidth() - x;//from w ww . j a v a 2s . co m if (height > image.getHeight() - y) height = image.getHeight() - y; return image.getSubimage(x, y, width, height); }
From source file:com.github.zhanhb.ckfinder.connector.utils.ImageUtils.java
/** * Creates image file with fixed width and height. * * @param sourceFile input file//from w w w . j a va2 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); } }