Example usage for java.awt Image SCALE_FAST

List of usage examples for java.awt Image SCALE_FAST

Introduction

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

Prototype

int SCALE_FAST

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

Click Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) {
    String imageFile = "A.jpg";
    RepaintManager.currentManager(null).setDoubleBufferingEnabled(false);
    Image image = Toolkit.getDefaultToolkit().getImage(Main.class.getResource(imageFile));
    image = image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_FAST);
    JFrame frame = new JFrame("DragImage");
    frame.getContentPane().add(new Main(image));
    frame.setSize(300, 300);//from  w  w w  .  j ava2  s . co  m
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Scale.java

public void go() {
    // Sleep first to see original
    rest();//from  w w  w . j  av a2s  .  c  o  m
    Image original = image;
    // Down fast
    image = original.getScaledInstance(200, -1, Image.SCALE_FAST);
    repaint();
    rest();
    // Down slow
    image = original.getScaledInstance(200, -1, Image.SCALE_SMOOTH);
    repaint();
    rest();
    // Up fast
    image = original.getScaledInstance(400, -1, Image.SCALE_FAST);
    repaint();
    rest();
    // Up slow
    image = original.getScaledInstance(400, -1, Image.SCALE_SMOOTH);
    repaint();
    rest();
    System.exit(0);
}

From source file:examples.gp.monalisa.gui.GeneticDrawingView.java

/**
 * Scale an image to fit the size of the targetImageLabel.
 *///from  www.  j  a  v  a 2s  . co m
public ImageIcon scaleToImageLabel(Image image) {
    ImageIcon scaled = new ImageIcon(image);
    if (scaled.getIconHeight() > targetImageLabel.getHeight()) {
        scaled = new ImageIcon(image.getScaledInstance(-1, targetImageLabel.getHeight(), Image.SCALE_FAST));
    }
    if (scaled.getIconWidth() > targetImageLabel.getWidth()) {
        scaled = new ImageIcon(image.getScaledInstance(targetImageLabel.getWidth(), -1, Image.SCALE_FAST));
    }
    return scaled;
}

From source file:examples.monalisa.gui.GeneticDrawingView.java

/**
 * Scale an image to fit the size of the targetImageLabel.
 *
 * @param a_image the image to scale//from w  w w. j a  v a2  s.  com
 * @return scaled image
 */
public ImageIcon scaleToImageLabel(Image a_image) {
    ImageIcon scaled = new ImageIcon(a_image);
    if (scaled.getIconHeight() > targetImageLabel.getHeight()) {
        scaled = new ImageIcon(a_image.getScaledInstance(-1, targetImageLabel.getHeight(), Image.SCALE_FAST));
    }
    if (scaled.getIconWidth() > targetImageLabel.getWidth()) {
        scaled = new ImageIcon(a_image.getScaledInstance(targetImageLabel.getWidth(), -1, Image.SCALE_FAST));
    }
    return scaled;
}

From source file:gd.gui.GeneticDrawingView.java

/**
 * Scale an image to fit the size of the targetImageLabel.
 *///from   ww w. j ava  2s  .c  o m
public ImageIcon scaleToImageLabel(Image image) {
    ImageIcon scaled = new ImageIcon(image);

    if (scaled.getIconHeight() > targetImageLabel.getHeight()) {
        scaled = new ImageIcon(image.getScaledInstance(-1, targetImageLabel.getHeight(), Image.SCALE_FAST));
    }
    if (scaled.getIconWidth() > targetImageLabel.getWidth()) {
        scaled = new ImageIcon(image.getScaledInstance(targetImageLabel.getWidth(), -1, Image.SCALE_FAST));
    }

    return scaled;
}

From source file:de.quadrillenschule.azocamsyncd.gui.ExploreWifiSDPanel.java

private void updateSingleView() {
    try {// ww w.  j  a v  a  2s  .  c o m
        if (remotejTree.getSelectionPaths().length > 0) {
            TreePath tp = remotejTree.getSelectionPaths()[0];
            String mynode = tp.getLastPathComponent().toString();
            AZoFTPFile myaffilea = null;
            for (AZoFTPFile af : afs) {
                if (new String(af.dir + af.ftpFile.getName()).equals(mynode)) {
                    myaffilea = af;
                    //     GlobalProperties gp=new GlobalProperties();
                    //     gp.setProperty(GlobalProperties.CamSyncProperties.LATESTIMAGEPATH, af.dir+af.ftpFile.getName());
                    break;
                }
            }
            final AZoFTPFile myaffile = myaffilea;
            Thread imageUpdater = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        File localFile;

                        localFile = localStorage.getLocalFile(myaffile);

                        localFileNamejTextField1.setText(localFile.getAbsolutePath());
                        ImageIcon ii = new ImageIcon(localFile.toURI().toURL());

                        int mywidth = imagejLabel.getWidth();
                        int width = ii.getIconWidth();
                        int height = ii.getIconHeight();
                        if (width <= 0) {
                            imagejLabel.setText("No image to view.");
                        } else {
                            imagejLabel.setText("");
                        }
                        double factor = (double) height / (double) width;
                        Image image = ii.getImage().getScaledInstance(mywidth,
                                (int) ((double) mywidth * factor), Image.SCALE_FAST);
                        imagejLabel.setIcon(new ImageIcon(image));
                    } catch (Exception ex) {
                        imagejLabel.setText("No image to view.");
                    }
                }
            });
            imageUpdater.start();

        }
    } catch (Exception e) {

    }
}

From source file:com.chiorichan.factory.event.PostImageProcessor.java

@EventHandler()
public void onEvent(PostEvalEvent event) {
    try {// w ww .ja  v a  2 s . co m
        if (event.context().contentType() == null
                || !event.context().contentType().toLowerCase().startsWith("image"))
            return;

        float x = -1;
        float y = -1;

        boolean cacheEnabled = AppConfig.get().getBoolean("advanced.processors.imageProcessorCache", true);
        boolean grayscale = false;

        ScriptingContext context = event.context();
        HttpRequestWrapper request = context.request();
        Map<String, String> rewrite = request.getRewriteMap();

        if (rewrite != null) {
            if (rewrite.get("serverSideOptions") != null) {
                String[] params = rewrite.get("serverSideOptions").trim().split("_");

                for (String p : params)
                    if (p.toLowerCase().startsWith("width") && x < 0)
                        x = Integer.parseInt(p.substring(5));
                    else if ((p.toLowerCase().startsWith("x") || p.toLowerCase().startsWith("w"))
                            && p.length() > 1 && x < 0)
                        x = Integer.parseInt(p.substring(1));
                    else if (p.toLowerCase().startsWith("height") && y < 0)
                        y = Integer.parseInt(p.substring(6));
                    else if ((p.toLowerCase().startsWith("y") || p.toLowerCase().startsWith("h"))
                            && p.length() > 1 && y < 0)
                        y = Integer.parseInt(p.substring(1));
                    else if (p.toLowerCase().equals("thumb")) {
                        x = 150;
                        y = 0;
                        break;
                    } else if (p.toLowerCase().equals("bw") || p.toLowerCase().equals("grayscale"))
                        grayscale = true;
            }

            if (request.getArgument("width") != null && request.getArgument("width").length() > 0)
                x = request.getArgumentInt("width");

            if (request.getArgument("height") != null && request.getArgument("height").length() > 0)
                y = request.getArgumentInt("height");

            if (request.getArgument("w") != null && request.getArgument("w").length() > 0)
                x = request.getArgumentInt("w");

            if (request.getArgument("h") != null && request.getArgument("h").length() > 0)
                y = request.getArgumentInt("h");

            if (request.getArgument("thumb") != null) {
                x = 150;
                y = 0;
            }

            if (request.hasArgument("bw") || request.hasArgument("grayscale"))
                grayscale = true;
        }

        // Tests if our Post Processor can process the current image.
        List<String> readerFormats = Arrays.asList(ImageIO.getReaderFormatNames());
        List<String> writerFormats = Arrays.asList(ImageIO.getWriterFormatNames());
        if (context.contentType() != null
                && !readerFormats.contains(context.contentType().split("/")[1].toLowerCase()))
            return;

        int inx = event.context().buffer().readerIndex();
        BufferedImage img = ImageIO.read(new ByteBufInputStream(event.context().buffer()));
        event.context().buffer().readerIndex(inx);

        if (img == null)
            return;

        float w = img.getWidth();
        float h = img.getHeight();
        float w1 = w;
        float h1 = h;

        if (x < 1 && y < 1) {
            x = w;
            y = h;
        } else if (x > 0 && y < 1) {
            w1 = x;
            h1 = x * (h / w);
        } else if (y > 0 && x < 1) {
            w1 = y * (w / h);
            h1 = y;
        } else if (x > 0 && y > 0) {
            w1 = x;
            h1 = y;
        }

        boolean resize = w1 > 0 && h1 > 0 && w1 != w && h1 != h;
        boolean argb = request.hasArgument("argb") && request.getArgument("argb").length() == 8;

        if (!resize && !argb && !grayscale)
            return;

        // Produce a unique encapsulated id based on this image processing request
        String encapId = SecureFunc.md5(context.filename() + w1 + h1 + request.getArgument("argb") + grayscale);
        File tmp = context.site() == null ? AppConfig.get().getDirectoryCache()
                : context.site().directoryTemp();
        File file = new File(tmp, encapId + "_" + new File(context.filename()).getName());

        if (cacheEnabled && file.exists()) {
            event.context().resetAndWrite(FileUtils.readFileToByteArray(file));
            return;
        }

        Image image = resize ? img.getScaledInstance(Math.round(w1), Math.round(h1),
                AppConfig.get().getBoolean("advanced.processors.useFastGraphics", true) ? Image.SCALE_FAST
                        : Image.SCALE_SMOOTH)
                : img;

        // TODO Report malformed parameters to user

        if (argb) {
            FilteredImageSource filteredSrc = new FilteredImageSource(image.getSource(),
                    new RGBColorFilter((int) Long.parseLong(request.getArgument("argb"), 16)));
            image = Toolkit.getDefaultToolkit().createImage(filteredSrc);
        }

        BufferedImage rtn = new BufferedImage(Math.round(w1), Math.round(h1), img.getType());
        Graphics2D graphics = rtn.createGraphics();
        graphics.drawImage(image, 0, 0, null);
        graphics.dispose();

        if (grayscale) {
            ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
            op.filter(rtn, rtn);
        }

        if (resize)
            Log.get().info(EnumColor.GRAY + "Resized image from " + Math.round(w) + "px by " + Math.round(h)
                    + "px to " + Math.round(w1) + "px by " + Math.round(h1) + "px");

        if (rtn != null) {
            ByteArrayOutputStream bs = new ByteArrayOutputStream();

            if (context.contentType() != null
                    && writerFormats.contains(context.contentType().split("/")[1].toLowerCase()))
                ImageIO.write(rtn, context.contentType().split("/")[1].toLowerCase(), bs);
            else
                ImageIO.write(rtn, "png", bs);

            if (cacheEnabled && !file.exists())
                FileUtils.writeByteArrayToFile(file, bs.toByteArray());

            event.context().resetAndWrite(bs.toByteArray());
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }

    return;
}

From source file:it.geosolutions.opensdi2.mvc.BaseFileManager.java

/**
 * Generate a image thumb//from w w w . j a  va  2  s.  c o m
 * 
 * @param toServeUp
 * @param thumbPath
 * @return
 * @throws IOException
 */
protected InputStream getImageThumb(File toServeUp, String thumbPath) throws IOException {
    BufferedImage image = ImageIO.read(toServeUp);
    ImageIcon img = new ImageIcon(
            new ImageIcon(image).getImage().getScaledInstance(THUMB_W, THUMB_H, Image.SCALE_FAST));
    BufferedImage bimage = new BufferedImage(THUMB_W, THUMB_H, BufferedImage.TYPE_INT_RGB);

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    g.drawImage(img.getImage(), 0, 0, null);
    g.dispose();
    ImageIO.write(bimage, "jpg", new File(thumbPath));
    return new java.io.FileInputStream(thumbPath);
}

From source file:ru.spbspu.viewer.DataView.java

/**
 * ? ?  // w ww  .j  a  v  a 2  s . c  o  m
 */
void buildSpectrogram() {
    int cursorHeight = 19;
    spectrogram.setSize(spectrogram.getWidth(), jScrollPane1.getHeight() - cursorHeight);
    int spectHeigth = spectrogram.getHeight() - cursorHeight / 2;

    int spectWidth = spectrogram.getWidth() - SCALE_OF_SPECT - SPECT_RIGHT_SPACE;
    double limit = Double.valueOf(spinnerLimitFreq.getValue().toString()) * 2
            / Double.valueOf(spinnerDiscretization.getValue().toString());
    Image spectrogram = _presenter.getSpectrogram(getFramePosition(), getFrameWidth(), getWindowWidth(), limit)
            .getScaledInstance(spectWidth, spectHeigth, Image.SCALE_FAST);
    labelForFrameSpectr.setIcon(new ImageIcon(spectrogram));
    drawScale();
}