List of usage examples for java.awt Graphics2D clearRect
public abstract void clearRect(int x, int y, int width, int height);
From source file:Main.java
public static 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 ww w. j av a 2 s. c o m*/ height = imgHeight * width / imgWidth; } BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 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:com.mikenimer.familydam.services.photos.ThumbnailService.java
/** * using the metadata orientation transformation information rotate the image. * @param image/*w w w .ja v a 2 s.c o m*/ * @param transform * @return * @throws Exception */ public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception { AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); BufferedImage destinationImage = op.createCompatibleDestImage(image, (image.getType() == BufferedImage.TYPE_BYTE_GRAY) ? image.getColorModel() : null); Graphics2D g = destinationImage.createGraphics(); g.setBackground(Color.WHITE); g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight()); destinationImage = op.filter(image, destinationImage); return destinationImage; }
From source file:edu.umn.cs.spatialHadoop.nasa.MultiHDFPlot.java
/** * Draws a scale used with the heat map// ww w .j av a2 s. c o m * @param output * @param valueRange * @param width * @param height * @throws IOException */ private static void drawVerticalScale(Path output, double min, double max, int width, int height, OperationsParams params) throws IOException { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); g.setBackground(Color.BLACK); g.clearRect(0, 0, width, height); // fix this part to work according to color1, color2 and gradient type HDFPlot.HDFRasterizer gradient = new HDFPlot.HDFRasterizer(); gradient.configure(params); HDFRasterLayer gradientLayer = (HDFRasterLayer) gradient.createCanvas(0, 0, new Rectangle()); for (int y = 0; y < height; y++) { Color color = gradientLayer.calculateColor(height - y, 0, height); g.setColor(color); g.drawRect(width * 3 / 4, y, width / 4, 1); } int fontSize = 24; g.setFont(new Font("Arial", Font.BOLD, fontSize)); double step = (max - min) * fontSize * 5 / height; step = (int) (Math.pow(10.0, Math.round(Math.log10(step)))); double min_value = Math.floor(min / step) * step; double max_value = Math.floor(max / step) * step; g.setColor(Color.WHITE); for (double value = min_value; value <= max_value; value += step) { double y = ((value - min) + (max - value) * (height - fontSize)) / (max - min); g.drawString(String.valueOf((int) value), 5, (int) y); } g.dispose(); FileSystem fs = output.getFileSystem(new Configuration()); FSDataOutputStream outStream = fs.create(output, true); ImageIO.write(image, "png", outStream); outStream.close(); }
From source file:edu.umn.cs.spatialHadoop.nasa.MultiHDFPlot.java
/** * Draws a scale used with the heat map/*from ww w.j a va 2 s . c om*/ * @param output * @param valueRange * @param width * @param height * @throws IOException */ private static void drawHorizontalScale(Path output, double min, double max, int width, int height, OperationsParams params) throws IOException { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); g.setBackground(Color.BLACK); g.clearRect(0, 0, width, height); int fontSize = 24; // fix this part to work according to color1, color2 and gradient type HDFPlot.HDFRasterizer gradient = new HDFPlot.HDFRasterizer(); gradient.configure(params); HDFRasterLayer gradientLayer = (HDFRasterLayer) gradient.createCanvas(0, 0, new Rectangle()); for (int x = 0; x < width; x++) { Color color = gradientLayer.calculateColor(x, 0, width); g.setColor(color); g.drawRect(x, height - (fontSize - 5), 1, fontSize - 5); } g.setFont(new Font("Arial", Font.BOLD, fontSize)); double step = (max - min) * fontSize * 5 / width; step = (int) (Math.pow(10.0, Math.round(Math.log10(step)))); double min_value = Math.floor(min / step) * step; double max_value = Math.floor(max / step) * step; g.setColor(Color.WHITE); for (double value = min_value; value <= max_value; value += step) { double x = ((value - min) * (width - fontSize) + (max - value)) / (max - min); g.drawString(String.valueOf((int) value), (int) x, fontSize); } g.dispose(); FileSystem fs = output.getFileSystem(new Configuration()); FSDataOutputStream outStream = fs.create(output, true); ImageIO.write(image, "png", outStream); outStream.close(); }
From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
private static void enforceBorderColors(BufferedImage inputImage) { Graphics2D g = inputImage.createGraphics(); g.setBackground(new Color(0, 0, 0, 0)); g.clearRect(1, 1, inputImage.getWidth() - 2, inputImage.getHeight() - 2); g.dispose();/*from ww w.j av a 2s . co m*/ int w = inputImage.getWidth(); int h = inputImage.getHeight(); int[] rgb = new int[w * h]; inputImage.getRGB(0, 0, w, h, rgb, 0, w); for (int i = 0; i < rgb.length; i++) { if ((0xff000000 & rgb[i]) != 0) { rgb[i] = 0xff000000; } } inputImage.setRGB(0, 0, w, h, rgb, 0, w); }
From source file:iqq.util.ImageUtil.java
/** * /*from ww w.j av a 2s .c o m*/ * * @param srcFile ? * @param destFile * @param destWidth * @param destHeight */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { if (type == Type.jdk) { try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); graphics2D.dispose(); FileOutputStream out = new FileOutputStream(destFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(destBufferedImage); param.setQuality((float) DEST_QUALITY / 100, false); encoder.setJPEGEncodeParam(param); encoder.encode(destBufferedImage); out.close(); } catch (IOException e) { e.printStackTrace(); } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:net.shopxx.util.ImageUtil.java
/** * /*www. j a va 2 s . c o m*/ * @param srcFile ? * @param destFile * @param destWidth * @param destHeight */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile); Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); graphics2D.dispose(); FileOutputStream out = new FileOutputStream(destFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(destBufferedImage); param.setQuality((float) DEST_QUALITY / 100, false); encoder.setJPEGEncodeParam(param); encoder.encode(destBufferedImage); out.close(); } catch (IOException e) { e.printStackTrace(); } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:net.semanticmetadata.lire.utils.FileUtils.java
public static void saveImageResultsToPng(String prefix, TopDocs hits, String queryImage, IndexReader ir) throws IOException { LinkedList<BufferedImage> results = new LinkedList<BufferedImage>(); int width = 0; for (int i = 0; i < Math.min(hits.scoreDocs.length, 10); i++) { // hits.score(i) // hits.doc(i).get("descriptorImageIdentifier") BufferedImage tmp = ImageIO .read(new FileInputStream(ir.document(hits.scoreDocs[i].doc).get("descriptorImageIdentifier"))); if (tmp.getHeight() > 200) { double factor = 200d / ((double) tmp.getHeight()); tmp = ImageUtils.scaleImage(tmp, (int) (tmp.getWidth() * factor), 200); }//from w w w . j a v a 2 s . co m width += tmp.getWidth() + 5; results.add(tmp); } BufferedImage result = new BufferedImage(width, 220, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) result.getGraphics(); g2.setColor(Color.black); g2.clearRect(0, 0, result.getWidth(), result.getHeight()); g2.setColor(Color.green); g2.setFont(Font.decode("\"Arial\", Font.BOLD, 12")); int offset = 0; int count = 0; for (Iterator<BufferedImage> iterator = results.iterator(); iterator.hasNext();) { BufferedImage next = iterator.next(); g2.drawImage(next, offset, 20, null); g2.drawString(hits.scoreDocs[count].score + "", offset + 5, 12); offset += next.getWidth() + 5; count++; } ImageIO.write(result, "PNG", new File(prefix + "_" + (System.currentTimeMillis() / 1000) + ".png")); }
From source file:net.shopxx.util.ImageUtil.java
/** * ?//w w w. j a v a 2s. c om * @param srcFile ? * @param destFile * @param watermarkFile ? * @param watermarkPosition ?? * @param alpha ?? */ public static void addWatermark(File srcFile, File destFile, File watermarkFile, WatermarkPosition watermarkPosition, int alpha) { Assert.notNull(srcFile); Assert.notNull(destFile); Assert.state(alpha >= 0); Assert.state(alpha <= 100); if (watermarkFile == null || !watermarkFile.exists() || watermarkPosition == null || watermarkPosition == WatermarkPosition.no) { return; } if (type == Type.jdk) { try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); BufferedImage destBufferedImage = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, srcWidth, srcHeight); graphics2D.drawImage(srcBufferedImage, 0, 0, null); graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha / 100.0F)); BufferedImage watermarkBufferedImage = ImageIO.read(watermarkFile); int watermarkImageWidth = watermarkBufferedImage.getWidth(); int watermarkImageHeight = watermarkBufferedImage.getHeight(); int x = srcWidth - watermarkImageWidth; int y = srcHeight - watermarkImageHeight; if (watermarkPosition == WatermarkPosition.topLeft) { x = 0; y = 0; } else if (watermarkPosition == WatermarkPosition.topRight) { x = srcWidth - watermarkImageWidth; y = 0; } else if (watermarkPosition == WatermarkPosition.center) { x = (srcWidth - watermarkImageWidth) / 2; y = (srcHeight - watermarkImageHeight) / 2; } else if (watermarkPosition == WatermarkPosition.bottomLeft) { x = 0; y = srcHeight - watermarkImageHeight; } else if (watermarkPosition == WatermarkPosition.bottomRight) { x = srcWidth - watermarkImageWidth; y = srcHeight - watermarkImageHeight; } graphics2D.drawImage(watermarkBufferedImage, x, y, watermarkImageWidth, watermarkImageHeight, null); graphics2D.dispose(); FileOutputStream out = new FileOutputStream(destFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(destBufferedImage); param.setQuality((float) DEST_QUALITY / 100, false); encoder.setJPEGEncodeParam(param); encoder.encode(destBufferedImage); out.close(); } catch (IOException e) { e.printStackTrace(); } } else { String gravity = "SouthEast"; if (watermarkPosition == WatermarkPosition.topLeft) { gravity = "NorthWest"; } else if (watermarkPosition == WatermarkPosition.topRight) { gravity = "NorthEast"; } else if (watermarkPosition == WatermarkPosition.center) { gravity = "Center"; } else if (watermarkPosition == WatermarkPosition.bottomLeft) { gravity = "SouthWest"; } else if (watermarkPosition == WatermarkPosition.bottomRight) { gravity = "SouthEast"; } IMOperation operation = new IMOperation(); operation.gravity(gravity); operation.dissolve(alpha); operation.quality((double) DEST_QUALITY); operation.addImage(watermarkFile.getPath()); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { CompositeCmd compositeCmd = new CompositeCmd(true); if (graphicsMagickPath != null) { compositeCmd.setSearchPath(graphicsMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { CompositeCmd compositeCmd = new CompositeCmd(false); if (imageMagickPath != null) { compositeCmd.setSearchPath(imageMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:iqq.util.ImageUtil.java
/** * ?/*from w w w. j a v a 2 s .c o m*/ * * @param srcFile ? * @param destFile * @param watermarkFile ? * @param watermarkPosition ?? * @param alpha ?? */ public synchronized static void addWatermark(File srcFile, File destFile, InputStream watermarkFile, WatermarkPosition watermarkPosition, int alpha) { //watermarkFile == null || !watermarkFile.exists() || if (!srcFile.exists() || watermarkFile == null || watermarkPosition == null || watermarkPosition == WatermarkPosition.no) { Log.println("addWatermark null"); return; } if (type == Type.jdk) { try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); if (srcBufferedImage == null) { return; } int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); BufferedImage destBufferedImage = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, srcWidth, srcHeight); graphics2D.drawImage(srcBufferedImage, 0, 0, null); graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha / 100.0F)); BufferedImage watermarkBufferedImage = ImageIO.read(watermarkFile); int watermarkImageWidth = watermarkBufferedImage.getWidth(); int watermarkImageHeight = watermarkBufferedImage.getHeight(); int x = srcWidth - watermarkImageWidth; int y = srcHeight - watermarkImageHeight; if (watermarkPosition == WatermarkPosition.topLeft) { x = 0; y = 0; } else if (watermarkPosition == WatermarkPosition.topRight) { x = srcWidth - watermarkImageWidth; y = 0; } else if (watermarkPosition == WatermarkPosition.center) { x = (srcWidth - watermarkImageWidth) / 2; y = (srcHeight - watermarkImageHeight) / 2; } else if (watermarkPosition == WatermarkPosition.bottomLeft) { x = 0; y = srcHeight - watermarkImageHeight; } else if (watermarkPosition == WatermarkPosition.bottomRight) { x = srcWidth - watermarkImageWidth; y = srcHeight - watermarkImageHeight; } graphics2D.drawImage(watermarkBufferedImage, x, y, watermarkImageWidth, watermarkImageHeight, null); graphics2D.dispose(); FileOutputStream out = new FileOutputStream(destFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(destBufferedImage); param.setQuality((float) DEST_QUALITY / 100, false); encoder.setJPEGEncodeParam(param); encoder.encode(destBufferedImage); out.close(); } catch (IOException e) { e.printStackTrace(); } } else { String gravity = "SouthEast"; if (watermarkPosition == WatermarkPosition.topLeft) { gravity = "NorthWest"; } else if (watermarkPosition == WatermarkPosition.topRight) { gravity = "NorthEast"; } else if (watermarkPosition == WatermarkPosition.center) { gravity = "Center"; } else if (watermarkPosition == WatermarkPosition.bottomLeft) { gravity = "SouthWest"; } else if (watermarkPosition == WatermarkPosition.bottomRight) { gravity = "SouthEast"; } IMOperation operation = new IMOperation(); operation.gravity(gravity); operation.dissolve(alpha); operation.quality((double) DEST_QUALITY); //operation.addImage(watermarkFile); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { CompositeCmd compositeCmd = new CompositeCmd(true); if (graphicsMagickPath != null) { compositeCmd.setSearchPath(graphicsMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { CompositeCmd compositeCmd = new CompositeCmd(false); if (imageMagickPath != null) { compositeCmd.setSearchPath(imageMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }