List of usage examples for java.awt.image BufferedImage getSubimage
public BufferedImage getSubimage(int x, int y, int w, int h)
From source file:lu.fisch.unimozer.Diagram.java
public void copyToClipboardPNG() { Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //DataFlavor pngFlavor = new DataFlavor("image/png","Portable Network Graphics"); // reset the comments commentString = null;//from w ww. j a v a 2s . c o m commentPoint = null; // get diagram BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB); paint(image.getGraphics()); /*System.out.println(this.topLeft.x); System.out.println(this.topLeft.y); System.out.println(this.bottomRight.x); System.out.println(this.bottomRight.y);*/ int x = this.topLeft.x - 2; if (x < 0) x = 0; int y = this.topLeft.y - 2; if (y < 0) y = 0; image = new BufferedImage(this.bottomRight.x + 2 + 1 + 4, this.bottomRight.y + 2 + 1 + 4, BufferedImage.TYPE_INT_ARGB); paint(image.getGraphics()); /*System.out.println(this.getWidth()); System.out.println(this.getParent().getWidth()); System.out.println(x+this.bottomRight.x-x+2+1+4); System.out.println(x+" , "+ y+" , "+ (this.bottomRight.x-x+2+1+4)+" , "+ (this.bottomRight.y-y+2+1+4)); */ BufferedImage image2 = image.getSubimage(x, y, this.bottomRight.x - x + 2 + 1 + 4, this.bottomRight.y - y + 2 + 1 + 4); // put image to clipboard ImageSelection imageSelection = new ImageSelection(image2); try { systemClipboard.setContents(imageSelection, null); } catch (Exception ex) { // ignore } // use the JWS clipboard if loaded via JWS try { Class.forName("javax.jnlp.ClipboardService"); final ClipboardService clipboardService = (ClipboardService) ServiceManager .lookup("javax.jnlp.ClipboardService"); clipboardService.setContents(imageSelection); } catch (Exception ex) { //ex.printStackTrace(); } }
From source file:net.sourceforge.subsonic.controller.CoverArtControllerEx.java
public static BufferedImage scale(BufferedImage image, int width, int height) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage thumb = image; // For optimal results, use step by step bilinear resampling - halfing the size at each step. do {//from w w w . j a v a 2 s .c om w /= 2; h /= 2; if (w < width) { w = width; } if (h < height) { h = height; } double thumbRatio = (double) width / (double) height; double aspectRatio = (double) w / (double) h; // LOG.debug("## thumbsRatio: " + thumbRatio); // LOG.debug("## aspectRatio: " + aspectRatio); if (thumbRatio < aspectRatio) { h = (int) (w / aspectRatio); } else { w = (int) (h * aspectRatio); } BufferedImage temp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = temp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(thumb, 0, 0, w, h, null); g2.dispose(); thumb = temp; } while (w != width); //FIXME: check if (thumb.getHeight() > thumb.getWidth()) { thumb = thumb.getSubimage(0, 0, thumb.getWidth(), thumb.getWidth()); } return thumb; }
From source file:org.apache.hadoop.chukwa.hicc.ImageSlicer.java
public BufferedImage tile(BufferedImage image, int level, XYData quadrant, XYData size, boolean efficient) throws Exception { double scale = Math.pow(2, level); if (efficient) { /* efficient: crop out the area of interest first, then scale and copy it */ XYData inverSize = new XYData((int) (image.getWidth(null) / (size.getX() * scale)), (int) (image.getHeight(null) / (size.getY() * scale))); XYData topLeft = new XYData(quadrant.getX() * size.getX() * inverSize.getX(), quadrant.getY() * size.getY() * inverSize.getY()); XYData newSize = new XYData((size.getX() * inverSize.getX()), (size.getY() * inverSize.getY())); if (inverSize.getX() < 1.0 || inverSize.getY() < 1.0) { throw new Exception("Requested zoom level (" + level + ") is too high."); }//w w w.j a v a 2s . c o m image = image.getSubimage(topLeft.getX(), topLeft.getY(), newSize.getX(), newSize.getY()); BufferedImage zoomed = new BufferedImage(size.getX(), size.getY(), BufferedImage.TYPE_INT_RGB); zoomed.getGraphics().drawImage(image, 0, 0, size.getX(), size.getY(), null); if (level > maxLevel) { maxLevel = level; } return zoomed; } else { /* inefficient: copy the whole image, scale it and then crop out the area of interest */ XYData newSize = new XYData((int) (size.getX() * scale), (int) (size.getY() * scale)); XYData topLeft = new XYData(quadrant.getX() * size.getX(), quadrant.getY() * size.getY()); if (newSize.getX() > image.getWidth(null) || newSize.getY() > image.getHeight(null)) { throw new Exception("Requested zoom level (" + level + ") is too high."); } AffineTransform tx = new AffineTransform(); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); tx.scale(scale, scale); image = op.filter(image, null); BufferedImage zoomed = image.getSubimage(topLeft.getX(), topLeft.getY(), newSize.getX(), newSize.getY()); if (level > maxLevel) { maxLevel = level; } return zoomed; } }
From source file:org.asqatasun.sebuilder.interpreter.TgTestRun.java
/** * Resize the snapshot image to 270*170/*from www . j a v a 2 s .co m*/ * * @param originalImage * @return */ private BufferedImage resizeImage(BufferedImage originalImage) { float scaleRatio = (float) 270 / originalImage.getWidth(); int resizedImageHeight = Float.valueOf(originalImage.getHeight() * scaleRatio).intValue(); BufferedImage resizedImage = new BufferedImage(270, resizedImageHeight, originalImage.getType()); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, 270, resizedImageHeight, null); g.dispose(); return resizedImage.getSubimage(0, 0, 270, 170); }
From source file:org.broad.igv.hic.MainWindow.java
private JMenuBar createMenuBar(final JPanel hiCPanel) { JMenuBar menuBar = new JMenuBar(); //======== fileMenu ======== JMenu fileMenu = new JMenu("File"); //---- loadMenuItem ---- JMenuItem loadMenuItem = new JMenuItem(); loadMenuItem.setText("Load..."); loadMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { loadMenuItemActionPerformed(e); }//from w ww. j a v a 2 s . co m }); fileMenu.add(loadMenuItem); //---- loadFromURL ---- JMenuItem loadFromURL = new JMenuItem(); JMenuItem getEigenvector = new JMenuItem(); final JCheckBoxMenuItem viewDNAseI; loadFromURL.setText("Load from URL ..."); loadFromURL.setName("loadFromURL"); loadFromURL.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { loadFromURLActionPerformed(e); } }); fileMenu.add(loadFromURL); fileMenu.addSeparator(); // Pre-defined datasets. TODO -- generate from a file addPredefinedLoadItems(fileMenu); JMenuItem saveToImage = new JMenuItem(); saveToImage.setText("Save to image"); saveToImage.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { BufferedImage image = (BufferedImage) createImage(1000, 1000); Graphics g = image.createGraphics(); hiCPanel.paint(g); JFileChooser fc = new JFileChooser(); fc.setSelectedFile(new File("image.png")); int actionDialog = fc.showSaveDialog(null); if (actionDialog == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); if (file.exists()) { actionDialog = JOptionPane.showConfirmDialog(null, "Replace existing file?"); if (actionDialog == JOptionPane.NO_OPTION || actionDialog == JOptionPane.CANCEL_OPTION) return; } try { // default if they give no format or invalid format String fmt = "jpg"; int ind = file.getName().indexOf("."); if (ind != -1) { String ext = file.getName().substring(ind + 1); String[] strs = ImageIO.getWriterFormatNames(); for (String aStr : strs) if (ext.equals(aStr)) fmt = ext; } ImageIO.write(image.getSubimage(0, 0, 600, 600), fmt, file); } catch (IOException ie) { System.err.println("Unable to write " + file + ": " + ie); } } } }); fileMenu.add(saveToImage); getEigenvector = new JMenuItem("Get principal eigenvector"); getEigenvector.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { getEigenvectorActionPerformed(e); } }); fileMenu.add(getEigenvector); //---- exit ---- JMenuItem exit = new JMenuItem(); exit.setText("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { exitActionPerformed(e); } }); fileMenu.add(exit); menuBar.add(fileMenu); //======== Tracks menu ======== JMenu tracksMenu = new JMenu("Tracks"); viewEigenvector = new JCheckBoxMenuItem("View Eigenvector..."); viewEigenvector.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (viewEigenvector.isSelected()) { if (eigenvectorTrack == null) { eigenvectorTrack = new EigenvectorTrack("eigen", "Eigenvectors"); } updateEigenvectorTrack(); } else { trackPanel.setEigenvectorTrack(null); if (HiCTrackManager.getLoadedTracks().isEmpty()) { trackPanel.setVisible(false); } } } }); viewEigenvector.setEnabled(false); tracksMenu.add(viewEigenvector); JMenuItem loadItem = new JMenuItem("Load..."); loadItem.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent actionEvent) { HiCTrackManager.loadHostedTrack(MainWindow.this); } }); tracksMenu.add(loadItem); JMenuItem loadFromFileItem = new JMenuItem("Load from file..."); loadFromFileItem.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent actionEvent) { HiCTrackManager.loadTrackFromFile(MainWindow.this); } }); tracksMenu.add(loadFromFileItem); menuBar.add(tracksMenu); //======== Extras menu ======== JMenu extrasMenu = new JMenu("Extras"); JMenuItem dumpPearsons = new JMenuItem("Dump pearsons matrix ..."); dumpPearsons.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent actionEvent) { BasicMatrix pearsons = hic.zd.getPearsons(); try { String chr1 = hic.getChromosomes()[hic.zd.getChr1()].getName(); String chr2 = hic.getChromosomes()[hic.zd.getChr2()].getName(); int binSize = hic.zd.getBinSize(); File initFile = new File("pearsons_" + chr1 + "_" + "_" + chr2 + "_" + binSize + ".bin"); File f = FileDialogUtils.chooseFile("Save pearsons", null, initFile, FileDialogUtils.SAVE); if (f != null) { ScratchPad.dumpPearsonsBinary(pearsons, chr1, chr2, hic.zd.getBinSize(), f); } } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }); JMenuItem dumpEigenvector = new JMenuItem("Dump eigenvector ..."); dumpEigenvector.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent actionEvent) { try { ScratchPad.dumpEigenvector(hic); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }); extrasMenu.add(dumpEigenvector); JMenuItem readPearsons = new JMenuItem("Read pearsons..."); readPearsons.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent actionEvent) { try { File f = FileDialogUtils.chooseFile("Pearsons file (Yunfan format)"); if (f != null) { BasicMatrix bm = ScratchPad.readPearsons(f.getAbsolutePath()); hic.zd.setPearsons(bm); } } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }); extrasMenu.add(readPearsons); extrasMenu.add(dumpPearsons); menuBar.add(extrasMenu); return menuBar; }
From source file:org.ednovo.gooru.application.util.GooruImageUtil.java
public static void cropImage(String path, int x, int y, int width, int height) throws Exception { BufferedImage srcImg = ImageIO.read(new File(path)); srcImg = srcImg.getSubimage(x, y, width, height); ImageIO.write(srcImg, "png", new File(path)); }
From source file:org.elfinder.servlets.commands.ResizeCommand.java
@Override public void execute() throws ConnectorException { File dirCurrent = getExistingDir(getParam("current"), FileActionEnum.WRITE); if (dirCurrent != null) { String targethash = (String) getParamObject("target"); File fileTarget = getExistingFile(targethash, dirCurrent, FileActionEnum.WRITE); String mode = (String) getParamObject("mode"); if (mode.equals("resize")) { int with = Integer.parseInt((String) getParamObject("width")); int height = Integer.parseInt((String) getParamObject("height")); ResampleOp resampleOp = new ResampleOp(with, height); BufferedImage origImage; try { origImage = ImageIO.read(fileTarget); String ext = FilenameUtils.getExtension(fileTarget.getName()); BufferedImage rescaledImage = resampleOp.filter(origImage, null); ImageIO.write(rescaledImage, ext, fileTarget); } catch (IOException rescaleEx) { java.util.logging.Logger.getLogger(ResizeCommand.class.getName()).log(Level.SEVERE, null, rescaleEx);/* w ww. j av a 2 s . c o m*/ } } else if (mode.equals("crop")) { int width = Integer.parseInt((String) getParamObject("width")); int height = Integer.parseInt((String) getParamObject("height")); int x = Integer.parseInt((String) getParamObject("x")); int y = Integer.parseInt((String) getParamObject("y")); BufferedImage origImage; try { origImage = ImageIO.read(fileTarget); String ext = FilenameUtils.getExtension(fileTarget.getName()); BufferedImage cropImage = origImage.getSubimage(x, y, width, height); ImageIO.write(cropImage, ext, fileTarget); } catch (IOException cropEx) { java.util.logging.Logger.getLogger(ResizeCommand.class.getName()).log(Level.SEVERE, null, cropEx); } } else if (mode.equals("rotate")) { BufferedImage origImage; int degree = Integer.parseInt((String) getParamObject("degree")); System.out.println("Degree: " + degree); try { origImage = ImageIO.read(fileTarget); String ext = FilenameUtils.getExtension(fileTarget.getName()); /* TODO: check affinetransfor for correct feature about rotation now rotation working only 90 degrees by 90 degrees*/ /* Some trouble with AffineTransform AffineTransform tx = new AffineTransform(); tx.rotate(Math.toRadians(degree), origImage.getWidth()/2, origImage.getHeight()/2); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); BufferedImage rotateImage = op.filter(origImage, null); */ ImageIO.write(rotateCw(origImage), ext, fileTarget); } catch (IOException cropEx) { java.util.logging.Logger.getLogger(ResizeCommand.class.getName()).log(Level.SEVERE, null, cropEx); } } _content(dirCurrent, true); } }
From source file:org.finra.jtaf.ewd.utils.ScreenshotUtils.java
/*** * You can use this method to take your control pictures. Note that the file should be a png. * //from w w w . j ava 2 s . c o m * @param element * @param toSaveAs * @throws IOException * @throws WidgetException * @throws WidgetTimeoutException * @throws Exception */ public static void takeScreenshotOfElement(IElement element, File toSaveAs) throws IOException, WidgetException { for (int i = 0; i < 10; i++) { //Loop up to 10x to ensure a clean screenshot was taken log.info("Taking screen shot of locator " + element.getLocator() + " ... attempt #" + (i + 1)); //Scroll to element element.scrollTo(); //Take picture of the page WebDriver wd = SessionManager.getInstance().getCurrentSession().getWrappedDriver(); File screenshot; boolean isRemote = false; if (!(wd instanceof RemoteWebDriver)) { screenshot = ((TakesScreenshot) wd).getScreenshotAs(OutputType.FILE); } else { Augmenter augmenter = new Augmenter(); screenshot = ((TakesScreenshot) augmenter.augment(wd)).getScreenshotAs(OutputType.FILE); isRemote = true; } BufferedImage fullImage = ImageIO.read(screenshot); WebElement ele = element.getWebElement(); //Parse out the picture of the element Point point = ele.getLocation(); int eleWidth = ele.getSize().getWidth(); int eleHeight = ele.getSize().getHeight(); int x; int y; if (isRemote) { x = ((Locatable) ele).getCoordinates().inViewPort().getX(); y = ((Locatable) ele).getCoordinates().inViewPort().getY(); } else { x = point.getX(); y = point.getY(); } log.debug("Screenshot coordinates x: " + x + ", y: " + y); BufferedImage eleScreenshot = fullImage.getSubimage(x, y, eleWidth, eleHeight); ImageIO.write(eleScreenshot, "png", screenshot); //Ensure clean snapshot (sometimes WebDriver takes bad pictures and they turn out all black) if (!isBlack(ImageIO.read(screenshot))) { FileUtils.copyFile(screenshot, toSaveAs); break; } } }
From source file:org.finra.jtaf.ewd.widget.element.Element.java
/*** * You can use this method to take screenshots of elements / control pictures to compare with. * Note that the file should be a png.//from w w w. j a v a2 s.c o m * * @param toSaveAs * @throws IOException * @throws WidgetTimeoutException */ public void captureElementScreenshot(File toSaveAs) throws IOException, WidgetException { for (int i = 0; i < 10; i++) { //Loop up to 10x to ensure a clean screenshot was taken //log.info("Taking screen shot of locator " + element.getByLocator() + " ... attempt #" + (i+1)); //Scroll to element this.scrollTo(); //Take picture of the page WebDriver wd = getGUIDriver().getWrappedDriver(); File screenshot; boolean isRemote = false; if (!(wd instanceof RemoteWebDriver)) { screenshot = ((TakesScreenshot) wd).getScreenshotAs(OutputType.FILE); } else { Augmenter augmenter = new Augmenter(); screenshot = ((TakesScreenshot) augmenter.augment(wd)).getScreenshotAs(OutputType.FILE); isRemote = true; } BufferedImage fullImage = ImageIO.read(screenshot); WebElement ele = this.getWebElement(); //Parse out the picture of the element Point point = ele.getLocation(); int eleWidth = ele.getSize().getWidth(); int eleHeight = ele.getSize().getHeight(); int x; int y; if (isRemote) { x = ((Locatable) ele).getCoordinates().inViewPort().getX(); y = ((Locatable) ele).getCoordinates().inViewPort().getY(); } else { x = point.getX(); y = point.getY(); } //log.debug("Screenshot coordinates x: "+x+", y: "+y); BufferedImage eleScreenshot = fullImage.getSubimage(x, y, eleWidth, eleHeight); ImageIO.write(eleScreenshot, "png", screenshot); //Ensure clean snapshot (sometimes WebDriver takes bad pictures and they turn out all black) if (!isScreenshotBlack(ImageIO.read(screenshot))) { FileUtils.copyFile(screenshot, toSaveAs); break; } } }
From source file:org.geopoke.WorldMap.java
/** * Get a snapshot of the current map as an image. * <p/>/* w ww . j ava 2 s. c o m*/ * @return the current snapshot of the map. */ public BufferedImage getSnapshot() { BufferedImage image = SwingFXUtils.fromFXImage(getScene().snapshot(null), null); Bounds bounds = localToScene(getLayoutBounds()); return image.getSubimage((int) bounds.getMinX(), (int) bounds.getMinY(), (int) getWidth(), (int) getHeight()); }