List of usage examples for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR
Object VALUE_INTERPOLATION_BILINEAR
To view the source code for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR.
Click Source Link
From source file:com.partagames.imageresizetool.SimpleImageResizeTool.java
/** * Scales an image to the desired dimensions. * * @param img Original image/*from w ww. ja va 2 s .co m*/ * @param newW Target width * @param newH Target height * @return Scaled image */ public static BufferedImage scale(BufferedImage img, int newW, int newH) { int w = img.getWidth(); int h = img.getHeight(); final BufferedImage dimg = new BufferedImage(newW, newH, img.getType()); final Graphics2D g = dimg.createGraphics(); // use provided rendering hint, default is bilinear switch (scalingHint) { case "n": g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); break; case "b": g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); break; } g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null); g.dispose(); return dimg; }
From source file:IconDemoApp.java
/** * Resizes an image using a Graphics2D object backed by a BufferedImage. * /*from w w w. j av a2s .c o m*/ * @param srcImg - * source image to scale * @param w - * desired width * @param h - * desired height * @return - the new resized image */ private Image getScaledImage(Image srcImg, int w, int h) { BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = resizedImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(srcImg, 0, 0, w, h, null); g2.dispose(); return resizedImg; }
From source file:dcstore.web.ImagesWeb.java
private void resizeImage(String inPath, int w, int h, String outPath) { try {/*from w w w .jav a2 s . c o m*/ BufferedImage originalImage = ImageIO.read(new File(inPath)); int ow, oh; ow = originalImage.getWidth(); oh = originalImage.getHeight(); double ratio = (double) ow / (double) oh; int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); int ch = (int) Math.round(w / ratio); BufferedImage resizedImage = new BufferedImage(w, h, type); Graphics2D g = resizedImage.createGraphics(); 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); g.setColor(Color.white); g.fillRect(0, 0, w, h); g.drawImage(originalImage, 0, (int) (((float) h - (float) ch) / 2), w, ch, null); g.dispose(); ImageIO.write(resizedImage, "jpg", new File(outPath)); } catch (Exception e) { FacesContext.getCurrentInstance().addMessage("", new FacesMessage("Error while resizeing image: " + e.getMessage())); } }
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 w w w .j a va2 s .com*/ 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:com.kahlon.guard.controller.PersonImageManager.java
private BufferedImage resizeImageWithHint(BufferedImage originalImage, int type) { BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); g.dispose();/*ww w . j a v a 2 s . c o m*/ 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); return resizedImage; }
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 ww w . j a v a 2 s . c o m 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:edu.ku.brc.ui.GraphicsUtils.java
/** * @param orig the original image/* w w w . j ava 2 s . c o m*/ * @param maxHeight the max height of the scaled image * @param maxWidth the max width of the scaled image * @param preserveAspectRatio if true, the scaling preserves the aspect ratio of the original image * @param doHighQuality do higher quality thumbnail (slow) * @return the byte array of the scaled image * @throws IOException an error occurred while encoding the result as a JPEG image */ public static byte[] scaleImage(final BufferedImage orig, final int maxHeight, final int maxWidth, final boolean preserveAspectRatio, boolean doHighQuality) throws IOException { BufferedImage scaled; if (true) { int targetW = maxWidth; int targetH = maxHeight; if (preserveAspectRatio) { int origWidth = orig.getWidth(); int origHeight = orig.getHeight(); double origRatio = (double) origWidth / (double) origHeight; double scaledRatio = (double) maxWidth / (double) maxHeight; if (origRatio > scaledRatio) { targetH = (int) (targetW / origRatio); } else { targetW = (int) (targetH * origRatio); } } scaled = getScaledInstance(orig, targetW, targetH, doHighQuality); } else { scaled = generateScaledImage(orig, RenderingHints.VALUE_INTERPOLATION_BILINEAR, Math.max(maxHeight, maxWidth)); } ByteArrayOutputStream output = new ByteArrayOutputStream(8192); ImageIO.write(scaled, "jpeg", output); byte[] outputBytes = output.toByteArray(); output.close(); return outputBytes; }
From source file:gg.msn.ui.panel.MainPanel.java
@Override public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; super.paintComponent(g2d); g2d.setRenderingHints(//ww w. ja va 2 s . c o m new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR)); try { ImageIcon icon = ThemeManager.getTheme().get(ThemeManager.MAIN_BACKGROUND); if (icon != null) { Image image = icon.getImage(); g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this); } else { log.warn("image " + icon.getDescription() + " not Found"); } icon = ThemeManager.getTheme().get(ThemeManager.MAIN_IMAGE); if (icon != null) { Image cartel = icon.getImage(); g2d.drawImage(cartel, 0 - cartel.getWidth(this) / 10, getHeight() - cartel.getHeight(this), this); } else { log.warn("image " + icon.getDescription() + " not Found"); } } catch (Exception e) { // log.error(e); } }
From source file:ch.rasc.downloadchart.DownloadChartServlet.java
private static void writeImage(HttpServletResponse response, byte[] imageData, Integer width, Integer height, String formatName) throws IOException { BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData)); Dimension newDimension = calculateDimension(originalImage, width, height); if (newDimension != null) { int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); BufferedImage resizedImage = new BufferedImage(newDimension.width, newDimension.height, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, newDimension.width, newDimension.height, null); g.dispose();/*from www. j a v a 2 s . c o m*/ 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); ImageIO.write(resizedImage, formatName, response.getOutputStream()); } else { if ("png".equals(formatName)) { response.getOutputStream().write(imageData); } else { ImageIO.write(originalImage, "gif", response.getOutputStream()); } } }
From source file:org.hydroponics.dao.JDBCHydroponicsDaoImpl.java
public byte[] getThumbnail(BufferedImage buffImage) throws IOException { BufferedImage pDestImage = new BufferedImage(Constants.THUMBNAIL_WIDTH, Constants.THUMBNAIL_HEIGHT, BufferedImage.TYPE_3BYTE_BGR); AffineTransform transform = new AffineTransform(); transform.scale((float) Constants.THUMBNAIL_WIDTH / (float) buffImage.getWidth(), (float) Constants.THUMBNAIL_HEIGHT / (float) buffImage.getHeight()); Graphics2D g = (Graphics2D) pDestImage.getGraphics(); //set the rendering hints for a good thumbnail image Map m = g.getRenderingHints(); m.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); m.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); m.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); m.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHints(m);/*from w w w. j ava2 s. co m*/ g.drawImage(buffImage, transform, null); g.dispose(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(pDestImage, "JPEG", out); return out.toByteArray(); }