List of usage examples for java.awt.image BufferedImage getHeight
public int getHeight()
From source file:com.ckfinder.connector.utils.ImageUtils.java
/** * create thumb file./* w ww . j av a 2 s . co m*/ * * @param orginFile orgin image file. * @param file file to save thumb * @param conf connector configuration * @throws IOException when error occurs. */ public static void createThumb(final File orginFile, final File file, final IConfiguration conf) throws IOException { BufferedImage image = ImageIO.read(orginFile); if (image != null) { Dimension dimension = createThumbDimension(image, conf.getMaxThumbWidth(), conf.getMaxThumbHeight()); FileUtils.createPath(file, conf, true); if (image.getHeight() == dimension.height && image.getWidth() == dimension.width) { writeUntouchedImage(orginFile, file); } else { resizeImage(image, dimension.width, dimension.height, conf.getThumbsQuality(), file); } } else { if (conf.isDebugMode()) { throw new IOException("Wrong image file"); } } }
From source file:ImageProcessing.ImageProcessing.java
public static void processConvolve(BufferedImage image, double[] filter) { //Applies convolution operation using passed filter to the image. //Initialize values int alphaValue, filteredRed, filteredGreen, filteredBlue; int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double[] temp_alpha = extractAlphaValue(image); double[] temp_red = extractRedColor(image); double[] temp_green = extractGreenColor(image); double[] temp_blue = extractBlueColor(image); //For every pixels (except top/bottom row & borderline left/right row, //Apply filter. for (int i = 1; i < imageHeight - 1; i++) { for (int j = 1; j < imageWidth - 1; j++) { alphaValue = (int) temp_alpha[(i * imageWidth) + j]; //Apply filter to every color component (RGB) filteredRed = processFiltering(j, i, imageWidth, imageHeight, temp_red, filter); filteredGreen = processFiltering(j, i, imageWidth, imageHeight, temp_green, filter); filteredBlue = processFiltering(j, i, imageWidth, imageHeight, temp_blue, filter); //Copy the processed color values to a RGB integer using bitwise operator. int filteredRGB = (alphaValue << 24) + (filteredRed << 16) + (filteredGreen << 8) + filteredBlue; //Set the RGB back to the exact same pixel position. image.setRGB(j, i, filteredRGB); }//w ww . j a va 2 s. com } }
From source file:alfio.manager.UploadedResourceManager.java
private static Map<String, String> getAttributes(UploadBase64FileModification file) { if (!StringUtils.startsWith(file.getType(), "image/")) { return file.getAttributes(); }/* w w w. j ava2 s .c o m*/ try { BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getFile())); Map<String, String> attributes = new HashMap<>(file.getAttributes()); attributes.put(ATTR_IMG_WIDTH, String.valueOf(image.getWidth())); attributes.put(ATTR_IMG_HEIGHT, String.valueOf(image.getHeight())); return attributes; } catch (IOException e) { log.error("error while processing image: ", e); return file.getAttributes(); } }
From source file:com.jaeksoft.searchlib.util.ImageUtils.java
public final static BufferedImage reduceImage(BufferedImage image, int width, int height) { int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = (BufferedImage) image; int w = image.getWidth(); int h = image.getHeight(); while (w != width || h != height) { if (w > width) { w /= 2;//from w w w . j a v a 2 s . co m if (w < width) w = width; } if (h > height) { h /= 2; if (h < height) h = height; } BufferedImage tmp = new BufferedImage(w, h, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } return ret; }
From source file:com.boundlessgeo.geoserver.api.controllers.ThumbnailController.java
/** * Utility method for scaling thumbnails. Scales byte[] image by a scale factor. * Optionally crops images to square./*from www.j a v a 2s . c o m*/ * @param src RenderedImage containing the input image * @param scale Scale amount * @param square Boolean flag to crop to a square image * @return RenderedImage containing the transformed image * @throws IOException */ public static BufferedImage scaleImage(BufferedImage image, double scale, boolean square) throws IOException { int sx = 0, sy = 0; int swidth = image.getWidth(); int sheight = image.getHeight(); if (square) { if (image.getHeight() > image.getWidth()) { sy = (int) ((image.getHeight() - image.getWidth()) / 2.0); sheight = swidth; } else if (image.getHeight() < image.getWidth()) { sx = (int) ((image.getWidth() - image.getHeight()) / 2.0); swidth = sheight; } } int width = (int) (swidth * scale); int height = (int) (sheight * scale); BufferedImage scaled = new BufferedImage(image.getColorModel(), image.getRaster().createCompatibleWritableRaster(width, height), image.isAlphaPremultiplied(), null); Graphics g = scaled.getGraphics(); g.drawImage(image, 0, 0, width, height, sx, sy, sx + swidth, sy + sheight, null); g.dispose(); return scaled; }
From source file:org.jamwiki.utils.ImageUtil.java
/** * Resize an image, using a maximum dimension value. Image dimensions will * be constrained so that the proportions are the same, but neither the width * or height exceeds the value specified. *//*from www . java2 s . co m*/ private static BufferedImage resizeImage(WikiImage wikiImage, int maxDimension) throws Exception { File imageFile = new File(Environment.getValue(Environment.PROP_FILE_DIR_FULL_PATH), wikiImage.getUrl()); BufferedImage original = ImageUtil.loadImage(imageFile); maxDimension = calculateImageIncrement(maxDimension); int increment = Environment.getIntValue(Environment.PROP_IMAGE_RESIZE_INCREMENT); if (increment <= 0 || (maxDimension > original.getWidth() && maxDimension > original.getHeight())) { // let the browser scale the image return original; } String newUrl = wikiImage.getUrl(); int pos = newUrl.lastIndexOf('.'); if (pos > -1) { newUrl = newUrl.substring(0, pos) + "-" + maxDimension + "px" + newUrl.substring(pos); } else { newUrl += "-" + maxDimension + "px"; } wikiImage.setUrl(newUrl); File newImageFile = new File(Environment.getValue(Environment.PROP_FILE_DIR_FULL_PATH), newUrl); if (newImageFile.exists()) { return ImageUtil.loadImage(newImageFile); } int width = -1; int height = -1; if (original.getWidth() >= original.getHeight()) { width = maxDimension; } else { height = maxDimension; } Image resized = null; try { resized = original.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING); } catch (Throwable t) { logger.severe( "Unable to resize image. This problem sometimes occurs due to dependencies between Java and X on UNIX systems. Consider enabling an X server or setting the java.awt.headless parameter to true for your JVM.", t); resized = original; } BufferedImage bufferedImage = null; if (resized instanceof BufferedImage) { bufferedImage = (BufferedImage) resized; } else { bufferedImage = ImageUtil.imageToBufferedImage(resized); } ImageUtil.saveImage(bufferedImage, newImageFile); return bufferedImage; }
From source file:Main.java
/** * Snapshots the specified JavaFX {@link Image} object and stores a * copy of its pixels into a {@link BufferedImage} object, creating * a new object if needed.// w w w . j a v a 2s . c om * The method will only convert a JavaFX {@code Image} that is readable * as per the conditions on the * {@link Image#getPixelReader() Image.getPixelReader()} * method. * If the {@code Image} is not readable, as determined by its * {@code getPixelReader()} method, then this method will return null. * If the {@code Image} is a writable, or other dynamic image, then * the {@code BufferedImage} will only be set to the current state of * the pixels in the image as determined by its {@link PixelReader}. * Further changes to the pixels of the {@code Image} will not be * reflected in the returned {@code BufferedImage}. * <p> * The optional {@code BufferedImage} parameter may be reused to store * the copy of the pixels. * A new {@code BufferedImage} will be created if the supplied object * is null, is too small or of a type which the image pixels cannot * be easily converted into. * * @param img the JavaFX {@code Image} to be converted * @param bimg an optional {@code BufferedImage} object that may be * used to store the returned pixel data * @return a {@code BufferedImage} containing a snapshot of the JavaFX * {@code Image}, or null if the {@code Image} is not readable. * @since JavaFX 2.2 */ public static BufferedImage fromFXImage(Image img, BufferedImage bimg) { PixelReader pr = img.getPixelReader(); if (pr == null) { return null; } int iw = (int) img.getWidth(); int ih = (int) img.getHeight(); int prefBimgType = getBestBufferedImageType(pr.getPixelFormat(), bimg); if (bimg != null) { int bw = bimg.getWidth(); int bh = bimg.getHeight(); if (bw < iw || bh < ih || bimg.getType() != prefBimgType) { bimg = null; } else if (iw < bw || ih < bh) { Graphics2D g2d = bimg.createGraphics(); g2d.setComposite(AlphaComposite.Clear); g2d.fillRect(0, 0, bw, bh); g2d.dispose(); } } if (bimg == null) { bimg = new BufferedImage(iw, ih, prefBimgType); } IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster(); int offset = icr.getDataOffset(0); int scan = icr.getScanlineStride(); int data[] = icr.getDataStorage(); WritablePixelFormat<IntBuffer> pf = getAssociatedPixelFormat(bimg); pr.getPixels(0, 0, iw, ih, pf, data, offset, scan); return bimg; }
From source file:com.lingxiang2014.util.ImageUtils.java
public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile);/*from w w w.j a v a2s .c o m*/ 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.t3.macro.api.functions.input.InputFunctions.java
/** * Gets icon from the asset manager. Code copied and modified from * EditTokestaticnDialog.java/*from ww w. ja v a 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:com.lingxiang2014.util.ImageUtils.java
public static void addWatermark(File srcFile, File destFile, File watermarkFile, int alpha) { Assert.notNull(srcFile);/* w ww .ja v a 2 s . co m*/ Assert.notNull(destFile); Assert.state(alpha >= 0); Assert.state(alpha <= 100); if (watermarkFile == null || !watermarkFile.exists()) { try { FileUtils.copyFile(srcFile, destFile); } catch (IOException e) { e.printStackTrace(); } return; } 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(); BufferedImage destBufferedImage = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB); graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, srcWidth, srcHeight); graphics2D.drawImage(srcBufferedImage, 0, 0, null); graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha / 100F)); BufferedImage watermarkBufferedImage = ImageIO.read(watermarkFile); int watermarkImageWidth = watermarkBufferedImage.getWidth(); int watermarkImageHeight = watermarkBufferedImage.getHeight(); int x = srcWidth - watermarkImageWidth; int y = srcHeight - watermarkImageHeight; graphics2D.drawImage(watermarkBufferedImage, x, y, watermarkImageWidth, watermarkImageHeight, 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(DEST_QUALITY / 100F); 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.dissolve(alpha); operation.quality((double) DEST_QUALITY); operation.addImage(watermarkFile.getPath()); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { CompositeCmd compositeCmd = new CompositeCmd(true); if (graphicsMagickPath != null) { compositeCmd.setSearchPath(graphicsMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { CompositeCmd compositeCmd = new CompositeCmd(false); if (imageMagickPath != null) { compositeCmd.setSearchPath(imageMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }