List of usage examples for java.awt Graphics2D drawImage
public abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer);
From source file:ar.com.zauber.common.image.impl.AbstractImage.java
/** * Creates a thumbnail// w ww . j a va 2 s . co m * * @param is data source * @param os data source * @throws IOException if there is a problem reading is */ public static void createThumbnail(final InputStream is, final OutputStream os, final int target) throws IOException { final float compression = 0.85F; ImageWriter writer = null; MemoryCacheImageOutputStream mos = null; Graphics2D graphics2D = null; try { final BufferedImage bi = ImageIO.read(is); final Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("JPG"); if (!iter.hasNext()) { throw new IllegalStateException("can't find JPG subsystem"); } int w = bi.getWidth(), h = bi.getHeight(); if (w < target && h < target) { // nothing to recalculate, ya es chiquita. } else { if (w > h) { h = target * bi.getHeight() / bi.getWidth(); w = target; } else { w = target * bi.getWidth() / bi.getHeight(); h = target; } } // draw original image to thumbnail image object and // scale it to the new size on-the-fly final BufferedImage thumbImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(bi, 0, 0, w, h, null); writer = (ImageWriter) iter.next(); final ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(compression); mos = new MemoryCacheImageOutputStream(os); writer.setOutput(mos); writer.write(null, new IIOImage(thumbImage, null, null), iwp); } finally { if (writer != null) { writer.dispose(); } if (mos != null) { mos.close(); } if (graphics2D != null) { graphics2D.dispose(); } is.close(); os.close(); } }
From source file:bjerne.gallery.service.impl.ImageResizeServiceImpl.java
@Override public void resizeImage(File origImage, File newImage, int width, int height) throws IOException { LOG.debug("Entering resizeImage(origImage={}, width={}, height={})", origImage, width, height); long startTime = System.currentTimeMillis(); InputStream is = new BufferedInputStream(new FileInputStream(origImage)); BufferedImage i = ImageIO.read(is); IOUtils.closeQuietly(is);/*w w w. ja v a2s . c o m*/ BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); int origWidth = i.getWidth(); int origHeight = i.getHeight(); LOG.debug("Original size of image - width: {}, height={}", origWidth, height); float widthFactor = ((float) origWidth) / ((float) width); float heightFactor = ((float) origHeight) / ((float) height); float maxFactor = Math.max(widthFactor, heightFactor); int newHeight, newWidth; if (maxFactor > 1) { newHeight = (int) (((float) origHeight) / maxFactor); newWidth = (int) (((float) origWidth) / maxFactor); } else { newHeight = origHeight; newWidth = origWidth; } LOG.debug("Size of scaled image will be: width={}, height={}", newWidth, newHeight); int startX = Math.max((width - newWidth) / 2, 0); int startY = Math.max((height - newHeight) / 2, 0); Graphics2D scaledGraphics = scaledImage.createGraphics(); scaledGraphics.setColor(backgroundColor); scaledGraphics.fillRect(0, 0, width, height); scaledGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); scaledGraphics.drawImage(i, startX, startY, newWidth, newHeight, null); OutputStream resultImageOutputStream = new BufferedOutputStream(FileUtils.openOutputStream(newImage)); String extension = FilenameUtils.getExtension(origImage.getName()); ImageIO.write(scaledImage, extension, resultImageOutputStream); IOUtils.closeQuietly(resultImageOutputStream); long duration = System.currentTimeMillis() - startTime; LOG.debug("Time in milliseconds to scale {}: {}", newImage.toString(), duration); }
From source file:com.kahlon.guard.controller.PersonImageManager.java
private BufferedImage resizeImage(BufferedImage originalImage, int type) { BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); g.dispose();//from ww w.ja v a 2s .c o m return resizedImage; }
From source file:org.asqatasun.sebuilder.interpreter.TgTestRun.java
/** * Resize the snapshot image to 270*170/*from ww w.j a v a 2 s . com*/ * * @param originalImage * @return */ private BufferedImage resizeImage(BufferedImage originalImage) { float scaleRatio = (float) 270 / originalImage.getWidth(); int resizedImageHeight = Float.valueOf(originalImage.getHeight() * scaleRatio).intValue(); BufferedImage resizedImage = new BufferedImage(270, resizedImageHeight, originalImage.getType()); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, 270, resizedImageHeight, null); g.dispose(); return resizedImage.getSubimage(0, 0, 270, 170); }
From source file:com.kahlon.guard.controller.PersonImageManager.java
private BufferedImage resizeImageWithHint(BufferedImage originalImage, int type) { BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); g.dispose();/* w w w.j a v a2s . co m*/ g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return resizedImage; }
From source file:IconDemoApp.java
/** * Resizes an image using a Graphics2D object backed by a BufferedImage. * /*from w w w. j ava 2 s . c o m*/ * @param srcImg - * source image to scale * @param w - * desired width * @param h - * desired height * @return - the new resized image */ private Image getScaledImage(Image srcImg, int w, int h) { BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = resizedImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(srcImg, 0, 0, w, h, null); g2.dispose(); return resizedImg; }
From source file:com.cubusmail.server.services.RetrieveImageServlet.java
/** * @param bufInputStream/*from w w w.j a va 2 s. com*/ * @param outputStream */ private void writeScaledImage(BufferedInputStream bufInputStream, OutputStream outputStream) { long millis = System.currentTimeMillis(); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = bufInputStream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); byte[] imageBytes = bos.toByteArray(); Image image = Toolkit.getDefaultToolkit().createImage(imageBytes); MediaTracker mediaTracker = new MediaTracker(new Container()); mediaTracker.addImage(image, 0); mediaTracker.waitForID(0); // determine thumbnail size from WIDTH and HEIGHT int thumbWidth = 300; int thumbHeight = 200; double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } // draw original image to thumbnail image object and // scale it to the new size on-the-fly 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(image, 0, 0, thumbWidth, thumbHeight, null); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage); int quality = 70; quality = Math.max(0, Math.min(quality, 100)); param.setQuality((float) quality / 100.0f, false); encoder.setJPEGEncodeParam(param); encoder.encode(thumbImage); } catch (IOException ex) { log.error(ex.getMessage(), ex); } catch (InterruptedException ex) { log.error(ex.getMessage(), ex); } finally { log.debug("Time for thumbnail: " + (System.currentTimeMillis() - millis) + "ms"); } }
From source file:jdroidremote.ServerFrame.java
private void startMonitoring() { thStartMonitoring = new Thread(new Runnable() { @Override//from w w w. j a v a2 s.c o m public void run() { try { System.out.println("START MONITORING.........."); // while (1 == 1) { BufferedImage screenCapture = robot .createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); Image cursor = ImageIO.read(new File("cursor.png")); int x = MouseInfo.getPointerInfo().getLocation().x; int y = MouseInfo.getPointerInfo().getLocation().y; Graphics2D graphics2D = screenCapture.createGraphics(); graphics2D.drawImage(cursor, x, y, 13, 25, null); // cursor.gif is 16x16 size. ImageIO.write(screenCapture, "JPG", new File("2.jpg")); Thread.sleep(200); File file = new File("2.jpg"); // Reading a Image file from file system FileInputStream imageInFile = new FileInputStream(file); byte imageData[] = new byte[(int) file.length()]; imageInFile.read(imageData); // Converting Image byte array into Base64 String String imageDataString = ImageManipulation.encodeImage(imageData); System.out.println(imageDataString.length()); // System.out.println(imageDataString); // // Converting a Base64 String into Image byte array // byte[] imageByteArray = ImageManipulation.decodeImage(imageDataString); // // // Write a image byte array into file system // FileOutputStream imageOutFile = new FileOutputStream( // "_avatar_.jpg"); // // imageOutFile.write(imageByteArray); imageInFile.close(); // imageOutFile.close(); System.out.println("Image Successfully Manipulated!"); // Split the five sandwiches.! StringBuilder sbImageDataString = new StringBuilder(imageDataString); for (int i = 0; i < sbImageDataString.toString().length(); i += 30000) { if (i + 30000 <= sbImageDataString.toString().length()) { dos.writeUTF(sbImageDataString.substring(i, i + 30000)); dos.flush(); } else { dos.writeUTF(sbImageDataString.substring(i, sbImageDataString.toString().length())); dos.flush(); } } dos.writeUTF("..."); dos.flush(); // } } catch (IOException ex) { Logger.getLogger(ServerFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(ServerFrame.class.getName()).log(Level.SEVERE, null, ex); } } }); }
From source file:ImageDuplicity.java
private void createOffscreenImage() { Dimension d = getSize();//from w ww. j ava2 s. c o m int width = d.width; int height = d.height; image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); try { String filename = "largeJava2sLogo.jpg"; InputStream in = getClass().getResourceAsStream(filename); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in); BufferedImage image = decoder.decodeAsBufferedImage(); in.close(); g2.drawImage(image, 0, 0, width, height, null); } catch (Exception e) { System.out.print(e); } g2.setStroke(new BasicStroke(2)); Color[] colors = { Color.red, Color.blue, Color.green }; for (int i = -32; i < 40; i += 8) { g2.setPaint(colors[Math.abs(i) % 3]); g2.drawOval(i, i, width - i * 2, height - i * 2); } }
From source file:com.frostwire.gui.library.LibraryCoverArt.java
private void setPrivateImage(Image image) { coverArtImage = image;/*from www .ja va 2s . c o m*/ if (coverArtImage == null) { coverArtImage = defaultCoverArt; } Graphics2D g2 = background.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setBackground(new Color(255, 255, 255, 0)); g2.clearRect(0, 0, getWidth(), getHeight()); g2.drawImage(coverArtImage, 0, 0, getWidth(), getHeight(), null); g2.dispose(); repaint(); getToolkit().sync(); }