List of usage examples for java.awt.image BufferedImage getHeight
public int getHeight()
From source file:Main.java
public static byte[] getCompressedImage(byte[] rgb) { BufferedImage image; int width;/*from w w w. ja va2 s . c o m*/ 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:Main.java
/** * Changes the opacity of a given image. * @param src The source image.//from w ww.j a va 2 s . c om * @param opacity The opacity to change to. * @return */ public static BufferedImage imgUtilAdjustImageTransparency(BufferedImage src, float opacity) { if (src == null) return null; AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity); BufferedImage cpy = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D) cpy.getGraphics(); g.setComposite(ac); g.drawImage(src, 0, 0, null); g.dispose(); return cpy; }
From source file:net.duckling.ddl.util.ImageUtils.java
/** * ???/*from www . jav a 2 s .com*/ * @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:at.gv.egiz.pdfas.common.utils.ImageUtils.java
public static BufferedImage convertRGBAToIndexed(BufferedImage src) { BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); Graphics g = dest.getGraphics(); g.setColor(new Color(231, 20, 189)); g.fillRect(0, 0, dest.getWidth(), dest.getHeight()); // fill with a // hideous color // and make it // transparent dest = makeTransparent(dest, 0, 0);// w ww .ja va 2s . c o m dest.createGraphics().drawImage(src, 0, 0, null); return dest; }
From source file:davmail.util.IOUtil.java
/** * Resize image to a max width or height image size. * * @param inputImage input image/*from ww w .j a v a 2 s . co 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:Main.java
public static String getImageDimension(String imgLocation) throws Exception { BufferedImage image = null; try {/*from www . j a v a 2s . c o m*/ image = ImageIO.read(new URL(imgLocation)); } catch (Exception e) { // If an error occurs, we consider the image as null. - FB } return (image == null ? "-1,-1" : image.getWidth() + "," + image.getHeight()); }
From source file:com.openkm.util.ImageUtils.java
/** * pointWithinRange/* ww w.j a va2 s .c o m*/ */ public static boolean pointWithinRange(Point p, BufferedImage img) { return !(p.x < 0 || p.y < 0 || p.x >= img.getWidth() || p.y >= img.getHeight()); }
From source file:com.openkm.util.ImageUtils.java
/** * cloneImage//from w w w . j av a2s .com */ public static BufferedImage clone(BufferedImage source) { BufferedImage img = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.drawImage(source, 0, 0, null); g.dispose(); return img; }
From source file:com.partagames.imageresizetool.SimpleImageResizeTool.java
/** * Scales an image to the desired dimensions. * * @param img Original image//from www . j a v a 2 s . c o m * @param newW Target width * @param newH Target height * @return Scaled image */ public static BufferedImage scale(BufferedImage img, int newW, int newH) { int w = img.getWidth(); int h = img.getHeight(); final BufferedImage dimg = new BufferedImage(newW, newH, img.getType()); final Graphics2D g = dimg.createGraphics(); // use provided rendering hint, default is bilinear switch (scalingHint) { case "n": g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); break; case "b": g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); break; } g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null); g.dispose(); return dimg; }
From source file:com.simiacryptus.mindseye.test.data.CIFAR10.java
private static LabeledObject<BufferedImage> toImage(final byte[] b) { @Nonnull final BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < img.getWidth(); x++) { for (int y = 0; y < img.getHeight(); y++) { final int red = 0xFF & b[1 + 1024 * 0 + x + y * 32]; final int blue = 0xFF & b[1 + 1024 * 1 + x + y * 32]; final int green = 0xFF & b[1 + 1024 * 2 + x + y * 32]; final int c = (red << 16) + (blue << 8) + green; img.setRGB(x, y, c);/*from www .j av a2s .c o m*/ } } return new LabeledObject<>(img, Arrays.toString(new byte[] { b[0] })); }