List of usage examples for java.awt.image BufferedImage getWidth
public int getWidth()
From source file:ConvertUtil.java
/** * Converts the source image to 24-bit colour (RGB). No transparency. * //from www . j a v a 2 s . c om * @param src * the source image to convert * @return a copy of the source image with a 24-bit colour depth */ public static BufferedImage convert24(BufferedImage src) { BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB); ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(), dest.getColorModel().getColorSpace(), null); cco.filter(src, dest); return dest; }
From source file:ConvertUtil.java
/** * Converts the source image to 32-bit colour with transparency (ARGB). * /* ww w. j av a 2 s .co m*/ * @param src * the source image to convert * @return a copy of the source image with a 32-bit colour depth. */ public static BufferedImage convert32(BufferedImage src) { BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB); ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(), dest.getColorModel().getColorSpace(), null); cco.filter(src, dest); return dest; }
From source file:com.datazuul.iiif.presentation.api.ManifestGenerator.java
private static void addPage(String urlPrefix, String imageDirectoryName, List<Canvas> canvases, int pageCounter, Path file) throws IOException, URISyntaxException { Path fileName = file.getFileName(); System.out.println(fileName.toAbsolutePath()); BufferedImage bimg = ImageIO.read(file.toFile()); int width = bimg.getWidth(); int height = bimg.getHeight(); // add a new page Canvas canvas1 = new Canvas(urlPrefix + imageDirectoryName + "/canvas/canvas-" + pageCounter, "p-" + pageCounter, height, width); canvases.add(canvas1);/*from www. j a v a2 s.c om*/ List<Image> images = new ArrayList<>(); canvas1.setImages(images); Image image1 = new Image(); image1.setOn(canvas1.getId()); images.add(image1); ImageResource imageResource1 = new ImageResource( urlPrefix + imageDirectoryName + "/" + fileName.toString()); imageResource1.setHeight(height); imageResource1.setWidth(width); image1.setResource(imageResource1); Service service1 = new Service(urlPrefix + imageDirectoryName + "/" + fileName.toString() + "?"); service1.setContext("http://iiif.io/api/image/2/context.json"); service1.setProfile("http://iiif.io/api/image/2/level1.json"); imageResource1.setService(service1); }
From source file:Main.java
/** * Rotates given image by given degrees which should be a multiple of 90 * @param source image to be rotated/*from ww w . j a va 2 s .c o m*/ * @param degrees the angle by which to rotate, should be a multiple of 90 * @return the rotated image */ public static BufferedImage rotateByRightAngle(BufferedImage source, int degrees) { assert degrees % 90 == 0; degrees = degrees % 360; int w = source.getWidth(); int h = source.getHeight(); int w1, h1; switch (degrees) { case 90: case 270: w1 = h; h1 = w; break; default: w1 = w; h1 = h; } BufferedImage rotated = new BufferedImage(w1, h1, source.getType()); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { int v = source.getRGB(x, y); int x1, y1; switch (degrees) { case 90: x1 = h - y - 1; y1 = x; break; case 180: x1 = w - x - 1; y1 = h - y - 1; break; case 270: x1 = y; y1 = w - x - 1; break; default: x1 = x; y1 = y; break; } rotated.setRGB(x1, y1, v); } } return rotated; }
From source file:Main.java
public static byte[] getCompressedImage(byte[] rgb) { BufferedImage image; int width;/*from w w w . j ava 2 s . c om*/ int height; try { image = ImageIO.read(new ByteArrayInputStream(rgb)); width = image.getWidth(); height = image.getHeight(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color c = new Color(image.getRGB(j, i)); int red = (int) (c.getRed() * 0.299); int green = (int) (c.getGreen() * 0.587); int blue = (int) (c.getBlue() * 0.114); Color newColor = new Color(red + green + blue, red + green + blue, red + green + blue); image.setRGB(j, i, newColor.getRGB()); } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); baos.close(); return imageInByte; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:net.duckling.ddl.util.ImageUtils.java
/** * ???// w w w . ja v a 2s .co m * @param tmpFilePath ? * @return */ public static boolean scare(String tmpFilePath) { try { BufferedImage src = ImageIO.read(new File(tmpFilePath)); // int width = src.getWidth(); int height = src.getHeight(); if (width > DEFAULT_WIDTH) { height = (DEFAULT_WIDTH * height) / width; width = DEFAULT_WIDTH; } Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(image, 0, 0, null); // ?? g.dispose(); File resultFile = new File(tmpFilePath); ImageIO.write(tag, "JPEG", resultFile);// ? return true; } catch (IOException e) { LOG.error(e); } return false; }
From source file:davmail.util.IOUtil.java
/** * Resize image to a max width or height image size. * * @param inputImage input image/*from w ww . j a v a2 s . c o m*/ * @param max max size * @return scaled image */ public static BufferedImage resizeImage(BufferedImage inputImage, int max) { int width = inputImage.getWidth(); int height = inputImage.getHeight(); int targetWidth; int targetHeight; if (width <= max && height <= max) { return inputImage; } else if (width > height) { targetWidth = max; targetHeight = targetWidth * height / width; } else { targetHeight = max; targetWidth = targetHeight * width / height; } Image scaledImage = inputImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH); BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); targetImage.getGraphics().drawImage(scaledImage, 0, 0, null); return targetImage; }
From source file:de.bamamoto.mactools.png2icns.Scaler.java
public static BufferedImage resize(BufferedImage source, int width, int height) { double xScale = ((double) width) / (double) source.getWidth(); double yScale = ((double) height) / (double) source.getHeight(); BufferedImage result = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration()//from w w w. j a va2s .co m .createCompatibleImage(width, height, source.getColorModel().getTransparency()); Graphics2D newImage = null; try { newImage = result.createGraphics(); newImage.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); newImage.drawRenderedImage(source, AffineTransform.getScaleInstance(xScale, yScale)); } finally { if (newImage != null) { newImage.dispose(); } } return result; }
From source file:contactangle.ImageControl.java
public static BufferedImage copyByPixel(BufferedImage ii) { BufferedImage oi = new BufferedImage(ii.getWidth(), ii.getHeight(), BufferedImage.TYPE_INT_RGB); for (int x = 0; x < ii.getWidth(); x++) { for (int y = 0; y < ii.getHeight(); y++) { int pixelData = ii.getRGB(x, y); oi.setRGB(x, y, pixelData);/*from w w w .j av a 2 s . c om*/ } } return oi; }
From source file:costumetrade.common.util.PdfUtils.java
public static PDDocument createImagePdf(File file) { PDPageContentStream contentStream = null; try {/* ww w . j av a 2 s. c om*/ BufferedImage bimg = ImageIO.read(file); PDDocument document = new PDDocument(); PDPage page = new PDPage(new PDRectangle(bimg.getWidth(), bimg.getHeight())); document.addPage(page); PDImageXObject image = PDImageXObject.createFromFileByExtension(file, document); contentStream = new PDPageContentStream(document, page); contentStream.drawImage(image, 0, 0); return document; } catch (IOException e) { throw new RuntimeException("?PDF", e); } finally { IOUtils.closeQuietly(contentStream); } }