List of usage examples for java.awt.image BufferedImage getWidth
public int getWidth()
From source file:edu.csun.ecs.cs.multitouchj.ui.control.Canvas.java
protected Image getBufferedImageImage(BufferedImage bufferedImage) { byte[] bytes = (byte[]) bufferedImage.getRaster().getDataElements(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), null); return new Image(bufferedImage.getWidth(), bufferedImage.getHeight(), bytes, true); }
From source file:com.funambol.foundation.util.MediaUtils.java
/** * Rotates given buffered image by given amount of degree. * The valid degree values are 0, 90, 180, 270. * If the image is a jpg, the rotation is lossless, exif data are preserved * and image size is almost the same./* w w w . j a v a 2 s . com*/ * * @param bufImage the buffered image * @param degree amount of degree to apply * @return a buffered image containing rotated image data * @throws PicturesException if amount of degree is invalid or if an * IOException occurs */ private static BufferedImage rotateImage(BufferedImage bufImage, int degree) throws FileDataObjecyUtilsException { degree = degree % 360; int h; int w; switch (degree) { case 0: case 180: h = bufImage.getHeight(); w = bufImage.getWidth(); break; case 90: case 270: h = bufImage.getWidth(); w = bufImage.getHeight(); break; default: throw new FileDataObjecyUtilsException( "Error rotating image since the '" + degree + "' degree value is unsupported"); } BufferedImage out = null; int bufImageType = bufImage.getType(); if (BufferedImage.TYPE_BYTE_INDEXED == bufImageType || BufferedImage.TYPE_BYTE_BINARY == bufImageType) { IndexColorModel model = (IndexColorModel) bufImage.getColorModel(); out = new BufferedImage(w, h, bufImage.getType(), model); } else if (BufferedImage.TYPE_CUSTOM == bufImageType) { // we don't know what type of image it can be // there's a bug in some VM that cause some PNG images to have // type custom: this should take care of this issue //check if we need to have alpha channel boolean alpha = bufImage.getTransparency() != BufferedImage.OPAQUE; if (alpha) { // TYPE_INT_ARGB_PRE gives you smaller output images // than TYPE_INT_ARGB out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE); } else { out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); } } else { out = new BufferedImage(w, h, bufImage.getType()); } Graphics2D g2d = out.createGraphics(); Map renderingHints = new HashMap(); renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.setRenderingHints(renderingHints); g2d.rotate(Math.toRadians(degree)); switch (degree) { case 90: g2d.translate(0, -w); break; case 180: g2d.translate(-w, -h); break; case 270: g2d.translate(-h, 0); break; } g2d.drawImage(bufImage, null, 0, 0); g2d.dispose(); return out; }
From source file:com.webpagebytes.cms.DefaultImageProcessor.java
public boolean resizeImage(WPBFileStorage cloudStorage, WPBFilePath cloudFile, int desiredSize, String outputFormat, OutputStream os) throws WPBException { InputStream is = null;//from w ww . j a va 2 s. co m try { //get the file content WPBFileInfo fileInfo = cloudStorage.getFileInfo(cloudFile); String type = fileInfo.getContentType().toLowerCase(); if (!type.startsWith("image")) { return false; } is = cloudStorage.getFileContent(cloudFile); BufferedImage bufImg = ImageIO.read(is); Dimension<Integer> newSize = getResizeSize(bufImg.getWidth(), bufImg.getHeight(), desiredSize); BufferedImage bdest = new BufferedImage(newSize.getX(), newSize.getY(), BufferedImage.TYPE_INT_RGB); Graphics2D g = bdest.createGraphics(); Dimension<Double> scale = getResizeScale(bufImg.getHeight(), bufImg.getWidth(), desiredSize); AffineTransform at = AffineTransform.getScaleInstance(scale.getX(), scale.getY()); g.drawRenderedImage(bufImg, at); ImageIO.write(bdest, outputFormat, os); return true; } catch (Exception e) { throw new WPBException("Cannot resize image ", e); } finally { IOUtils.closeQuietly(is); } }
From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadThumbnailer.java
private String imageSize(BufferedImage image) { return image.getWidth() + " by " + image.getHeight(); }
From source file:org.shredzone.cilla.service.resource.ImageTools.java
/** * Analyzes the dimension of the given image {@link DataSource}. * * @param src//w w w .j a v a 2 s .c om * {@link DataSource} to analyze * @return image dimensions */ public Dimension analyzeDimension(DataSource src) throws CillaServiceException { try { BufferedImage image = ImageIO.read(src.getInputStream()); return (image != null ? new Dimension(image.getWidth(), image.getHeight()) : null); } catch (IOException ex) { throw new CillaServiceException(ex); } }
From source file:ImageOpByRomain.java
/** * <p>/* www . ja v a 2 s . com*/ * Return a new compatible image that contains a copy of the specified image. * This method ensures an image is compatible with the hardware, and therefore * optimized for fast blitting operations. * </p> * * @see #createCompatibleImage(java.awt.image.BufferedImage) * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int) * @see #createCompatibleImage(int, int) * @see #createTranslucentCompatibleImage(int, int) * @see #loadCompatibleImage(java.net.URL) * @param image * the image to copy into a new compatible image * @return a new compatible copy, with the same width and height and * transparency and content, of <code>image</code> */ public static BufferedImage toCompatibleImage(BufferedImage image) { if (image.getColorModel().equals(CONFIGURATION.getColorModel())) { return image; } BufferedImage compatibleImage = CONFIGURATION.createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency()); Graphics g = compatibleImage.getGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return compatibleImage; }
From source file:dk.netdesign.common.osgi.config.osgi.OCD.java
public BufferedImage scaleImage(BufferedImage img, int width, int height, Color background) { int imgWidth = img.getWidth(); int imgHeight = img.getHeight(); if (imgWidth * height < imgHeight * width) { width = imgWidth * height / imgHeight; } else {/*from w w w .j a va 2 s . co m*/ height = imgHeight * width / imgWidth; } BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = newImage.createGraphics(); try { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setBackground(background); g.clearRect(0, 0, width, height); g.drawImage(img, 0, 0, width, height, null); } finally { g.dispose(); } return newImage; }
From source file:org.lightadmin.core.web.util.ImageResourceControllerSupport.java
protected BufferedImage resizeImage(BufferedImage sourceImage, int width, int height) { final int currentWidth = sourceImage.getWidth(); final int currentHeight = sourceImage.getHeight(); float ratio = ((float) currentHeight / (float) currentWidth); if (width <= 0) { width = (int) (height / ratio); }//w w w . j a va 2 s. c om if (height <= 0) { height = (int) (width * ratio); } return resize(sourceImage, SPEED, AUTOMATIC, width, height, OP_ANTIALIAS); }
From source file:com.trailmagic.image.util.FixDimensions.java
public void fixDimensions(final String ownerName, final String rollName) { m_hibernateTemplate.execute(new HibernateCallback() { public Object doInHibernate(Session session) { try { User owner = userRepository.getByScreenName(ownerName); ImageGroup roll = imageGroupRepository.getRollByOwnerAndName(owner, rollName); for (ImageFrame frame : roll.getFrames()) { Image image = frame.getImage(); for (ImageManifestation mf : image.getManifestations()) { HeavyImageManifestation heavyMf = m_imageManifestationFactory.getHeavyById(mf.getId()); BufferedImage bi = ImageIO.read(heavyMf.getData().getBinaryStream()); mf.setHeight(bi.getHeight()); mf.setWidth(bi.getWidth()); session.evict(heavyMf); }/*w w w . j a v a 2 s. c o m*/ } } catch (Exception e) { s_log.error("Error fixing permissions", e); } return null; } }); }
From source file:net.d53dev.dslfy.web.service.GifService.java
public BufferedImage convertRGBAToGIF(BufferedImage src, int transColor) { BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); Graphics g = dst.getGraphics(); g.setColor(new Color(transColor)); g.fillRect(0, 0, dst.getWidth(), dst.getHeight()); {//from w w w .j a v a 2s . c o m IndexColorModel indexedModel = (IndexColorModel) dst.getColorModel(); WritableRaster raster = dst.getRaster(); int sample = raster.getSample(0, 0, 0); int size = indexedModel.getMapSize(); byte[] rr = new byte[size]; byte[] gg = new byte[size]; byte[] bb = new byte[size]; indexedModel.getReds(rr); indexedModel.getGreens(gg); indexedModel.getBlues(bb); IndexColorModel newModel = new IndexColorModel(8, size, rr, gg, bb, sample); dst = new BufferedImage(newModel, raster, dst.isAlphaPremultiplied(), null); } dst.createGraphics().drawImage(src, 0, 0, null); return dst; }