List of usage examples for java.awt Image getHeight
public abstract int getHeight(ImageObserver observer);
From source file:ala.soils2sat.DrawingUtils.java
public static Rectangle resizeImage(Image img, int targetWidth, int targetHeight) { int srcHeight = img.getHeight(null); int srcWidth = img.getWidth(null); return resizeImage(srcWidth, srcHeight, targetWidth, targetHeight); }
From source file:de.laures.cewolf.util.ImageHelper.java
public static BufferedImage loadBufferedImage(String fileName) { Image image = loadImage(fileName); if (image instanceof BufferedImage) { return (BufferedImage) image; }//from w w w .j a v a2s . com /* final boolean hasAlpha = hasAlpha(image); int transparency = Transparency.OPAQUE; if (hasAlpha) { transparency = Transparency.BITMASK; }*/ int width = (int) Math.max(1.0, image.getWidth(null)); int height = (int) Math.max(1.0, image.getHeight(null)); // BufferedImage bimage = GRAPHICS_CONV.createCompatibleImage(width, height, transparency); BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics g = bimage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return bimage; }
From source file:net.sf.webphotos.tools.Thumbnail.java
private static boolean save(Image thumbnail, String nmArquivo) { try {/*from w ww .j av a 2 s . co m*/ // This code ensures that all the // pixels in the image are loaded. Image temp = new ImageIcon(thumbnail).getImage(); // Create the buffered image. BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB); // Copy image to buffered image. Graphics g = bufferedImage.createGraphics(); // Clear background and paint the image. g.setColor(Color.white); g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); g.drawImage(temp, 0, 0, null); g.dispose(); // write the jpeg to a file File file = new File(nmArquivo); // Recria o arquivo se existir if (file.exists()) { Util.out.println("Redefinindo a Imagem: " + nmArquivo); file.delete(); file = new File(nmArquivo); } // encodes image as a JPEG file ImageIO.write(bufferedImage, IMAGE_FORMAT, file); return true; } catch (IOException ioex) { ioex.printStackTrace(Util.err); return false; } }
From source file:org.jfree.experimental.swt.SWTUtils.java
/** * Converts an AWT image to SWT./* w ww . j ava 2 s .c om*/ * * @param image the image (<code>null</code> not permitted). * * @return Image data. */ public static ImageData convertAWTImageToSWT(Image image) { ParamChecks.nullNotPermitted(image, "image"); int w = image.getWidth(null); int h = image.getHeight(null); if (w == -1 || h == -1) { return null; } BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return convertToSWT(bi); }
From source file:rega.genotype.ui.util.GenotypeLib.java
public static void scalePNG(File in, File out, double perc) throws IOException { Image i = ImageIO.read(in); Image resizedImage = null;/* w w w . ja va 2 s. c o m*/ int newWidth = (int) (i.getWidth(null) * perc / 100.0); int newHeight = (int) (i.getHeight(null) * perc / 100.0); resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); // This code ensures that all the pixels in the image are loaded. Image temp = new ImageIcon(resizedImage).getImage(); // Create the buffered image. BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB); // Copy image to buffered image. Graphics g = bufferedImage.createGraphics(); // Clear background and paint the image. g.setColor(Color.white); g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); g.drawImage(temp, 0, 0, null); g.dispose(); // Soften. float softenFactor = 0.05f; float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 }; Kernel kernel = new Kernel(3, 3, softenArray); ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); bufferedImage = cOp.filter(bufferedImage, null); ImageIO.write(bufferedImage, "png", out); }
From source file:org.mili.core.graphics.GraphicsUtil.java
/** * Points an image at coordinates x/y in defined image. * * @param og original image.//from w ww.j av a 2 s . c o m * @param i image to point. * @param x coordinate x >= 0. * @param y coordinate y >= 0. * @return new image included image to point. */ public static Image pointImage(Image og, Image i, int x, int y) { Validate.notNull(og, "original image cannot be null!"); Validate.notNull(i, "image to point cannot be null!"); Validate.isTrue(x >= 0, "x can only be >= 0!"); Validate.isTrue(y >= 0, "y can only be >= 0!"); BufferedImage bi = new BufferedImage(og.getWidth(null), og.getHeight(null), BufferedImage.TYPE_INT_RGB); og = new ImageIcon(og).getImage(); i = new ImageIcon(i).getImage(); Graphics2D g = (Graphics2D) bi.createGraphics(); g.drawImage(og, 0, 0, null); g.drawImage(i, x, y, null); g.dispose(); return bi; }
From source file:org.tolven.security.bean.DocProtectionBean.java
/** * Create a JPEG thumbnail of the underlying image and encode it to the output stream provided. * The aspect ratio of the underlying image is retained. As a result, the thumbnail is scaled to fit in * the specified rectangle. Whitespace is added if the image does not match the aspect ratio of the rectangle. * @param targetWidth// w ww . j ava 2 s .c o m * @param targetHeight * @param stream */ static public void streamJPEGThumbnail(byte[] unencryptedContent, int targetWidth, int targetHeight, OutputStream stream) { Image sourceImage = new ImageIcon(Toolkit.getDefaultToolkit().createImage(unencryptedContent)).getImage(); float hscale = ((float) targetWidth) / ((float) sourceImage.getWidth(null)); float vscale = ((float) targetHeight) / ((float) sourceImage.getHeight(null)); float scale = 1.0f; if (hscale < scale) scale = hscale; if (vscale < scale) scale = vscale; int newWidth = (int) (sourceImage.getWidth(null) * scale); int newHeight = (int) (sourceImage.getHeight(null) * scale); Image resizedImage = scaleImage(sourceImage, newWidth, newHeight); BufferedImage bufferedImage = toBufferedImage(resizedImage, targetWidth, targetHeight); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(stream); try { encoder.encode(bufferedImage); } catch (Exception ex) { throw new RuntimeException("Could not encode bufferedImage", ex); } }
From source file:net.rptools.lib.image.ImageUtil.java
public static BufferedImage createCompatibleImage(Image img, Map<String, Object> hints) { if (img == null) { return null; }/* ww w .ja va 2 s. c o m*/ return createCompatibleImage(img, img.getWidth(null), img.getHeight(null), hints); }
From source file:com.t3.image.ImageUtil.java
public static BufferedImage createCompatibleImage(Image img, Map<String, Object> hints) { if (img == null) { return null; }/* w ww .j a v a2 s . co m*/ return createCompatibleImage(img, img.getWidth(null), img.getHeight(null), hints); }
From source file:net.sf.webphotos.tools.Thumbnail.java
/** * Cria thumbs para as imagens. Testa se j existem valores setados para o * thumb, se no existir chama o mtodo/*www . jav a2 s.co m*/ * {@link net.sf.webphotos.Thumbnail#inicializar() inicializar} para setar * seus valores. Abre o arquivo de imagem passado como parmetro e checa se * uma foto vlida. Obtm o tamanho original da imagem, checa se est no * formato paisagem ou retrato e utiliza o mtodo * {@link java.awt.Image#getScaledInstance(int,int,int) getScaledInstance} * para calcular os thumbs. Ao final, salva as imagens. * * @param caminhoCompletoImagem Caminho da imagem. */ public static void makeThumbs(String caminhoCompletoImagem) { String diretorio, arquivo; if (t1 == 0) { inicializar(); } try { File f = new File(caminhoCompletoImagem); if (!f.isFile() || !f.canRead()) { Util.err.println("[Thumbnail.makeThumbs]/ERRO: Erro no caminho do arquivo " + caminhoCompletoImagem + " incorreto"); return; } // Foto em alta corrompida if (getFormatName(f) == null) { Util.err.println("[Thumbnail.makeThumbs]/ERRO: Foto Corrompida"); return; } else { Util.out.println("[Thumbnail.makeThumbs]/INFO: Foto Ok!"); } diretorio = f.getParent(); arquivo = f.getName(); ImageIcon ii = new ImageIcon(f.getCanonicalPath()); Image i = ii.getImage(); Image tumb1, tumb2, tumb3, tumb4; // obtm o tamanho da imagem original int iWidth = i.getWidth(null); int iHeight = i.getHeight(null); //int w, h; if (iWidth > iHeight) { tumb1 = i.getScaledInstance(t1, (t1 * iHeight) / iWidth, Image.SCALE_SMOOTH); tumb2 = i.getScaledInstance(t2, (t2 * iHeight) / iWidth, Image.SCALE_SMOOTH); tumb3 = i.getScaledInstance(t3, (t3 * iHeight) / iWidth, Image.SCALE_SMOOTH); tumb4 = i.getScaledInstance(t4, (t4 * iHeight) / iWidth, Image.SCALE_SMOOTH); //w = t4; //h = (t4 * iHeight) / iWidth; } else { tumb1 = i.getScaledInstance((t1 * iWidth) / iHeight, t1, Image.SCALE_SMOOTH); tumb2 = i.getScaledInstance((t2 * iWidth) / iHeight, t2, Image.SCALE_SMOOTH); tumb3 = i.getScaledInstance((t3 * iWidth) / iHeight, t3, Image.SCALE_SMOOTH); tumb4 = i.getScaledInstance((t4 * iWidth) / iHeight, t4, Image.SCALE_SMOOTH); //w = (t4 * iWidth) / iHeight; //h = t4; } tumb4 = estampar(tumb4); Util.log("Salvando Imagens"); save(tumb1, diretorio + File.separator + "_a" + arquivo); save(tumb2, diretorio + File.separator + "_b" + arquivo); save(tumb3, diretorio + File.separator + "_c" + arquivo); save(tumb4, diretorio + File.separator + "_d" + arquivo); } catch (Exception e) { Util.err.println("[Thumbnail.makeThumbs]/ERRO: Inesperado - " + e.getMessage()); e.printStackTrace(Util.err); } }