List of usage examples for java.awt.image BufferedImage getHeight
public int getHeight()
From source file:com.alvermont.terraj.stargen.ui.UIUtils.java
/** * Get a JLabel object to display with star details * /* w ww.ja va2 s. c o m*/ * @throws java.io.IOException If there is an error building the list * @return A <code>JLabel</code> representing the star */ public static JLabel getSunLabel() throws IOException { BufferedImage bi = UIUtils.getImage("Sun"); bi = UIUtils.scaleImage(bi, 30, 120); ImageIcon icon = new ImageIcon(bi); JLabel label = new JLabel(icon); label.setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight())); label.setMinimumSize(new Dimension(bi.getWidth(), bi.getHeight())); label.setToolTipText("The Star"); return label; }
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;//from w w w . ja va2s.c om 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: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.j a v a 2 s. c o m 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.flexive.shared.media.impl.FxMediaNativeEngine.java
/** * Flip the image horizontal/*from ww w.j av a2 s . com*/ * * @param bufferedImage original image * @return horizontally flipped image */ private static BufferedImage flipVertical(BufferedImage bufferedImage) { AffineTransform at = AffineTransform.getTranslateInstance(0, bufferedImage.getHeight()); at.scale(1.0, -1.0); BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType()); return biOp.filter(bufferedImage, imgRes); }
From source file:com.reydentx.core.common.PhotoUtils.java
public static byte[] resizeImageScaleCrop(InputStream data, int img_width, int img_height, boolean isPNG) { BufferedImage originalImage; try {//from www .j av a 2 s .c o m originalImage = ImageIO.read(data); Dimension origDimentsion = new Dimension(originalImage.getWidth(), originalImage.getHeight()); Dimension fitDimentsion = new Dimension(img_width, img_height); int flag = testThresholdMinWidthHeightImage(origDimentsion, fitDimentsion); // size Anh dang: MinW < W < 720, MinH < H < 720. if (flag == -1) { data.reset(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); byte[] read = new byte[2048]; int i = 0; while ((i = data.read(read)) > 0) { byteArray.write(read, 0, i); } data.close(); return byteArray.toByteArray(); } else if (flag == 0) { // size Anh dang: MinW < W < 720 < H || MinH < H < 720 < W double ratioWidth = (origDimentsion.width * 1.0) / fitDimentsion.width; double ratioHeight = (origDimentsion.height * 1.0) / fitDimentsion.height; if (ratioWidth < ratioHeight) { // fit width, crop height image. int yH = 0; if (origDimentsion.height > fitDimentsion.height) { yH = (origDimentsion.height - fitDimentsion.height) / 2; } return cropBufferedImage(isPNG, originalImage, 0, yH, origDimentsion.width, fitDimentsion.height); } else { // fit height, crop width image. int xW = 0; if (origDimentsion.width > fitDimentsion.width) { xW = (origDimentsion.width - fitDimentsion.width) / 2; } return cropBufferedImage(isPNG, originalImage, xW, 0, fitDimentsion.width, origDimentsion.height); } } else { // size Anh dang: 720 < W,H. // Scale Image. double ratioWidth = (origDimentsion.width * 1.0) / fitDimentsion.width; double ratioHeight = (origDimentsion.height * 1.0) / fitDimentsion.height; if (ratioWidth < ratioHeight) { // fit width, crop height image. int new_width = fitDimentsion.width; int new_height = (int) (origDimentsion.height / ratioWidth); int yH = 0; if (new_height > fitDimentsion.height) { yH = (new_height - fitDimentsion.height) / 2; } byte[] scaleByteImage = scaleBufferedImage(isPNG, originalImage, 0, 0, new_width, new_height); if (scaleByteImage != null && scaleByteImage.length > 0) { InputStream isImg = new ByteArrayInputStream(scaleByteImage); BufferedImage scaleBufferedImage = ImageIO.read(isImg); // Crop width image. return cropBufferedImage(isPNG, scaleBufferedImage, 0, yH, fitDimentsion.width, fitDimentsion.height); } } else { // fit height, crop width image. int new_width = (int) (origDimentsion.width / ratioHeight); int new_height = fitDimentsion.height; int xW = 0; if (new_width > fitDimentsion.width) { xW = (new_width - fitDimentsion.width) / 2; } byte[] scaleByteImage = scaleBufferedImage(isPNG, originalImage, 0, 0, new_width, new_height); if (scaleByteImage != null && scaleByteImage.length > 0) { InputStream isImg = new ByteArrayInputStream(scaleByteImage); BufferedImage scaleBufferedImage = ImageIO.read(isImg); // Crop height image. return cropBufferedImage(isPNG, scaleBufferedImage, xW, 0, fitDimentsion.width, fitDimentsion.height); } } } } catch (Exception ex) { } return null; }
From source file:editeurpanovisu.ReadWriteImage.java
public static void writeTiff(Image imgImage, String strNomFich, boolean bSharpen, float sharpenLevel) throws ImageReadException, IOException { File file = new File(strNomFich); BufferedImage imageRGBSharpen = null; BufferedImage imageRGB = SwingFXUtils.fromFXImage(imgImage, null); Graphics2D graphics = imageRGB.createGraphics(); graphics.drawImage(imageRGB, 0, 0, null); if (bSharpen) { imageRGBSharpen = new BufferedImage(imageRGB.getWidth(), imageRGB.getHeight(), BufferedImage.TYPE_INT_RGB); Kernel kernel = new Kernel(3, 3, sharpenMatrix); ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); cop.filter(imageRGB, imageRGBSharpen); }//from w w w . j a va 2s. c om final ImageFormat format = ImageFormats.TIFF; final Map<String, Object> params = new HashMap<>(); params.put(ImagingConstants.PARAM_KEY_COMPRESSION, new Integer(TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED)); if (bSharpen) { try { Imaging.writeImage(imageRGBSharpen, file, format, params); } catch (ImageWriteException ex) { Logger.getLogger(ReadWriteImage.class.getName()).log(Level.SEVERE, null, ex); } } else { try { Imaging.writeImage(imageRGB, file, format, params); } catch (ImageWriteException ex) { Logger.getLogger(ReadWriteImage.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
private static BufferedImage resizeBorder(final BufferedImage border, int targetWidth, int targetHeight) throws IOException { if (targetWidth > border.getWidth() || targetHeight > border.getHeight()) { BufferedImage endImage = rescaleBorder(border, targetWidth, targetHeight); enforceBorderColors(endImage);//from ww w . j a va2 s.c o m return endImage; } int w = border.getWidth(); int h = border.getHeight(); int[] data = border.getRGB(0, 0, w, h, null, 0, w); int[] newData = new int[targetWidth * targetHeight]; float widthRatio = (float) Math.max(targetWidth - 1, 1) / (float) Math.max(w - 1, 1); float heightRatio = (float) Math.max(targetHeight - 1, 1) / (float) Math.max(h - 1, 1); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if ((0xff000000 & data[y * w + x]) != 0) { int newX = Math.min(Math.round(x * widthRatio), targetWidth - 1); int newY = Math.min(Math.round(y * heightRatio), targetHeight - 1); newData[newY * targetWidth + newX] = data[y * w + x]; } } } BufferedImage img = UIUtil.createImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB); img.setRGB(0, 0, targetWidth, targetHeight, newData, 0, targetWidth); return img; }
From source file:net.rptools.lib.image.ImageUtil.java
public static BufferedImage rgbToGrayscale(BufferedImage image) { if (image == null) { return null; }/* ww w . jav a 2 s . co m*/ BufferedImage returnImage = new BufferedImage(image.getWidth(), image.getHeight(), pickBestTransparency(image)); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { int encodedPixel = image.getRGB(x, y); int alpha = (encodedPixel >> 24) & 0xff; int red = (encodedPixel >> 16) & 0xff; int green = (encodedPixel >> 8) & 0xff; int blue = (encodedPixel) & 0xff; int average = (int) ((red + blue + green) / 3.0); // y = 0.3R + 0.59G + 0.11B luminance formula int value = (alpha << 24) + (average << 16) + (average << 8) + average; returnImage.setRGB(x, y, value); } } return returnImage; }
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 *//*w w w . j a v 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);//from w ww.j av a 2 s. com 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; }