List of usage examples for java.awt RenderingHints VALUE_INTERPOLATION_BICUBIC
Object VALUE_INTERPOLATION_BICUBIC
To view the source code for java.awt RenderingHints VALUE_INTERPOLATION_BICUBIC.
Click Source Link
From source file:br.com.diegosilva.jsfcomponents.util.Utils.java
public static byte[] resizeImage(byte[] imageData, String contentType, int width) { ImageIcon icon = new ImageIcon(imageData); double ratio = (double) width / icon.getIconWidth(); int resizedHeight = (int) (icon.getIconHeight() * ratio); int imageType = "image/png".equals(contentType) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; BufferedImage bImg = new BufferedImage(width, resizedHeight, imageType); Graphics2D g2d = bImg.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.drawImage(icon.getImage(), 0, 0, width, resizedHeight, null); g2d.dispose();/* w ww.j ava 2 s . c o m*/ String formatName = ""; if ("image/png".equals(contentType)) formatName = "png"; else if ("image/jpeg".equals(contentType) || "image/jpg".equals(contentType)) formatName = "jpeg"; ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { ImageIO.write(bImg, formatName, baos); return baos.toByteArray(); } catch (IOException ex) { throw new RuntimeException(ex); } }
From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java
public static BufferedImage scale(BufferedImage bi, int width, int height) { BufferedImage bi2;//w w w . ja v a2 s. c o m int scaleWidth = bi.getWidth(null); int scaleHeight = bi.getHeight(null); double scaleX = (double) width / scaleWidth; double scaleY = (double) height / scaleHeight; double scale = Math.min(scaleX, scaleY); scaleWidth = (int) ((double) scaleWidth * scale); scaleHeight = (int) ((double) scaleHeight * scale); Image scaledImage; if (HEADLESS) { // create a new buffered image, don't rely on a local graphics system (headless mode) final int type; if (bi.getType() != BufferedImage.TYPE_CUSTOM) { type = bi.getType(); } else if (bi.getAlphaRaster() != null) { // alpha channel available type = BufferedImage.TYPE_INT_ARGB; } else { type = BufferedImage.TYPE_INT_RGB; } bi2 = new BufferedImage(scaleWidth, scaleHeight, type); } else { GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration(); bi2 = gc.createCompatibleImage(scaleWidth, scaleHeight, bi.getTransparency()); } Graphics2D g = bi2.createGraphics(); if (scale < 0.3 && Math.max(scaleWidth, scaleHeight) < 500) { scaledImage = bi.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH); new ImageIcon(scaledImage).getImage(); g.drawImage(scaledImage, 0, 0, scaleWidth, scaleHeight, null); } else { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.drawImage(bi, 0, 0, scaleWidth, scaleHeight, null); } g.dispose(); return bi2; }
From source file:ddf.catalog.transformer.input.pptx.PptxInputTransformer.java
/** * If the slide show doesn't contain any slides, then return null. Otherwise, return jpeg * image data of the first slide in the deck. * * @param slideShow/*from ww w. j ava 2s.c om*/ * @return jpeg thumbnail or null if thumbnail can't be created * @throws IOException */ private byte[] generatePptxThumbnail(XMLSlideShow slideShow) throws IOException { if (slideShow.getSlides().isEmpty()) { LOGGER.info("the powerpoint file does not contain any slides, skipping thumbnail generation"); return null; } Dimension pgsize = slideShow.getPageSize(); int largestDimension = (int) Math.max(pgsize.getHeight(), pgsize.getWidth()); float scalingFactor = IMAGE_HEIGHTWIDTH / largestDimension; int scaledHeight = (int) (pgsize.getHeight() * scalingFactor); int scaledWidth = (int) (pgsize.getWidth() * scalingFactor); BufferedImage img = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); try { graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); graphics.scale(scalingFactor, scalingFactor); slideShow.getSlides().get(0).draw(graphics); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { ImageIOUtil.writeImage(img, FORMAT_NAME, outputStream, RESOLUTION_DPI, IMAGE_QUALITY); return outputStream.toByteArray(); } } catch (RuntimeException e) { if (e.getCause() instanceof javax.imageio.IIOException) { LOGGER.warn("unable to generate thumbnail for PPTX file", e); } else { throw e; } } finally { graphics.dispose(); } return null; }
From source file:PictureScaler.java
/** * Render all scaled versions 10 times, timing each version and * reporting the results below the appropriate scaled image. *//*from w w w . j a va2 s .co m*/ protected void paintComponent(Graphics g) { // Scale with NEAREST_NEIGHBOR int xLoc = PADDING, yLoc = PADDING; long startTime, endTime; float totalTime; int iterations = 10; ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); startTime = System.nanoTime(); for (int i = 0; i < iterations; ++i) { g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null); } endTime = System.nanoTime(); totalTime = (float) ((endTime - startTime) / 1000000) / iterations; g.drawString("NEAREST ", xLoc, yLoc + scaleH + PADDING); g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10); System.out.println("NEAREST: " + ((endTime - startTime) / 1000000)); // Scale with BILINEAR xLoc += scaleW + PADDING; ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); startTime = System.nanoTime(); for (int i = 0; i < iterations; ++i) { g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null); } endTime = System.nanoTime(); totalTime = (float) ((endTime - startTime) / 1000000) / iterations; g.drawString("BILINEAR", xLoc, yLoc + scaleH + PADDING); g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10); System.out.println("BILINEAR: " + ((endTime - startTime) / 1000000)); // Scale with BICUBIC xLoc += scaleW + PADDING; ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); startTime = System.nanoTime(); for (int i = 0; i < iterations; ++i) { g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null); } endTime = System.nanoTime(); totalTime = (float) ((endTime - startTime) / 1000000) / iterations; g.drawString("BICUBIC", xLoc, yLoc + scaleH + PADDING); g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10); System.out.println("BICUBIC: " + ((endTime - startTime) / 1000000)); // Scale with getScaledInstance xLoc += scaleW + PADDING; startTime = System.nanoTime(); for (int i = 0; i < iterations; ++i) { Image scaledPicture = picture.getScaledInstance(scaleW, scaleH, Image.SCALE_AREA_AVERAGING); g.drawImage(scaledPicture, xLoc, yLoc, null); } endTime = System.nanoTime(); totalTime = (float) ((endTime - startTime) / 1000000) / iterations; g.drawString("getScaled", xLoc, yLoc + scaleH + PADDING); g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10); System.out.println("getScaled: " + ((endTime - startTime) / 1000000)); // Scale with Progressive Bilinear xLoc += scaleW + PADDING; startTime = System.nanoTime(); for (int i = 0; i < iterations; ++i) { Image scaledPicture = getFasterScaledInstance(picture, scaleW, scaleH, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true); g.drawImage(scaledPicture, xLoc, yLoc, null); } endTime = System.nanoTime(); totalTime = (float) ((endTime - startTime) / 1000000) / iterations; g.drawString("Progressive", xLoc, yLoc + scaleH + PADDING); g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10); System.out.println("Progressive: " + ((endTime - startTime) / 1000000)); }
From source file:ch.entwine.weblounge.preview.jai.JAIPreviewGenerator.java
/** * Resizes the given image to what is defined by the image style and writes * the result to the output stream./* w w w . j ava 2 s.c o m*/ * * @param is * the input stream * @param os * the output stream * @param format * the image format * @param style * the style * @throws IllegalArgumentException * if the image is in an unsupported format * @throws IllegalArgumentException * if the input stream is empty * @throws IOException * if reading from or writing to the stream fails * @throws OutOfMemoryError * if the image is too large to be processed in memory */ private void style(InputStream is, OutputStream os, String format, ImageStyle style) throws IllegalArgumentException, IOException, OutOfMemoryError { // Does the input stream contain any data? if (is.available() == 0) throw new IllegalArgumentException("Empty input stream was passed to image styling"); // Do we need to do any work at all? if (style == null || ImageScalingMode.None.equals(style.getScalingMode())) { logger.trace("No scaling needed, performing a noop stream copy"); IOUtils.copy(is, os); return; } SeekableStream seekableInputStream = null; RenderedOp image = null; try { // Load the image from the given input stream seekableInputStream = new FileCacheSeekableStream(is); image = JAI.create("stream", seekableInputStream); if (image == null) throw new IOException("Error reading image from input stream"); // Get the original image size int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); // Resizing float scale = ImageStyleUtils.getScale(imageWidth, imageHeight, style); RenderingHints scaleHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); scaleHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); scaleHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); scaleHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); int scaledWidth = Math.round(scale * image.getWidth()); int scaledHeight = Math.round(scale * image.getHeight()); int cropX = 0; int cropY = 0; // If either one of scaledWidth or scaledHeight is < 1.0, then // the scale needs to be adapted to scale to 1.0 exactly and accomplish // the rest by cropping. if (scaledWidth < 1.0f) { scale = 1.0f / imageWidth; scaledWidth = 1; cropY = imageHeight - scaledHeight; scaledHeight = Math.round(imageHeight * scale); } else if (scaledHeight < 1.0f) { scale = 1.0f / imageHeight; scaledHeight = 1; cropX = imageWidth - scaledWidth; scaledWidth = Math.round(imageWidth * scale); } if (scale > 1.0) { ParameterBlock scaleParams = new ParameterBlock(); scaleParams.addSource(image); scaleParams.add(scale).add(scale).add(0.0f).add(0.0f); scaleParams.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC_2)); image = JAI.create("scale", scaleParams, scaleHints); } else if (scale < 1.0) { ParameterBlock subsampleAverageParams = new ParameterBlock(); subsampleAverageParams.addSource(image); subsampleAverageParams.add(Double.valueOf(scale)); subsampleAverageParams.add(Double.valueOf(scale)); image = JAI.create("subsampleaverage", subsampleAverageParams, scaleHints); } // Cropping cropX = (int) Math.max(cropX, (float) Math.ceil(ImageStyleUtils.getCropX(scaledWidth, scaledHeight, style))); cropY = (int) Math.max(cropY, (float) Math.ceil(ImageStyleUtils.getCropY(scaledWidth, scaledHeight, style))); if ((cropX > 0 && Math.floor(cropX / 2.0f) > 0) || (cropY > 0 && Math.floor(cropY / 2.0f) > 0)) { ParameterBlock cropTopLeftParams = new ParameterBlock(); cropTopLeftParams.addSource(image); cropTopLeftParams.add(cropX > 0 ? ((float) Math.floor(cropX / 2.0f)) : 0.0f); cropTopLeftParams.add(cropY > 0 ? ((float) Math.floor(cropY / 2.0f)) : 0.0f); cropTopLeftParams.add(scaledWidth - Math.max(cropX, 0.0f)); // width cropTopLeftParams.add(scaledHeight - Math.max(cropY, 0.0f)); // height RenderingHints croppingHints = new RenderingHints(JAI.KEY_BORDER_EXTENDER, BorderExtender.createInstance(BorderExtender.BORDER_COPY)); image = JAI.create("crop", cropTopLeftParams, croppingHints); } // Write resized/cropped image encoded as JPEG to the output stream ParameterBlock encodeParams = new ParameterBlock(); encodeParams.addSource(image); encodeParams.add(os); encodeParams.add("jpeg"); JAI.create("encode", encodeParams); } catch (Throwable t) { if (t.getClass().getName().contains("ImageFormat")) { throw new IllegalArgumentException(t.getMessage()); } } finally { IOUtils.closeQuietly(seekableInputStream); if (image != null) image.dispose(); } }
From source file:ScaleTest_2008.java
/** * Scale the image to several smaller sizes using each of the approaches * and time each series of operations. The times are output into the * application window for each row that they represent. *///from ww w . jav a2 s.c o m protected void paintComponent(Graphics g) { if (!originalImagePainted) { paintOriginalImage(); } long startTime, endTime, totalTime; int xLoc, yLoc; // Draw scaled versions with nearest neighbor xLoc = 5; yLoc = 20; startTime = System.nanoTime(); drawImage(g, yLoc, false); endTime = System.nanoTime(); totalTime = (endTime - startTime) / 1000000; g.drawString("NEAREST ", xLoc, yLoc + (FULL_SIZE / 2)); g.drawString(Long.toString(totalTime) + " ms", xLoc, yLoc + (FULL_SIZE / 2) + 15); System.out.println("NEAREST: " + (endTime - startTime) / 1000000); // BILINEAR yLoc += FULL_SIZE + PADDING; ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); startTime = System.nanoTime(); drawImage(g, yLoc, false); endTime = System.nanoTime(); totalTime = (endTime - startTime) / 1000000; g.drawString("BILINEAR ", xLoc, yLoc + (FULL_SIZE / 2)); g.drawString(Long.toString(totalTime) + " ms", xLoc, yLoc + (FULL_SIZE / 2) + 15); System.out.println("BILINEAR: " + (endTime - startTime) / 1000000); // BIDUBIC yLoc += FULL_SIZE + PADDING; ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); startTime = System.nanoTime(); drawImage(g, yLoc, false); endTime = System.nanoTime(); totalTime = (endTime - startTime) / 1000000; g.drawString("BICUBIC ", xLoc, yLoc + (FULL_SIZE / 2)); g.drawString(Long.toString(totalTime) + " ms", xLoc, yLoc + (FULL_SIZE / 2) + 15); System.out.println("BICUBIC: " + (endTime - startTime) / 1000000); // getScaledInstance() yLoc += FULL_SIZE + PADDING; ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); startTime = System.nanoTime(); drawImage(g, yLoc, true); endTime = System.nanoTime(); totalTime = (endTime - startTime) / 1000000; g.drawString("getScaled ", xLoc, yLoc + (FULL_SIZE / 2)); g.drawString(Long.toString(totalTime) + " ms", xLoc, yLoc + (FULL_SIZE / 2) + 15); System.out.println("getScaled: " + (endTime - startTime) / 1000000); // Progressive Bilinear yLoc += FULL_SIZE + PADDING; startTime = System.nanoTime(); drawBetterImage(g, yLoc); endTime = System.nanoTime(); totalTime = (endTime - startTime) / 1000000; g.drawString("Progressive ", xLoc, yLoc + (FULL_SIZE / 2)); g.drawString(Long.toString(totalTime) + " ms", xLoc, yLoc + (FULL_SIZE / 2) + 15); System.out.println("faster: " + (endTime - startTime) / 1000000); // Draw image sizes xLoc = 100; int delta = (int) (SCALE_FACTOR * FULL_SIZE); for (int scaledSize = FULL_SIZE; scaledSize > 0; scaledSize -= delta) { g.drawString(scaledSize + " x " + scaledSize, xLoc + Math.max(0, scaledSize / 2 - 20), 15); xLoc += scaledSize + 20; } }
From source file:de.inren.service.picture.PictureModificationServiceImpl.java
private BufferedImage scaleImage(File orginal, final int max) throws IOException { // Load the image. BufferedImage originalImage = ImageIO.read(orginal); // Figure out the new dimensions. final int w = originalImage.getWidth(); final int h = originalImage.getHeight(); final double maxOriginal = Math.max(w, h); final double scaling = max / maxOriginal; final int newW = (int) Math.round(scaling * w); final int newH = (int) Math.round(scaling * h); // If we need to scale down by more than 2x, scale to double the // eventual size and then scale again. This provides much higher // quality results. if (scaling < 0.5f) { final BufferedImage newImg = new BufferedImage(newW * 2, newH * 2, BufferedImage.TYPE_INT_RGB); final Graphics2D gfx = newImg.createGraphics(); gfx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); gfx.drawImage(originalImage, 0, 0, newW * 2, newH * 2, null); gfx.dispose();//from w w w.ja v a 2 s . com newImg.flush(); originalImage = newImg; } // Scale it. BufferedImage newImg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_RGB); final Graphics2D gfx = newImg.createGraphics(); gfx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); gfx.drawImage(originalImage, 0, 0, newW, newH, null); gfx.dispose(); newImg.flush(); originalImage.flush(); return newImg; }
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 {/*from ww w.j a va 2 s . c om*/ 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.silverpeas.thumbnail.control.ThumbnailController.java
protected static void createCropThumbnailFileOnServer(String pathOriginalFile, String pathCropdir, String pathCropFile, ThumbnailDetail thumbnail, int thumbnailWidth, int thumbnailHeight) { try {/* w w w . jav a2s . c om*/ // Creates folder if not exists File dir = new File(pathCropdir); if (!dir.exists()) { FileFolderManager.createFolder(pathCropdir); } // create empty file File cropFile = new File(pathCropFile); if (!cropFile.exists()) { cropFile.createNewFile(); } File originalFile = new File(pathOriginalFile); BufferedImage bufferOriginal = ImageIO.read(originalFile); // crop image BufferedImage cropPicture = bufferOriginal.getSubimage(thumbnail.getXStart(), thumbnail.getYStart(), thumbnail.getXLength(), thumbnail.getYLength()); BufferedImage cropPictureFinal = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB); // Redimensionnement de l'image Graphics2D g2 = cropPictureFinal.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(cropPicture, 0, 0, thumbnailWidth, thumbnailHeight, null); g2.dispose(); // save crop image String extension = FilenameUtils.getExtension(originalFile.getName()); ImageIO.write(cropPictureFinal, extension, cropFile); } catch (Exception e) { SilverTrace.warn("thumbnail", "ThumbnailController.createThumbnailFileOnServer()", "thumbnail_MSG_CREATE_CROP_FILE_KO", "originalFileName=" + thumbnail.getOriginalFileName() + " cropFileName = " + thumbnail.getCropFileName(), e); } }