List of usage examples for java.awt.image BufferedImage getWidth
public int getWidth()
From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
private static BufferedImage resizeNormalImage(BufferedImage image, ImageInformation information) throws IOException { int originalWidth = image.getWidth(); int originalHeight = image.getHeight(); if (originalHeight == information.getTargetHeight() && originalWidth == information.getTargetWidth() && MathUtils.floatEquals(information.getFactor(), 1f)) { return image; }/*from w w w. j a v a2 s. c om*/ int newWidth = information.getTargetWidth(); int newHeight = information.getTargetHeight(); if (newWidth <= 0 || newHeight <= 0) { newWidth = originalWidth; newHeight = originalHeight; } if (information.getFactor() >= 0) { newWidth = (int) (newWidth * information.getFactor()); newHeight = (int) (newHeight * information.getFactor()); } return resizeNormalImage(image, newWidth, newHeight, information); }
From source file:com.t3.macro.api.functions.input.InputFunctions.java
/** * Gets icon from the asset manager. Code copied and modified from * EditTokestaticnDialog.java/*from w w w. j a va 2 s .c o m*/ */ static ImageIcon getIcon(String id, int size, ImageObserver io) { // Extract the MD5Key from the URL if (id == null) return null; MD5Key assetID = new MD5Key(id); // Get the base image && find the new size for the icon BufferedImage assetImage = ImageManager.getImage(assetID, io); // Resize if (assetImage.getWidth() > size || assetImage.getHeight() > size) { Dimension dim = new Dimension(assetImage.getWidth(), assetImage.getWidth()); if (dim.height < dim.width) { dim.height = (int) ((dim.height / (double) dim.width) * size); dim.width = size; } else { dim.width = (int) ((dim.width / (double) dim.height) * size); dim.height = size; } BufferedImage image = new BufferedImage(dim.width, dim.height, Transparency.BITMASK); Graphics2D g = image.createGraphics(); g.drawImage(assetImage, 0, 0, dim.width, dim.height, null); assetImage = image; } return new ImageIcon(assetImage); }
From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
public static BufferedImage resizeNinePatchImage(Project project, ImageInformation information) throws IOException { BufferedImage image = ImageIO.read(information.getImageFile()); int originalWidth = image.getWidth(); int originalHeight = image.getHeight(); if (originalWidth - 2 == information.getTargetWidth() && originalHeight - 2 == information.getTargetHeight() && MathUtils.floatEquals(information.getFactor(), 1f)) { return image; }//w w w. ja v a 2 s.c om int newWidth = information.getTargetWidth(); int newHeight = information.getTargetHeight(); if (newWidth <= 0 || newHeight <= 0) { newWidth = originalWidth; newHeight = originalHeight; } if (information.getFactor() >= 0) { newWidth = (int) (newWidth * information.getFactor()); newHeight = (int) (newHeight * information.getFactor()); } BufferedImage trimmedImage = trim9PBorder(image); ImageInformation trimmedImageInformation = ImageInformation.newBuilder(information) .setExportName(getExportName("trimmed", information.getExportName())).build(project); saveImageTempFile(trimmedImage, trimmedImageInformation); trimmedImage = resizeNormalImage(trimmedImage, newWidth, newHeight, trimmedImageInformation); saveImageTempFile(trimmedImage, ImageInformation.newBuilder(trimmedImageInformation) .setExportName(getExportName("trimmedResized", information.getExportName())).build(project)); BufferedImage borderImage; int w = trimmedImage.getWidth(); int h = trimmedImage.getHeight(); try { borderImage = generateBordersImage(image, w, h); } catch (Exception e) { return null; } int[] rgbArray = new int[w * h]; trimmedImage.getRGB(0, 0, w, h, rgbArray, 0, w); borderImage.setRGB(1, 1, w, h, rgbArray, 0, w); return borderImage; }
From source file:com.vaadin.testbench.screenshot.ImageUtil.java
/** * Resize images to be same size. The size is determined by the minimum * height and minimum width of the images. * /*from w w w.j a va 2s .co m*/ * @param image1 * an image. * @param image2 * an image. * @return a list containing two images with the same dimensions */ public static List<BufferedImage> cropToBeSameSize(BufferedImage image1, BufferedImage image2) { if (imagesSameSize(image1, image2)) { return Arrays.asList(image1, image2); } int minHeight = Math.min(image1.getHeight(), image2.getHeight()); int minWidth = Math.min(image1.getWidth(), image2.getWidth()); BufferedImage cropped1 = cropImage(image1, minWidth, minHeight); BufferedImage cropped2 = cropImage(image2, minWidth, minHeight); return Arrays.asList(cropped1, cropped2); }
From source file:ch.rasc.downloadchart.DownloadChartServlet.java
private static void handleJpg(HttpServletResponse response, byte[] imageData, Integer width, Integer height, String filename, JpegOptions options) throws IOException { response.setContentType("image/jpeg"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + ".jpg\";"); BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData)); Dimension newDimension = calculateDimension(image, width, height); int imgWidth = image.getWidth(); int imgHeight = image.getHeight(); if (newDimension != null) { imgWidth = newDimension.width;/* w w w. j av a 2 s . c o m*/ imgHeight = newDimension.height; } BufferedImage newImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = newImage.createGraphics(); g.drawImage(image, 0, 0, imgWidth, imgHeight, Color.BLACK, null); g.dispose(); if (newDimension != null) { 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); } try (ImageOutputStream ios = ImageIO.createImageOutputStream(response.getOutputStream())) { Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg"); ImageWriter writer = iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); if (options != null && options.quality != null && options.quality != 0 && options.quality != 100) { iwp.setCompressionQuality(options.quality / 100f); } else { iwp.setCompressionQuality(1); } writer.setOutput(ios); writer.write(null, new IIOImage(newImage, null, null), iwp); writer.dispose(); } }
From source file:com.sun.socialsite.util.ImageUtil.java
public static BufferedImage getScaledImage(BufferedImage origImage, Integer desiredWidth, Integer desiredHeight) throws IOException { if (origImage == null) { return null; }/*ww w .ja va 2 s . co m*/ if (desiredWidth == null) { desiredWidth = origImage.getWidth(); } if (desiredHeight == null) { desiredHeight = origImage.getHeight(); } int origWidth = origImage.getWidth(); int origHeight = origImage.getHeight(); double ratio = Math.min((((double) desiredWidth) / origWidth), (((double) desiredHeight) / origHeight)); int extraWidth = desiredWidth - ((int) (origWidth * ratio)); int extraHeight = desiredHeight - ((int) (origHeight * ratio)); int tmpWidth = (desiredWidth - extraWidth); int tmpHeight = (desiredHeight - extraHeight); BufferedImage tmpImage = getScaledInstance(origImage, tmpWidth, tmpHeight, RenderingHints.VALUE_INTERPOLATION_BICUBIC, true); log.debug(String.format("tmpImage[width=%d height=%d", tmpImage.getWidth(), tmpImage.getHeight())); if ((tmpImage.getWidth() == desiredWidth) && (tmpImage.getHeight() == desiredHeight)) { return tmpImage; } else { BufferedImage scaledImage = new BufferedImage(desiredWidth, desiredHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = scaledImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); // We recalculate these in case scaling didn't quite hit its targets extraWidth = desiredWidth - tmpImage.getWidth(); extraHeight = desiredHeight - tmpImage.getHeight(); int dx1 = extraWidth / 2; int dy1 = extraHeight / 2; int dx2 = desiredWidth - dx1; int dy2 = desiredWidth - dy1; // transparent background g2d.setColor(new Color(0, 0, 0, 0)); g2d.fillRect(0, 0, desiredWidth, desiredHeight); g2d.drawImage(tmpImage, dx1, dy1, dx2, dy2, null); return scaledImage; } }
From source file:com.reydentx.core.common.PhotoUtils.java
public static byte[] converImagePNGtoJPEG(InputStream data) { byte[] imageFinal = null; try {//from w ww. j a v a 2 s . c o m ByteArrayOutputStream outstream = new ByteArrayOutputStream(); BufferedImage bufferedImage = ImageIO.read(data); // create a blank, RGB, same width and height, and a white background BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB); newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null); ImageIO.write(newBufferedImage, "JPEG", outstream); imageFinal = outstream.toByteArray(); outstream.close(); } catch (Exception ex) { } return imageFinal; }
From source file:com.fluidops.iwb.deepzoom.DZConvert.java
/** * Process the given image file, producing its Deep Zoom output files * in a subdirectory of the given output directory. * @param inFile the file containing the image * @param outputDir the output directory *//* ww w.jav a2s . c o m*/ private static void processImageFile(File inFile, File outputDir) throws IOException { if (verboseMode) System.out.printf("Processing image file: %s%n", inFile); String fileName = inFile.getName(); String nameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.')); String pathWithoutExtension = outputDir + File.separator + nameWithoutExtension; BufferedImage image = loadImage(inFile); int originalWidth = image.getWidth(); int originalHeight = image.getHeight(); double maxDim = Math.max(originalWidth, originalHeight); int nLevels = (int) Math.ceil(Math.log(maxDim) / Math.log(2)); if (debugMode) System.out.printf("nLevels=%d%n", nLevels); // Delete any existing output files and folders for this image File descriptor = new File(pathWithoutExtension + ".xml"); if (descriptor.exists()) { if (deleteExisting) deleteFile(descriptor); else throw new IOException("File already exists in output dir: " + descriptor); } File imgDir = new File(pathWithoutExtension); if (imgDir.exists()) { if (deleteExisting) { if (debugMode) System.out.printf("Deleting directory: %s%n", imgDir); deleteDir(imgDir); } else throw new IOException("Image directory already exists in output dir: " + imgDir); } imgDir = createDir(outputDir, nameWithoutExtension + "_files"); double width = originalWidth; double height = originalHeight; for (int level = nLevels; level >= 0; level--) { int nCols = (int) Math.ceil(width / tileSize); int nRows = (int) Math.ceil(height / tileSize); if (debugMode) System.out.printf("level=%d w/h=%f/%f cols/rows=%d/%d%n", level, width, height, nCols, nRows); File dir = createDir(imgDir, Integer.toString(level)); for (int col = 0; col < nCols; col++) { for (int row = 0; row < nRows; row++) { BufferedImage tile = getTile(image, row, col); saveImage(tile, dir + File.separator + col + '_' + row); } } // Scale down image for next level width = Math.ceil(width / 2); height = Math.ceil(height / 2); if (width > 10 && height > 10) { // resize in stages to improve quality image = resizeImage(image, width * 1.66, height * 1.66); image = resizeImage(image, width * 1.33, height * 1.33); } image = resizeImage(image, width, height); } saveImageDescriptor(originalWidth, originalHeight, descriptor); }
From source file:net.mindengine.galen.utils.GalenUtils.java
public static File makeFullScreenshot(WebDriver driver) throws IOException, InterruptedException { // scroll up first scrollVerticallyTo(driver, 0);// w w w . j a v a2 s. co 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()) { resultingImage = GalenUtils.resizeScreenshotIfNeeded(driver, resultingImage); } ImageIO.write(resultingImage, "png", file); return file; }
From source file:com.smash.revolance.ui.model.helper.ImageHelper.java
public static BufferedImage cropImage(final BufferedImage img, int x, int y, int w, int h, double xScale, double yScale) { x = (int) (x * xScale); w = (int) (w * xScale); y = (int) (y * yScale); h = (int) (h * yScale); if (img.getHeight() < (y + h)) { h = img.getHeight() - y;/*from www. jav a 2 s .c o m*/ } if (img.getWidth() < (x + w)) { w = img.getWidth() - x; } return img.getSubimage(x, y, w, h); }