List of usage examples for javax.swing ImageIcon getIconHeight
public int getIconHeight()
From source file:org.apache.cocoon.reading.RepoImageReader.java
/** * @return the time the read source was last modified or 0 if it is not * possible to detect/*from www . j av a2 s.co m*/ */ /* public long getLastModified() { if (hasRanges()) { // This is a byte range request so we can't use the cache, return null. return 0; } if (quickTest) { return inputSource.getLastModified(); } final String systemId = (String) documents.get(request.getRequestURI()); // Note: getURI() might be null in some incomplete implementations final String sourceURI = inputSource.getURI(); if (systemId == null || (sourceURI != null && sourceURI.equals(systemId))) { return inputSource.getLastModified(); } documents.remove(request.getRequestURI()); return 0; }*/ protected void processStream(InputStream inputStream) throws IOException, ProcessingException { if (inputStream != null) { if (hasTransform()) { if (getLogger().isDebugEnabled()) { getLogger().debug("image " + ((width == 0) ? "?" : Integer.toString(width)) + "x" + ((height == 0) ? "?" : Integer.toString(height)) + " expires: " + expires); } try { byte content[] = readFully(inputStream); ImageIcon icon = new ImageIcon(content); BufferedImage original = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); BufferedImage currentImage = original; currentImage.getGraphics().drawImage(icon.getImage(), 0, 0, null); if (width > 0 || height > 0) { double ow = icon.getImage().getWidth(null); double oh = icon.getImage().getHeight(null); if (usePercent) { if (width > 0) { width = Math.round((int) (ow * width) / 100); } if (height > 0) { height = Math.round((int) (oh * height) / 100); } } AffineTransformOp filter = new AffineTransformOp(getTransform(ow, oh, width, height), AffineTransformOp.TYPE_BILINEAR); WritableRaster scaledRaster = filter.createCompatibleDestRaster(currentImage.getRaster()); filter.filter(currentImage.getRaster(), scaledRaster); currentImage = new BufferedImage(original.getColorModel(), scaledRaster, true, null); } JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(currentImage); p.setQuality(this.quality[0], true); encoder.setJPEGEncodeParam(p); encoder.encode(currentImage); out.flush(); } catch (ImageFormatException e) { throw new ProcessingException( "Error reading the image. " + "Note that only JPEG images are currently supported."); } finally { // Bugzilla Bug 25069, close inputStream in finally block // this will close inputStream even if processStream throws // an exception inputStream.close(); } } else { try { InputStream is = inputStream; long expires = parameters.getParameterAsInteger("expires", -1); if (expires > 0) { response.setDateHeader("Expires", System.currentTimeMillis() + expires); } response.setHeader("Accept-Ranges", "bytes"); byte[] buffer = new byte[8192]; int length; while ((length = is.read(buffer)) > -1) { out.write(buffer, 0, length); } is.close(); out.flush(); } catch (RuntimeException e) { throw e; } finally { // Bugzilla Bug 25069, close inputStream in finally block // this will close inputStream even if processStream throws // an exception inputStream.close(); } } } else { throw new IOException("Deals: Problem, resource not found or Repository not working correctly"); } }
From source file:org.craftercms.cstudio.alfresco.dm.content.pipeline.impl.CheckImageSizeProcessor.java
/** * check the width and the height of the given image as an inputstream match the width and the height specified * * @param in/*www. ja v a 2 s . co m*/ * @param allowedWidth * @param allowedHeight * @param lessSize * @param assetInfo * @return * @throws org.craftercms.cstudio.alfresco.service.exception.ServiceException * @throws org.craftercms.cstudio.alfresco.service.exception.ContentNotAllowedException */ protected InputStream checkForImageSize(InputStream in, int allowedWidth, int allowedHeight, boolean lessSize, ContentAssetInfoTO assetInfo) throws ContentNotAllowedException, ContentProcessException { ByteArrayOutputStream byteOutput = null; try { byteOutput = new ByteArrayOutputStream(); byte[] buffer = new byte[CStudioWebScriptConstants.READ_BUFFER_LENGTH]; int read = 0; while ((read = in.read(buffer)) > 0) { byteOutput.write(buffer, 0, read); } byte[] imageData = byteOutput.toByteArray(); Image image = Toolkit.getDefaultToolkit().createImage(imageData); ImageIcon icon = new ImageIcon(image); int height = icon.getIconHeight(); int width = icon.getIconWidth(); validateImageSize(allowedWidth, allowedHeight, height, width, lessSize); assetInfo.setHeight(height); assetInfo.setWidth(width); return new ByteArrayInputStream(imageData); } catch (IOException e) { throw new ContentProcessException(e); } finally { // close the original inputstream ContentUtils.release(in); ContentUtils.release(byteOutput); } }
From source file:org.craftercms.studio.impl.v1.content.pipeline.CheckImageSizeProcessor.java
/** * check the width and the height of the given image as an inputstream match the width and the height specified * * @param in//from w w w . j a va 2 s. c o m * @param allowedWidth * @param allowedHeight * @param lessSize * @param assetInfo * @return */ protected InputStream checkForImageSize(InputStream in, int allowedWidth, int allowedHeight, boolean lessSize, ContentAssetInfoTO assetInfo) throws ContentNotAllowedException, ContentProcessException { ByteArrayOutputStream byteOutput = null; try { byteOutput = new ByteArrayOutputStream(); byte[] buffer = new byte[1024];//PORT CStudioWebScriptConstants.READ_BUFFER_LENGTH]; int read = 0; while ((read = in.read(buffer)) > 0) { byteOutput.write(buffer, 0, read); } byte[] imageData = byteOutput.toByteArray(); Image image = Toolkit.getDefaultToolkit().createImage(imageData); ImageIcon icon = new ImageIcon(image); int height = icon.getIconHeight(); int width = icon.getIconWidth(); validateImageSize(allowedWidth, allowedHeight, height, width, lessSize); assetInfo.setHeight(height); assetInfo.setWidth(width); return new ByteArrayInputStream(imageData); } catch (IOException e) { throw new ContentProcessException(e); } finally { // close the original inputstream ContentUtils.release(in); ContentUtils.release(byteOutput); } }
From source file:org.echocat.velma.Resources.java
@Nonnull protected List<? extends Image> loadIcons(@Nonnull List<URL> iconUrls) { final List<Image> result = new ArrayList<>(); for (URL iconUrl : iconUrls) { final ImageIcon image = new ImageIcon(iconUrl); if (image.getIconHeight() >= 0) { result.add(image.getImage()); }//from www . ja v a 2 s .c om } return unmodifiableList(result); }
From source file:org.giavacms.base.controller.ResourceController.java
protected void setDimensions() { width = 0;//from www.j a va2 s . co m height = 0; croppedImage = null; if (!ResourceType.IMAGE.equals(getElement().getResourceType())) { return; } ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext() .getContext(); String folder = servletContext.getRealPath("") + File.separator; getElement().setBytes(FileUtils.getBytesFromFile( new File(new File(folder, ResourceType.IMAGE.getFolder()), getElement().getName()))); ImageIcon imageIcon = new ImageIcon(getElement().getBytes()); width = imageIcon.getIconWidth(); height = imageIcon.getIconHeight(); }
From source file:org.jajuk.ui.views.CoverView.java
/** * Long action to compute image to display (download, resizing...) * //from w w w .jav a 2 s. c o m * @param index * * @throws JajukException the jajuk exception */ private void prepareDisplay(final int index) throws JajukException { // find next correct cover Cover cover = null; ImageIcon icon = null; try { cover = alCovers.get(index); // take image at the given index Log.debug("Display cover: " + cover + " at index :" + index); Image img = cover.getImage(); // Never mirror our no cover image if (cover.getType().equals(CoverType.NO_COVER)) { icon = new ImageIcon(img); } else { if ( // should we mirror in our GUI (includeControls && Conf.getBoolean(Const.CONF_COVERS_MIRROW_COVER)) // should we mirror in fullscreen mode || (!includeControls && Conf.getBoolean(Const.CONF_COVERS_MIRROW_COVER_FS_MODE))) { icon = new ImageIcon(UtilGUI.get3dImage(img)); } else { icon = new ImageIcon(img); } } if (icon.getIconHeight() == 0 || icon.getIconWidth() == 0) { throw new JajukException(0, "Wrong picture, size is null"); } } catch (final FileNotFoundException e) { // do not display a stacktrace for FileNotfound as we expect this in cases // where the picture is gone on the net Log.warn("Cover image not found at URL: " + (cover == null ? "<null>" : cover.getURL().toString())); throw new JajukException(0, e); } catch (final UnknownHostException e) { // do not display a stacktrace for HostNotFound as we expect this in cases // where the whole server is gone on the net Log.warn("Cover image not found at URL: " + (cover == null ? "<null>" : cover.getURL().toString())); throw new JajukException(0, e); } catch (final IOException e) { // this cover cannot be loaded Log.error(e); throw new JajukException(0, e); } catch (final InterruptedException e) { // this cover cannot be loaded Log.error(e); throw new JajukException(0, e); } // We apply a 90% of space availability to avoid image cut-offs (see #1283) final int iDisplayAreaHeight = (int) (0.9f * CoverView.this.getHeight() - 30); final int iDisplayAreaWidth = (int) (0.9f * CoverView.this.getWidth() - 10); // check minimum sizes if ((iDisplayAreaHeight < 1) || (iDisplayAreaWidth < 1)) { return; } int iNewWidth; int iNewHeight; if (iDisplayAreaHeight > iDisplayAreaWidth) { // Width is smaller than height : try to optimize height iNewHeight = iDisplayAreaHeight; // take all possible height // we check now if width will be visible entirely with optimized // height final float fHeightRatio = (float) iNewHeight / icon.getIconHeight(); if (icon.getIconWidth() * fHeightRatio <= iDisplayAreaWidth) { iNewWidth = (int) (icon.getIconWidth() * fHeightRatio); } else { // no? so we optimize width iNewWidth = iDisplayAreaWidth; iNewHeight = (int) (icon.getIconHeight() * ((float) iNewWidth / icon.getIconWidth())); } } else { // Height is smaller or equal than width : try to optimize width iNewWidth = iDisplayAreaWidth; // take all possible width // we check now if height will be visible entirely with // optimized width final float fWidthRatio = (float) iNewWidth / icon.getIconWidth(); if (icon.getIconHeight() * fWidthRatio <= iDisplayAreaHeight) { iNewHeight = (int) (icon.getIconHeight() * fWidthRatio); } else { // no? so we optimize width iNewHeight = iDisplayAreaHeight; iNewWidth = (int) (icon.getIconWidth() * ((float) iNewHeight / icon.getIconHeight())); } } // Note that at this point, the image is fully loaded (done in the ImageIcon constructor) ii = UtilGUI.getResizedImage(icon, iNewWidth, iNewHeight); // Free memory of source image, removing this causes severe memory leaks ! (tested) icon.getImage().flush(); }
From source file:org.projectforge.core.CreateImageDimensions.java
public static void main(final String[] args) throws IOException { log.info("Create dimension file of all webapp images."); final List<ImageDimension> dimensions = new ArrayList<ImageDimension>(); for (final String subDir : SUB_DIRS) { final String path = PATH + subDir; final Collection<File> files = FileUtils.listFiles(new File(path), IMAGE_SUFFIXES, true); final File absolutePathFile = new File(PATH); final String absolutePath = absolutePathFile.getAbsolutePath(); for (final File file : files) { final Image image = Toolkit.getDefaultToolkit().getImage(file.getAbsolutePath()); final ImageIcon icon = new ImageIcon(image); final String filename = file.getAbsolutePath().substring(absolutePath.length() + 1); final ImageDimension dimension = new ImageDimension(filename, icon.getIconWidth(), icon.getIconHeight()); dimensions.add(dimension);/*from w w w. j av a 2s .c om*/ } } final FileWriter writer = new FileWriter(DIMENSION_FILE); final XmlObjectWriter xmlWriter = new XmlObjectWriter(); final AliasMap aliasMap = new AliasMap(); aliasMap.put(ArrayList.class, "images"); xmlWriter.setAliasMap(aliasMap); final String xml = xmlWriter.writeToXml(dimensions, true); writer.append(XmlHelper.XML_HEADER); writer.append(xml); IOUtils.closeQuietly(writer); log.info("Creation of dimension file done: " + DIMENSION_FILE); }
From source file:org.richie.codeGen.ui.GenAndPreviewUI.java
/** * ??//from ww w . jav a 2 s . c om * * @param fileName * @param fileContent */ private void addPreviewTablePanel(String fileName, String fileContent) { final JScrollPane content = new JScrollPane(); JTextArea viewTextArea = new JTextArea(); viewTextArea.setText(fileContent); content.setViewportView(viewTextArea); JPanel tab = new JPanel(); tab.setOpaque(false); JLabel tabLabel = new JLabel(fileName); ImageIcon closeXIcon = new ImageIcon(ClassLoader.getSystemResource("resources/images/close.gif")); JButton tabCloseButton = new JButton(closeXIcon); tabCloseButton.setToolTipText("close"); tabCloseButton.setBorder(null); tabCloseButton.setContentAreaFilled(false); tabCloseButton.setPreferredSize(new Dimension(closeXIcon.getIconWidth(), closeXIcon.getIconHeight())); tabCloseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int closeTabNumber = mainPanel.indexOfComponent(content); mainPanel.removeTabAt(closeTabNumber); } }); tab.add(tabLabel, BorderLayout.WEST); tab.add(tabCloseButton, BorderLayout.EAST); mainPanel.addTab(null, content); mainPanel.setTabComponentAt(mainPanel.getTabCount() - 1, tab); mainPanel.setSelectedComponent(content); }
From source file:org.vulpe.view.tags.Functions.java
/** * This method takes in an image as a byte array (currently supports GIF, * JPG, PNG and possibly other formats) and resizes it to have a width no * greater than the pMaxWidth parameter in pixels. It converts the image to * a standard quality JPG and returns the byte array of that JPG image. * * @param imageData/*from www . ja v a 2 s.com*/ * the image data. * @param maxWidth * the max width in pixels, 0 means do not scale. * @return the resized JPG image. * @throws IOException * if the image could not be manipulated correctly. */ public static byte[] resizeImageAsJPG(final byte[] imageData, final int maxWidth) throws IOException { // Create an ImageIcon from the image data final ImageIcon imageIcon = new ImageIcon(imageData); int width = imageIcon.getIconWidth(); if (width == maxWidth) { return imageData; } int height = imageIcon.getIconHeight(); LOG.debug("imageIcon width: " + width + " height: " + height); // If the image is larger than the max width, we need to resize it if (maxWidth > 0 && width > maxWidth) { // Determine the shrink ratio final double ratio = (double) maxWidth / imageIcon.getIconWidth(); LOG.debug("resize ratio: " + ratio); height = (int) (imageIcon.getIconHeight() * ratio); width = maxWidth; LOG.debug("imageIcon post scale width: " + width + " height: " + height); } // Create a new empty image buffer to "draw" the resized image into final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Create a Graphics object to do the "drawing" final Graphics2D g2d = bufferedImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); // Draw the resized image g2d.drawImage(imageIcon.getImage(), 0, 0, width, height, null); g2d.dispose(); // Now our buffered image is ready // Encode it as a JPEG final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos); encoder.encode(bufferedImage); return baos.toByteArray(); }
From source file:org.wings.SImageIcon.java
public SImageIcon(ImageIcon image, String mimeType) { if (image == null) throw new IllegalArgumentException("SImageIcon needs an Argument that's not null"); img = image;// w ww . j a v a 2 s . c o m url = new SimpleURL(SessionManager.getSession().getExternalizeManager().externalize(image, mimeType)); setIconWidth(image.getIconWidth()); setIconHeight(image.getIconHeight()); }