List of usage examples for java.awt.image BufferedImage getHeight
public int getHeight()
From source file:net.groupbuy.util.ImageUtils.java
/** * /*ww w . j ava 2 s . c o m*/ * * @param srcFile * ? * @param destFile * * @param destWidth * * @param destHeight * */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile); Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { Graphics2D graphics2D = null; ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); imageOutputStream = ImageIO.createImageOutputStream(destFile); imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName())) .next(); imageWriter.setOutput(imageOutputStream); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0)); imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam); imageOutputStream.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (graphics2D != null) { graphics2D.dispose(); } if (imageWriter != null) { imageWriter.dispose(); } if (imageOutputStream != null) { try { imageOutputStream.close(); } catch (IOException e) { } } } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:com.galenframework.utils.GalenUtils.java
public static File makeFullScreenshot(WebDriver driver) throws IOException, InterruptedException { // scroll up first scrollVerticallyTo(driver, 0);// w ww . j a v a2 s .c o m byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes)); int capturedWidth = image.getWidth(); int capturedHeight = image.getHeight(); long longScrollHeight = (Long) ((JavascriptExecutor) driver).executeScript( "return Math.max(" + "document.body.scrollHeight, document.documentElement.scrollHeight," + "document.body.offsetHeight, document.documentElement.offsetHeight," + "document.body.clientHeight, document.documentElement.clientHeight);"); Double devicePixelRatio = ((Number) ((JavascriptExecutor) driver) .executeScript(JS_RETRIEVE_DEVICE_PIXEL_RATIO)).doubleValue(); int scrollHeight = (int) longScrollHeight; File file = File.createTempFile("screenshot", ".png"); int adaptedCapturedHeight = (int) (((double) capturedHeight) / devicePixelRatio); BufferedImage resultingImage; if (Math.abs(adaptedCapturedHeight - scrollHeight) > 40) { int scrollOffset = adaptedCapturedHeight; int times = scrollHeight / adaptedCapturedHeight; int leftover = scrollHeight % adaptedCapturedHeight; final BufferedImage tiledImage = new BufferedImage(capturedWidth, (int) (((double) scrollHeight) * devicePixelRatio), BufferedImage.TYPE_INT_RGB); Graphics2D g2dTile = tiledImage.createGraphics(); g2dTile.drawImage(image, 0, 0, null); int scroll = 0; for (int i = 0; i < times - 1; i++) { scroll += scrollOffset; scrollVerticallyTo(driver, scroll); BufferedImage nextImage = ImageIO.read( new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES))); g2dTile.drawImage(nextImage, 0, (i + 1) * capturedHeight, null); } if (leftover > 0) { scroll += scrollOffset; scrollVerticallyTo(driver, scroll); BufferedImage nextImage = ImageIO.read( new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES))); BufferedImage lastPart = nextImage.getSubimage(0, nextImage.getHeight() - (int) (((double) leftover) * devicePixelRatio), nextImage.getWidth(), leftover); g2dTile.drawImage(lastPart, 0, times * capturedHeight, null); } scrollVerticallyTo(driver, 0); resultingImage = tiledImage; } else { resultingImage = image; } if (GalenConfig.getConfig().shouldAutoresizeScreenshots()) { try { resultingImage = GalenUtils.resizeScreenshotIfNeeded(driver, resultingImage); } catch (Exception ex) { LOG.trace("Couldn't resize screenshot", ex); } } ImageIO.write(resultingImage, "png", file); return file; }
From source file:kz.supershiny.core.services.ImageService.java
/** * Creates thumb for image.//from w ww.j av a2 s .c om * * @param originalData original image in byte array * @param type original - 0, large - 1, small - 2 * @return resized image in byte array */ public static byte[] resizeImage(byte[] originalData, ImageSize type) { //if original flag, then return original if (type.equals(ImageSize.ORIGINAL)) { return originalData; } BufferedImage originalImage = null; BufferedImage resizedImage = null; byte[] result = null; //convert bytes to BufferedImage try (InputStream in = new ByteArrayInputStream(originalData)) { originalImage = ImageIO.read(in); } catch (IOException ex) { LOG.error("Cannot convert byte array to BufferedImage!", ex); return null; } //get original size int scaledHeight = originalImage.getHeight(); int scaledWidth = originalImage.getWidth(); switch (type) { case LARGE: scaledWidth = LARGE_WIDTH; scaledHeight = LARGE_HEIGHT; break; case SMALL: scaledWidth = SMALL_WIDTH; scaledHeight = SMALL_HEIGHT; break; default: break; } //calculate aspect ratio float ratio = 1.0F * originalImage.getWidth() / originalImage.getHeight(); if (ratio > 1.0F) { scaledHeight = (int) (scaledHeight / ratio); } else { scaledWidth = (int) (scaledWidth * ratio); } //resize with hints resizedImage = new BufferedImage(scaledWidth, scaledHeight, originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType()); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); g.dispose(); g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //convert BufferedImage to bytes try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { ImageIO.write(resizedImage, "png", baos); baos.flush(); result = baos.toByteArray(); } catch (IOException ex) { LOG.error("Cannot convert BufferedImage to byte array!", ex); } return result; }
From source file:com.mikenimer.familydam.services.photos.ThumbnailService.java
/** * using the metadata orientation transformation information rotate the image. * @param image//w ww . j av a 2s . c om * @param transform * @return * @throws Exception */ public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception { AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); BufferedImage destinationImage = op.createCompatibleDestImage(image, (image.getType() == BufferedImage.TYPE_BYTE_GRAY) ? image.getColorModel() : null); Graphics2D g = destinationImage.createGraphics(); g.setBackground(Color.WHITE); g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight()); destinationImage = op.filter(image, destinationImage); return destinationImage; }
From source file:com.el.ecom.utils.ImageUtils.java
/** * //from w w w . ja v a2 s . c om * * @param srcFile ? * @param destFile * @param destWidth * @param destHeight */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile); Assert.state(srcFile.exists()); Assert.state(srcFile.isFile()); Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { Graphics2D graphics2D = null; ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); imageOutputStream = ImageIO.createImageOutputStream(destFile); imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName())) .next(); imageWriter.setOutput(imageOutputStream); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0)); imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam); imageOutputStream.flush(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { if (graphics2D != null) { graphics2D.dispose(); } if (imageWriter != null) { imageWriter.dispose(); } try { if (imageOutputStream != null) { imageOutputStream.close(); } } catch (IOException e) { } } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); try { operation.addImage(srcFile.getCanonicalPath()); operation.addImage(destFile.getCanonicalPath()); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (InterruptedException e) { throw new RuntimeException(e.getMessage(), e); } catch (IM4JavaException e) { throw new RuntimeException(e.getMessage(), e); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (InterruptedException e) { throw new RuntimeException(e.getMessage(), e); } catch (IM4JavaException e) { throw new RuntimeException(e.getMessage(), e); } } } }
From source file:com.sketchy.image.ImageProcessingThread.java
public static BufferedImage resizeImage(BufferedImage image, int drawingWidth, int drawingHeight, CenterOption centerOption, ScaleOption scaleOption) { int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double widthScale = drawingWidth / (double) imageWidth; double heightScale = drawingHeight / (double) imageHeight; double scale = Math.min(widthScale, heightScale); int scaleWidth = (int) (imageWidth * scale); int scaleHeight = (int) (imageHeight * scale); BufferedImage resizedImage = new BufferedImage(scaleWidth, scaleHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = resizedImage.createGraphics(); if (scaleOption == ScaleOption.SCALE_AREA_AVERAGING) { ImageProducer prod = new FilteredImageSource(image.getSource(), new AreaAveragingScaleFilter(scaleWidth, scaleHeight)); Image img = Toolkit.getDefaultToolkit().createImage(prod); g.drawImage(img, 0, 0, scaleWidth, scaleHeight, null); // it's already scaled } else {// w w w .j ava 2 s.c o m if (scaleOption == ScaleOption.SCALE_NEAREST_NEIGHBOR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); } else if (scaleOption == ScaleOption.SCALE_BICUBIC) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); } else if (scaleOption == ScaleOption.SCALE_BILINEAR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); } g.drawImage(image, 0, 0, scaleWidth, scaleHeight, 0, 0, imageWidth, imageHeight, null); } g.dispose(); int cropWidth = (int) Math.min(scaleWidth, drawingWidth); int cropHeight = (int) Math.min(scaleHeight, drawingHeight); int drawingLeft = 0; int drawingTop = 0; if ((centerOption == CenterOption.CENTER_HORIZONTAL) || (centerOption == CenterOption.CENTER_BOTH)) { drawingLeft = (int) ((drawingWidth - cropWidth) / 2.0); } if ((centerOption == CenterOption.CENTER_VERTICAL) || (centerOption == CenterOption.CENTER_BOTH)) { drawingTop = (int) ((drawingHeight - cropHeight) / 2.0); } BufferedImage croppedImage = resizedImage.getSubimage(0, 0, cropWidth, cropHeight); resizedImage = null; BufferedImage drawingImage = new BufferedImage(drawingWidth, drawingHeight, BufferedImage.TYPE_INT_ARGB); g = drawingImage.createGraphics(); g.drawImage(croppedImage, drawingLeft, drawingTop, drawingLeft + cropWidth, drawingTop + cropHeight, 0, 0, cropWidth, cropHeight, null); g.dispose(); croppedImage = null; return drawingImage; }
From source file:com.fengduo.bee.service.impl.file.FileServiceImpl.java
/** * ?//w ww . ja va 2 s . c om * * @param source * @param targetW * @param targetH * @param ifScaling ? * @return */ public static BufferedImage resize(BufferedImage source, int targetW, int targetH, boolean ifScaling) { // targetWtargetH int type = source.getType(); BufferedImage target = null; double sx = (double) targetW / source.getWidth(); double sy = (double) targetH / source.getHeight(); // targetWtargetH??,?if else??? if (ifScaling) { if (sx > sy) { sx = sy; targetW = (int) (sx * source.getWidth()); } else { sy = sx; targetH = (int) (sy * source.getHeight()); } } if (type == BufferedImage.TYPE_CUSTOM) { // handmade ColorModel cm = source.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH); boolean alphaPremultiplied = cm.isAlphaPremultiplied(); target = new BufferedImage(cm, raster, alphaPremultiplied, null); } else target = new BufferedImage(targetW, targetH, type); Graphics2D g = target.createGraphics(); // smoother than exlax: g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy)); g.dispose(); return target; }
From source file:com.silverpeas.gallery.ImageHelper.java
private static void createWatermark(final OutputStream watermarkedTargetStream, final String watermarkLabel, final BufferedImage image, final int percentSizeWatermark) throws IOException { final int imageWidth = image.getWidth(); final int imageHeight = image.getHeight(); // cration du buffer a la mme taille final BufferedImage outputBuf = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); final double max = Math.max(imageWidth, imageHeight); // recherche de la taille du watermark en fonction de la taille de la photo int size = 8; if (max < 600) { size = 8;/*from ww w .j a va 2 s .c om*/ } if (max >= 600 && max < 750) { size = 10; } if (max >= 750 && max < 1000) { size = 12; } if (max >= 1000 && max < 1250) { size = 14; } if (max >= 1250 && max < 1500) { size = 16; } if (max >= 1500 && max < 1750) { size = 18; } if (max >= 1750 && max < 2000) { size = 20; } if (max >= 2000 && max < 2250) { size = 22; } if (max >= 2250 && max < 2500) { size = 24; } if (max >= 2500 && max < 2750) { size = 26; } if (max >= 2750 && max < 3000) { size = 28; } if (max >= 3000) { size = (int) Math.rint(max * percentSizeWatermark / 100); } final Watermarker watermarker = new Watermarker(imageWidth, imageHeight); watermarker.addWatermark(image, outputBuf, new Font("Arial", Font.BOLD, size), watermarkLabel, size); ImageIO.write(outputBuf, "JPEG", watermarkedTargetStream); }
From source file:forge.ImageCache.java
private static BufferedImage scaleImage(String key, final int width, final int height, boolean useDefaultImage) { if (StringUtils.isEmpty(key) || (3 > width && -1 != width) || (3 > height && -1 != height)) { // picture too small or key not defined; return a blank return null; }/* w w w . ja v a 2 s. c om*/ String resizedKey = String.format("%s#%dx%d", key, width, height); final BufferedImage cached = _CACHE.getIfPresent(resizedKey); if (null != cached) { //System.out.println("found cached image: " + resizedKey); return cached; } BufferedImage original = getOriginalImage(key, useDefaultImage); if (original == null) { return null; } // Calculate the scale required to best fit the image into the requested // (width x height) dimensions whilst retaining aspect ratio. double scaleX = (-1 == width ? 1 : (double) width / original.getWidth()); double scaleY = (-1 == height ? 1 : (double) height / original.getHeight()); double bestFitScale = Math.min(scaleX, scaleY); if ((bestFitScale > 1) && !FModel.getPreferences().getPrefBoolean(FPref.UI_SCALE_LARGER)) { bestFitScale = 1; } BufferedImage result; if (1 == bestFitScale) { result = original; } else { int destWidth = (int) (original.getWidth() * bestFitScale); int destHeight = (int) (original.getHeight() * bestFitScale); ResampleOp resampler = new ResampleOp(destWidth, destHeight); result = resampler.filter(original, null); } //System.out.println("caching image: " + resizedKey); _CACHE.put(resizedKey, result); return result; }
From source file:com.sketchy.image.ImageProcessingThread.java
public static BufferedImage rotateImage(BufferedImage image, RotateOption rotateOption) { if (rotateOption == RotateOption.ROTATE_NONE) return image; int degrees = 0; int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); BufferedImage rotatedImage = null; if (rotateOption == RotateOption.ROTATE_90) { degrees = 90;/*from www. j a v a 2s . c om*/ rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB); } else if (rotateOption == RotateOption.ROTATE_180) { degrees = 180; rotatedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB); } else if (rotateOption == RotateOption.ROTATE_270) { degrees = 270; rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB); } Graphics2D g = rotatedImage.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.rotate(Math.toRadians(degrees), 0, 0); if (degrees == 90) { g.drawImage(image, null, 0, -imageHeight); } else if (degrees == 180) { g.drawImage(image, null, -imageWidth, -imageHeight); } else if (degrees == 270) { g.drawImage(image, null, -imageWidth, 0); } g.dispose(); return rotatedImage; }