List of usage examples for java.awt.image BufferedImage createGraphics
public Graphics2D createGraphics()
From source file:com.mirth.connect.server.util.DICOMUtil.java
private static byte[] saveAsJpeg(ImagePlus imagePlug, int quality) { int imageType = BufferedImage.TYPE_INT_RGB; if (imagePlug.getProcessor().isDefaultLut()) { imageType = BufferedImage.TYPE_BYTE_GRAY; }/*from w w w . jav a 2s . co m*/ BufferedImage bufferedImage = new BufferedImage(imagePlug.getWidth(), imagePlug.getHeight(), imageType); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { Graphics graphics = bufferedImage.createGraphics(); graphics.drawImage(imagePlug.getImage(), 0, 0, null); graphics.dispose(); ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); writer.setOutput(ImageIO.createImageOutputStream(baos)); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality / 100f); if (quality == 100) { param.setSourceSubsampling(1, 1, 0, 0); } IIOImage iioImage = new IIOImage(bufferedImage, null, null); writer.write(null, iioImage, param); return baos.toByteArray(); } catch (Exception e) { logger.error("Error converting dcm file", e); } finally { IOUtils.closeQuietly(baos); } return null; }
From source file:com.mirth.connect.server.util.DICOMMessageUtil.java
private static byte[] saveAsJpeg(ImagePlus imagePlug, int quality) { int imageType = BufferedImage.TYPE_INT_RGB; if (imagePlug.getProcessor().isDefaultLut()) { imageType = BufferedImage.TYPE_BYTE_GRAY; }// w ww . ja v a2 s .c o m BufferedImage bufferedImage = new BufferedImage(imagePlug.getWidth(), imagePlug.getHeight(), imageType); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { Graphics graphics = bufferedImage.createGraphics(); graphics.drawImage(imagePlug.getImage(), 0, 0, null); graphics.dispose(); ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); writer.setOutput(ImageIO.createImageOutputStream(baos)); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality / 100f); if (quality == 100) { param.setSourceSubsampling(1, 1, 0, 0); } IIOImage iioImage = new IIOImage(bufferedImage, null, null); writer.write(null, iioImage, param); return baos.toByteArray(); } catch (Exception e) { logger.error("Error Converting DICOM image", e); } finally { IOUtils.closeQuietly(baos); } return null; }
From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java
public static void updateImage(JLabel imageContainer, File imageFile, Format format) { if (imageFile == null || !imageFile.exists()) { return;/*from w w w . j av a 2 s. c om*/ } BufferedImage img = null; try { img = ImageIO.read(imageFile); } catch (IOException e) { e.printStackTrace(); } if (img == null) { return; } int imageWidth = img.getWidth(); int imageHeight = img.getHeight(); int imageViewWidth = (int) imageContainer.getPreferredSize().getWidth(); int imageViewHeight = (int) imageContainer.getPreferredSize().getHeight(); double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), new Dimension(imageViewWidth, imageViewHeight)); imageWidth = (int) (factor * imageWidth); imageHeight = (int) (factor * imageHeight); if (imageWidth <= 0 || imageHeight <= 0 || imageViewWidth <= 0 || imageViewHeight <= 0) { return; } BufferedImage tmp = UIUtil.createImage(imageViewWidth, imageViewHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); int x = (imageViewWidth - imageWidth) / 2; int y = (imageViewHeight - imageHeight) / 2; if (format == Format.PNG || format == Format.XML) { g2.drawImage(img, x, y, imageWidth, imageHeight, null); } else { g2.drawImage(img, x, y, imageWidth, imageHeight, Color.WHITE, null); } g2.dispose(); imageContainer.setIcon(new ImageIcon(tmp)); }
From source file:com.tomtom.speedtools.json.ImageSerializer.java
@Nonnull private static BufferedImage convertToBufferedImage(@Nonnull final Image image) throws IOException { assert image != null; if (image instanceof BufferedImage) { return (BufferedImage) image; }//from w ww.j ava2s . c om /** * Load the image in the background and wait * until is is downloaded. */ final MediaTracker tracker = new MediaTracker(new Component() { // Empty. }); tracker.addImage(image, 0); try { tracker.waitForAll(); } catch (final InterruptedException e) { throw new IOException(e.getMessage(), e); } /** * Create a buffered image with the right dimensions. */ final BufferedImage bufImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); /** * Draw the image in the buffer and return it as base64 data. */ final Graphics g = bufImage.createGraphics(); g.drawImage(image, 0, 0, null); return bufImage; }
From source file:com.mikenimer.familydam.services.photos.ThumbnailService.java
/** * using the metadata orientation transformation information rotate the image. * @param image//from w ww .ja v a 2s .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:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java
/** * Paints a chart with scaling options/* w w w . j a v a 2s . com*/ * * @param chart * @param info * @param out * @param width * @param height * @param resolution * @return BufferedImage of a given chart with scaling to resolution * @throws IOException */ private static BufferedImage paintScaledChartToBufferedImage(JFreeChart chart, ChartRenderingInfo info, OutputStream out, int width, int height, int resolution, int bufferedIType) throws IOException { Args.nullNotPermitted(out, "out"); Args.nullNotPermitted(chart, "chart"); double scaleX = resolution / 72.0; double scaleY = resolution / 72.0; double desiredWidth = width * scaleX; double desiredHeight = height * scaleY; double defaultWidth = width; double defaultHeight = height; boolean scale = false; // get desired width and height from somewhere then... if ((scaleX != 1) || (scaleY != 1)) { scale = true; } BufferedImage image = new BufferedImage((int) desiredWidth, (int) desiredHeight, bufferedIType); Graphics2D g2 = image.createGraphics(); if (scale) { AffineTransform saved = g2.getTransform(); g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY)); chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info); g2.setTransform(saved); g2.dispose(); } else { chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info); } return image; }
From source file:de.darkblue.bongloader2.utils.ToolBox.java
public static BufferedImage resizeImage(BufferedImage image, int width, int height) { BufferedImage returnValue = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2D = returnValue.createGraphics(); g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); AffineTransform at = AffineTransform.getScaleInstance((double) width / image.getWidth(), (double) height / image.getHeight()); g2D.drawRenderedImage(image, at);//from ww w . ja v a 2 s.com return returnValue; }
From source file:com.t3.macro.api.functions.input.InputFunctions.java
/** * Gets icon from the asset manager. Code copied and modified from * EditTokestaticnDialog.java/*from w w w .j a v a 2s . c o m*/ */ static ImageIcon getIcon(String id, int size, ImageObserver io) { // Extract the MD5Key from the URL if (id == null) return null; MD5Key assetID = new MD5Key(id); // Get the base image && find the new size for the icon BufferedImage assetImage = ImageManager.getImage(assetID, io); // Resize if (assetImage.getWidth() > size || assetImage.getHeight() > size) { Dimension dim = new Dimension(assetImage.getWidth(), assetImage.getWidth()); if (dim.height < dim.width) { dim.height = (int) ((dim.height / (double) dim.width) * size); dim.width = size; } else { dim.width = (int) ((dim.width / (double) dim.height) * size); dim.height = size; } BufferedImage image = new BufferedImage(dim.width, dim.height, Transparency.BITMASK); Graphics2D g = image.createGraphics(); g.drawImage(assetImage, 0, 0, dim.width, dim.height, null); assetImage = image; } return new ImageIcon(assetImage); }
From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
public static void updateImage(JLabel imageContainer, File imageFile) { if (!imageFile.exists()) { return;//from w w w. jav a 2 s . com } BufferedImage img = null; try { img = ImageIO.read(imageFile); } catch (IOException e) { e.printStackTrace(); } if (img == null) { return; } int imageWidth = img.getWidth(); int imageHeight = img.getHeight(); int imageViewWidth = imageContainer.getWidth(); int imageViewHeight = imageContainer.getHeight(); double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), new Dimension(imageViewWidth, imageViewHeight)); factor = Math.min(factor, 1f); imageWidth = (int) (factor * imageWidth); imageHeight = (int) (factor * imageHeight); if (imageWidth <= 0 || imageHeight <= 0 || imageViewWidth <= 0 || imageViewHeight <= 0) { return; } BufferedImage tmp = UIUtil.createImage(imageViewWidth, imageViewHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); int x = (imageViewWidth - imageWidth) / 2; int y = (imageViewHeight - imageHeight) / 2; g2.drawImage(img, x, y, imageWidth, imageHeight, null); g2.dispose(); imageContainer.setIcon(new ImageIcon(tmp)); }
From source file:net.rptools.lib.image.ImageUtil.java
/** * Create a copy of the image that is compatible with the current graphics context and scaled to the supplied size *//*from w w w .j ava 2 s . c om*/ public static BufferedImage createCompatibleImage(Image img, int width, int height, Map<String, Object> hints) { width = Math.max(width, 1); height = Math.max(height, 1); int transparency; if (hints != null && hints.containsKey(HINT_TRANSPARENCY)) { transparency = (Integer) hints.get(HINT_TRANSPARENCY); } else { transparency = pickBestTransparency(img); } BufferedImage compImg = new BufferedImage(width, height, transparency); Graphics2D g = null; try { g = compImg.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawImage(img, 0, 0, width, height, null); } finally { if (g != null) { g.dispose(); } } return compImg; }