Example usage for java.awt Image SCALE_SMOOTH

List of usage examples for java.awt Image SCALE_SMOOTH

Introduction

In this page you can find the example usage for java.awt Image SCALE_SMOOTH.

Prototype

int SCALE_SMOOTH

To view the source code for java.awt Image SCALE_SMOOTH.

Click Source Link

Document

Choose an image-scaling algorithm that gives higher priority to image smoothness than scaling speed.

Usage

From source file:com.mycompany.controllers.ClubController.java

private byte[] LogoConvertion(byte[] bytes) {
    int width = 200;
    int height = 200;
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    try {//from   ww w.  jav a  2  s.  c o  m
        BufferedImage img = ImageIO.read(in);
        if (height == 0) {
            height = (width * img.getHeight()) / img.getWidth();
        }
        if (width == 0) {
            width = (height * img.getWidth()) / img.getHeight();
        }
        Image scaledImage = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        imageBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0, 0, 0), null);

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        ImageIO.write(imageBuff, "jpg", buffer);

        bytes = buffer.toByteArray();
    } catch (IOException e) {
        log.error("File convertion error");
    }
    return bytes;
}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

public static BufferedImage scale(BufferedImage bi, int width, int height) {
    BufferedImage bi2;//from  ww  w  .  j a v a 2 s  .  com
    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:com.galenframework.utils.GalenUtils.java

/**
 * Check the devicePixelRatio and adapts the size of the screenshot as if the ratio was 1.0
 * @param driver//from  w  w  w  . j  a  va  2 s.c  o  m
 * @param screenshotImage
 * @return
 */
public static BufferedImage resizeScreenshotIfNeeded(WebDriver driver, BufferedImage screenshotImage) {
    Double devicePixelRatio = ((Number) ((JavascriptExecutor) driver)
            .executeScript(JS_RETRIEVE_DEVICE_PIXEL_RATIO)).doubleValue();

    if (devicePixelRatio > 1.0 && screenshotImage.getWidth() > 0) {
        Long screenSize = (Long) ((JavascriptExecutor) driver).executeScript(
                "return Math.max(" + "document.body.scrollWidth, document.documentElement.scrollWidth,"
                        + "document.body.offsetWidth, document.documentElement.offsetWidth,"
                        + "document.body.clientWidth, document.documentElement.clientWidth);");

        Double estimatedPixelRatio = ((double) screenshotImage.getWidth()) / ((double) screenSize);

        if (estimatedPixelRatio > 1.0) {

            int newWidth = (int) (screenshotImage.getWidth() / estimatedPixelRatio);
            int newHeight = (int) (screenshotImage.getHeight() / estimatedPixelRatio);

            Image tmp = screenshotImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
            BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

            Graphics2D g2d = scaledImage.createGraphics();
            g2d.drawImage(tmp, 0, 0, null);
            g2d.dispose();

            return scaledImage;
        } else
            return screenshotImage;
    } else
        return screenshotImage;
}

From source file:it.lufraproini.cms.servlet.upload_user_img.java

private Boolean creaThumbnail(Immagine img, Map info) {
    String cartella_img = getServletContext().getInitParameter("system.image_directory");
    String file_img = img.getFile().substring(img.getFile().indexOf("/"));
    String path_img = getServletContext().getRealPath(cartella_img);
    String cartella_thumb = getServletContext().getInitParameter("system.thumb_directory");
    String path_thumb = getServletContext().getRealPath(cartella_thumb);
    int altezza_thumb = 250;
    int larghezza_thumb = 250;
    BufferedImage thumb = new BufferedImage(larghezza_thumb, altezza_thumb, BufferedImage.TYPE_INT_RGB);
    String file_thumb = info.get("digest") + "." + info.get("estensione");

    try {/*from w  ww.ja v a 2s.  c  om*/
        if (ImageIO.read(new File(path_img + file_img)).getWidth() >= larghezza_thumb) {
            thumb.createGraphics().drawImage(ImageIO.read(new File(path_img + file_img)).getScaledInstance(-1,
                    altezza_thumb, Image.SCALE_SMOOTH), 0, 0, null);
        } else {
            thumb.createGraphics().drawImage(ImageIO.read(new File(path_img + file_img))
                    .getScaledInstance(larghezza_thumb, altezza_thumb, Image.SCALE_SMOOTH), 0, 0, null);
        }

        ImageIO.write(thumb, info.get("estensione").toString(),
                new File(path_thumb + File.separatorChar + info.get("digest") + "." + info.get("estensione")));
    } catch (IOException ex) {
        return false;
    }
    img.setThumb(cartella_thumb + File.separatorChar + file_thumb);
    return true;
}

From source file:projectresurrection.Weather.java

private ImageIcon getImage(String desc) {
    String iconPath = "F:\\Java\\ProjectResurrection\\icons\\weather\\";
    if (Eve.clock.getCurrent().getHourOfDay() > 19 || Eve.clock.getCurrent().getHourOfDay() < 6) {
        iconPath += "night\\";
    }// w  w w  .j  a va  2s .  c om
    switch (desc) {
    case "Overcast":
        iconPath += "cloudy.png";
        break;
    case "Clear":
        iconPath += "clear.png";
        break;
    case "Partly Cloudy":
        iconPath += "partly cloudy.png";
        break;
    case "Moderate or heavy rain shower":
        iconPath += "storm.png";
        break;
    case "Sunny":
        iconPath += "clear.png";
        break;
    case "Mist":
        iconPath += "fog.png";
        break;
    case "Ligt rain shower":
        iconPath += "shower.png";
        break;
    case "Thundery outbreaks in nearby":
        iconPath += "storm.png";
        break;
    case "Patchy light rain in area with thunder":
        iconPath += "storm.png";
        break;
    case "Light rain shower":
        iconPath += "shower.png";
        break;
    case "Light drizzle":
        iconPath += "shower.png";
        break;
    case "Patchy light drizzle":
        iconPath += "shower.png";
        break;
    case "Patchy rain nearby":
        iconPath += "shower.png";
        break;
    case "Heavy Rain With Thunderstorm, Mist":
        iconPath += "storm.png";
        break;
    case "Thunderstorm In Vicinity":
        iconPath += "storm.png";
        break;
    case "Light Rain":
        iconPath += "shower.png";
    default:
        iconPath += "clear.png";
        break;
    }
    Image img = new ImageIcon(iconPath).getImage();
    Image newimg = img.getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH);
    return new ImageIcon(newimg);
}

From source file:it.illinois.adsc.ema.softgrid.monitoring.ui.SPMainFrame.java

private void setupGUI() throws Exception {
    this.getContentPane().setLayout(new GridBagLayout());
    chartPanel = getChartPanel();//w  ww  .  j  av  a 2s  . c  o m
    this.setPreferredSize(new Dimension(800, 700));

    alertPanel.setPreferredSize(new Dimension(200, 700));
    alertPanel.setMinimumSize(new Dimension(200, 700));
    alertPanel.setMaximumSize(new Dimension(200, 700));

    transientPanel.setPreferredSize(new Dimension(200, 700));
    transientPanel.setMinimumSize(new Dimension(200, 700));
    transientPanel.setMaximumSize(new Dimension(200, 700));

    queryTextArea.setPreferredSize(new Dimension(300, 100));
    queryTextArea.setMinimumSize(new Dimension(300, 100));
    queryTextArea.setMaximumSize(new Dimension(300, 100));

    monitorButton.setPreferredSize(new Dimension(30, 30));
    monitorButton.setMinimumSize(new Dimension(30, 30));
    monitorButton.setMaximumSize(new Dimension(30, 30));
    monitorButton.setToolTipText("Execute and Monitor");

    exitButton.setPreferredSize(new Dimension(30, 30));
    exitButton.setMinimumSize(new Dimension(30, 30));
    exitButton.setMaximumSize(new Dimension(30, 30));
    exitButton.setToolTipText("Close and Exit");

    clearButton.setPreferredSize(new Dimension(30, 30));
    clearButton.setMinimumSize(new Dimension(30, 30));
    clearButton.setMaximumSize(new Dimension(30, 30));
    clearButton.setToolTipText("Reset");

    runButton.setPreferredSize(new Dimension(30, 30));
    runButton.setMinimumSize(new Dimension(30, 30));
    runButton.setMaximumSize(new Dimension(30, 30));
    runButton.setToolTipText("Initialize the server...!");

    alertPanel.setLayout(new VerticalFlowLayout());
    transientPanel.setLayout(new BorderLayout());
    //      System.out.println("new File(\"../MonitorEngine/Images/execute-xxl.png\").exists() = " + new File("../MonitorEngine/Images/execute-xxl.png").exists());
    //      System.out.println("execute-xxl.png = " + new File("execute-xxl.png").exists());
    //      System.out.println("new File().getAbsolutePath() = " + new File("openmuc.jar").getAbsolutePath());
    Image img = new ImageIcon(ImageIO.read(getClass().getClassLoader().getResourceAsStream("execute-xxl.png")))
            .getImage();
    Image newimg = img.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
    ImageIcon icon = new ImageIcon(newimg);
    monitorButton.setIcon(icon);

    img = new ImageIcon(ImageIO.read(getClass().getClassLoader().getResourceAsStream("stop-xxl.png")))
            .getImage();
    newimg = img.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
    icon = new ImageIcon(newimg);
    exitButton.setIcon(icon);

    img = new ImageIcon(ImageIO.read(getClass().getClassLoader().getResourceAsStream("reset-xxl.png")))
            .getImage();
    newimg = img.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
    icon = new ImageIcon(newimg);
    clearButton.setIcon(icon);

    img = new ImageIcon(ImageIO.read(getClass().getClassLoader().getResourceAsStream("start-xxl.png")))
            .getImage();
    newimg = img.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
    icon = new ImageIcon(newimg);
    runButton.setIcon(icon);
    altertScrolPane.getViewport().add(alertPanel, null);
    altertScrolPane.setBorder(BorderFactory.createEtchedBorder());
    logAreaScrollPane.getViewport().add(logTextArea, null);
    logAreaScrollPane.setBorder(BorderFactory.createEtchedBorder());

    JPanel tempQueryPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    //      tempQueryPanel.setBorder(BorderFactory.createTitledBorder("Monitor Query"));
    tempQueryPanel.setLayout(new GridBagLayout());
    buttonPanel.setLayout(new GridBagLayout());
    buttonPanel.setBorder(BorderFactory.createEtchedBorder());
    buttonPanel.setOpaque(true);
    buttonPanel.setBackground(Color.gray);
    buttonPanel.add(runButton, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER,
            GridBagConstraints.NONE, new Insets(6, 12, 3, 12), 0, 0));
    buttonPanel.add(monitorButton, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.CENTER,
            GridBagConstraints.NONE, new Insets(3, 12, 3, 12), 0, 0));
    buttonPanel.add(clearButton, new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.CENTER,
            GridBagConstraints.NONE, new Insets(3, 12, 3, 12), 0, 0));
    buttonPanel.add(exitButton, new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.CENTER,
            GridBagConstraints.NONE, new Insets(3, 12, 3, 12), 0, 0));
    //      tempQueryPanel.add(splitPane, new GridBagConstraints(0, 0, 1, 6, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    tempQueryPanel.add(buttonPanel, new GridBagConstraints(0, 0, 1, 1, 0, 1, GridBagConstraints.CENTER,
            GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0, 0));
    tempQueryPanel.add(queryScrolPane, new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER,
            GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    mainTabbedPane.add(splitPane, "Controller");

    mainTabbedPane.add(ConfigPanel.getInstance(), "Configuration");

    splitPane.setTopComponent(tempQueryPanel);
    splitPane.setDividerLocation(152);
    splitPane.setBottomComponent(resultTabbedPane);
    splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
    this.getContentPane().add(mainTabbedPane, new GridBagConstraints(0, 0, 1, 2, 0.75, 0.25,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(12, 12, 12, 0), 0, 0));
    //      this.getContentPane().add(alertTitile, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(12, 0, 0, 12), 0, 0));
    //      this.getContentPane().add(altertScrolPane, new GridBagConstraints(1, 1, 1, 2, 0, 1, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0, 0, 12, 12), 0, 0));
    //      tempQueryPanel.add(queryScrolPane, new GridBagConstraints(0, 0, 1, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    //      this.getContentPane().add(logAreaScrollPane, new GridBagConstraints(0, 2, 1, 1, 0.75, 0.75, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(12, 12, 12, 0), 0, 0));
    exitButton.addActionListener(this);
    monitorButton.addActionListener(this);
    clearButton.addActionListener(this);
    clearButton.setEnabled(false);
    clearButton.setVisible(false);
    monitorButton.setEnabled(true);
    runButton.addActionListener(this);
    queryTextArea.setContentType("text/html");
    queryTextArea.setText("select overloadrank from virtual");

    messageHandler = new MessageUIHandler(logTextArea, logAreaScrollPane);
    ConfigPanel.getInstance().setupConfigPanel();
}

From source file:com.github.dactiv.fear.service.service.account.AccountService.java

/**
 * ??// w  w  w.j av  a2  s  .  c o m
 *
 * @param sourcePath   ?
 * @param targetPath   ??
 * @param portraitSize ?
 *
 * @throws IOException
 */
private String scaleImage(String sourcePath, String targetPath, PortraitSize portraitSize) throws IOException {
    InputStream inputStream = new FileInputStream(sourcePath);

    BufferedImage source = ImageIO.read(inputStream);
    ColorModel targetColorModel = source.getColorModel();

    inputStream.close();

    int width = portraitSize.getWidth();
    int height = portraitSize.getHeight();

    BufferedImage target = new BufferedImage(targetColorModel,
            targetColorModel.createCompatibleWritableRaster(width, height),
            targetColorModel.isAlphaPremultiplied(), null);

    Image scaleImage = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);

    Graphics2D g = target.createGraphics();
    g.drawImage(scaleImage, 0, 0, width, height, null);
    g.dispose();

    String result = targetPath + portraitSize.getName();
    FileOutputStream fileOutputStream = new FileOutputStream(result);
    ImageIO.write(target, PORTRAIT_PIC_TYPE, fileOutputStream);

    fileOutputStream.close();

    return result;
}

From source file:org.exist.launcher.Launcher.java

private boolean initSystemTray() {
    final Dimension iconDim = tray.getTrayIconSize();
    BufferedImage image = null;/*from  w  w  w  .  ja  va  2 s .  c o m*/
    try {
        image = ImageIO.read(getClass().getResource("icon32.png"));
    } catch (final IOException e) {
        showMessageAndExit("Launcher failed", "Failed to read system tray icon.", false);
    }
    trayIcon = new TrayIcon(image.getScaledInstance(iconDim.width, iconDim.height, Image.SCALE_SMOOTH),
            "eXist-db Launcher");

    final JDialog hiddenFrame = new JDialog();
    hiddenFrame.setUndecorated(true);
    hiddenFrame.setIconImage(image);

    final PopupMenu popup = createMenu();
    trayIcon.setPopupMenu(popup);
    trayIcon.addActionListener(actionEvent -> {
        trayIcon.displayMessage(null, "Right click for menu", TrayIcon.MessageType.INFO);
        setServiceState();
    });

    // add listener for left click on system tray icon. doesn't work well on linux though.
    if (!SystemUtils.IS_OS_LINUX) {
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent mouseEvent) {
                if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
                    setServiceState();
                    hiddenFrame.add(popup);
                    popup.show(hiddenFrame, mouseEvent.getXOnScreen(), mouseEvent.getYOnScreen());
                }
            }
        });
    }

    try {
        hiddenFrame.setResizable(false);
        hiddenFrame.pack();
        hiddenFrame.setVisible(true);
        tray.add(trayIcon);
    } catch (final AWTException e) {
        return false;
    }

    return true;
}

From source file:preprocessing.Utils.java

public static BufferedImage resize(BufferedImage img, int newW, int newH) {
    Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = dimg.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);//from  ww w  .j  a v a2  s  .c om
    g2d.dispose();

    return dimg;
}

From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java

private static BufferedImage toThumbnailSize(BufferedImage image) {
    final int w = image.getWidth();
    final int h = image.getHeight();
    final int max = Math.max(w, h);
    if (max <= 256) {
        return image;
    }//from w  w  w .  j  ava2  s  .  co m
    final double s = max / 256d;
    final BufferedImage result = new BufferedImage(Math.max((int) Math.min(w / s, 256), 1),
            Math.max((int) Math.min(h / s, 256), 1), BufferedImage.TYPE_INT_BGR);
    final Graphics2D graphics = result.createGraphics();
    try {
        graphics.drawImage(image.getScaledInstance(result.getWidth(), result.getHeight(), Image.SCALE_SMOOTH),
                0, 0, null);
    } finally {
        graphics.dispose();
    }
    return result;
}