List of usage examples for java.awt.image BufferedImage createGraphics
public Graphics2D createGraphics()
From source file:Main.java
public void repaint(BufferedImage orig, BufferedImage copy) { Graphics2D g = copy.createGraphics(); g.drawImage(orig, 0, 0, null);//from w w w .ja v a 2 s .c om g.setColor(Color.RED); if (captureRect == null) { return; } g.draw(captureRect); g.setColor(new Color(25, 25, 23, 10)); g.fill(captureRect); g.dispose(); }
From source file:com.funambol.foundation.util.MediaUtils.java
/** * Creates the thumbnail.//from w w w. j a v a 2 s.c om * * @param imageFile the image file * @param thumbFile the empty thumbnail file * @param thumbX the width of the thumbnail * @param thumbY the height of the thumbnail * @param imageName the image file name with extension * @param tolerance the percentage of tolerance before creating a thumbnail * @return true is the thumbnail has been created, false otherwise * @throws IOException if an error occurs */ private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName, double tolerance) throws IOException { FileInputStream fileis = null; ImageInputStream imageis = null; Iterator readers = null; try { readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1)); if (readers == null || (!readers.hasNext())) { throw new IOException("File not supported"); } ImageReader reader = (ImageReader) readers.next(); fileis = new FileInputStream(imageFile); imageis = ImageIO.createImageInputStream(fileis); reader.setInput(imageis, true); // Determines thumbnail height, width and quality int thumbWidth = thumbX; int thumbHeight = thumbY; double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = reader.getWidth(0); int imageHeight = reader.getHeight(0); // // Don't create the thumbnail if the original file is smaller // than required size increased by % tolerance // if (imageWidth <= (thumbWidth * (1 + tolerance / 100)) && imageHeight <= (thumbHeight * (1 + tolerance / 100))) { return false; } double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(3, 3, 0, 0); BufferedImage bi = reader.read(0, param); Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH); BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null); FileOutputStream fileOutputStream = new FileOutputStream(thumbFile); ImageIO.write(thumbImage, "jpg", fileOutputStream); thumb.flush(); thumbImage.flush(); fileOutputStream.flush(); fileOutputStream.close(); graphics2D.dispose(); } finally { if (fileis != null) { fileis.close(); } if (imageis != null) { imageis.close(); } } return true; }
From source file:edu.stanford.epad.common.pixelmed.TIFFMasksToDSOConverter.java
public static BufferedImage convertRGBAToIndexed(BufferedImage src) { BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); dest.createGraphics().drawImage(src, 0, 0, null); return dest;/* w ww. ja va 2 s . c om*/ }
From source file:com.piaoyou.util.ImageUtil.java
/** * This method write the image to a stream. * It auto detect the image is Image or BufferedImage. * This method close the output stream before it return. * * @param image Image/*www .j a va 2s . c om*/ * @param outputStream OutputStream * @throws IOException */ public static void writeJpegImage_Sun(Image image, OutputStream outputStream) throws IOException { if (outputStream == null) { return; } try { BufferedImage bufferedImage = null; if (image instanceof BufferedImage) { bufferedImage = (BufferedImage) image; } else { // 30% cpu resource bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); // 7.5 cpu Graphics2D g = bufferedImage.createGraphics(); // 50% cpu g.drawImage(image, 0, 0, null); g.dispose(); // free resource } // write it to disk // 12% cpu //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream); //encoder.encode(bufferedImage); ImageIO.write(bufferedImage, "jpeg", outputStream); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
From source file:com.sketchy.image.ImageProcessingThread.java
public static BufferedImage rotateImage(BufferedImage image, RotateOption rotateOption) { if (rotateOption == RotateOption.ROTATE_NONE) return image; int degrees = 0; int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); BufferedImage rotatedImage = null; if (rotateOption == RotateOption.ROTATE_90) { degrees = 90;/*w ww .j ava 2s .c o m*/ rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB); } else if (rotateOption == RotateOption.ROTATE_180) { degrees = 180; rotatedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB); } else if (rotateOption == RotateOption.ROTATE_270) { degrees = 270; rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB); } Graphics2D g = rotatedImage.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.rotate(Math.toRadians(degrees), 0, 0); if (degrees == 90) { g.drawImage(image, null, 0, -imageHeight); } else if (degrees == 180) { g.drawImage(image, null, -imageWidth, -imageHeight); } else if (degrees == 270) { g.drawImage(image, null, -imageWidth, 0); } g.dispose(); return rotatedImage; }
From source file:BasicShapes.java
public void paint(Graphics g) { BufferedImage bimage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED); Graphics2D g2d = bimage.createGraphics(); Color transparent = new Color(0, 0, 0, 0); g2d.setColor(transparent);/*w ww.j av a2s . c o m*/ g2d.setComposite(AlphaComposite.Src); g2d.fill(new Rectangle2D.Float(20, 20, 100, 20)); g2d.dispose(); }
From source file:ImageOpByRomain.java
/** * <p>/*from w ww.j ava2 s .c o m*/ * Returns a thumbnail of a source image. * </p> * <p> * This method favors speed over quality. When the new size is less than half * the longest dimension of the source image, * {@link #createThumbnail(BufferedImage, int)} or * {@link #createThumbnail(BufferedImage, int, int)} should be used instead to * ensure the quality of the result without sacrificing too much performance. * </p> * * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image * the source image * @param newWidth * the width of the thumbnail * @param newHeight * the height of the thumbnail * @return a new compatible <code>BufferedImage</code> containing a * thumbnail of <code>image</code> * @throws IllegalArgumentException * if <code>newWidth</code> is larger than the width of * <code>image</code> or if code>newHeight</code> is larger * than the height of <code>image</code> or if one of the * dimensions is <= 0 */ public static BufferedImage createThumbnailFast(BufferedImage image, int newWidth, int newHeight) { if (newWidth >= image.getWidth() || newHeight >= image.getHeight()) { throw new IllegalArgumentException( "newWidth and newHeight cannot" + " be greater than the image" + " dimensions"); } else if (newWidth <= 0 || newHeight <= 0) { throw new IllegalArgumentException("newWidth and newHeight must" + " be greater than 0"); } BufferedImage temp = createCompatibleImage(image, newWidth, newHeight); Graphics2D g2 = temp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null); g2.dispose(); return temp; }
From source file:Main.java
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f); BufferedImage buffImg = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB); Graphics2D gbi = buffImg.createGraphics(); gbi.setPaint(Color.red);/*www . jav a2 s .c o m*/ gbi.fillRect(0, 0, 40, 40); gbi.setComposite(ac); gbi.setPaint(Color.green); gbi.fillRect(5, 5, 40, 40); g2d.drawImage(buffImg, 20, 20, null); }
From source file:CompositingDST_ATOP.java
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OUT, 0.5f); BufferedImage buffImg = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB); Graphics2D gbi = buffImg.createGraphics(); gbi.setPaint(Color.red);//from www. java 2 s .c o m gbi.fillRect(0, 0, 40, 40); gbi.setComposite(ac); gbi.setPaint(Color.green); gbi.fillRect(5, 5, 40, 40); g2d.drawImage(buffImg, 20, 20, null); }
From source file:CompositingDST_ATOP.java
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 0.5f); BufferedImage buffImg = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB); Graphics2D gbi = buffImg.createGraphics(); gbi.setPaint(Color.red);/*w w w. ja va2s . c om*/ gbi.fillRect(0, 0, 40, 40); gbi.setComposite(ac); gbi.setPaint(Color.green); gbi.fillRect(5, 5, 40, 40); g2d.drawImage(buffImg, 20, 20, null); }