List of usage examples for java.awt.image BufferedImage getWidth
public int getWidth()
From source file:de.darkblue.bongloader2.utils.ToolBox.java
public static BufferedImage grayScale(BufferedImage image) { BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); Graphics g = newImage.getGraphics(); g.drawImage(image, 0, 0, null);/*from w ww .j a va2s . c om*/ g.dispose(); return newImage; }
From source file:business.model.CaptchaModel.java
/** * * @param baseImage//from w w w .j ava 2 s . co m * @return */ private static BufferedImage getDistortedImage(BufferedImage baseImage) { BufferedImage distortedImage = new BufferedImage(baseImage.getWidth(), baseImage.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D graph = (Graphics2D) distortedImage.getGraphics(); ShadowFilter shadowFilter = new ShadowFilter(); shadowFilter.setRadius(10); shadowFilter.setDistance(5); shadowFilter.setOpacity(1); Random rand = new Random(); RippleFilter rippleFilter = new RippleFilter(); rippleFilter.setWaveType(RippleFilter.SINE); rippleFilter.setXAmplitude(1.6f); rippleFilter.setYAmplitude(1.0f); rippleFilter.setXWavelength(5); rippleFilter.setYWavelength(2); rippleFilter.setEdgeAction(TransformFilter.BILINEAR); BufferedImage effectImage = rippleFilter.filter(baseImage, null); effectImage = shadowFilter.filter(effectImage, null); graph.drawImage(effectImage, 0, 0, null, null); graph.dispose(); // draw lines over the image and/or text // noiseProducer.makeNoise(distortedImage, .1f, .1f, .25f, .25f); return distortedImage; }
From source file:ImageProcessing.ImageProcessing.java
public static void processConvolve(BufferedImage image, double[] filter) { //Applies convolution operation using passed filter to the image. //Initialize values int alphaValue, filteredRed, filteredGreen, filteredBlue; int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double[] temp_alpha = extractAlphaValue(image); double[] temp_red = extractRedColor(image); double[] temp_green = extractGreenColor(image); double[] temp_blue = extractBlueColor(image); //For every pixels (except top/bottom row & borderline left/right row, //Apply filter. for (int i = 1; i < imageHeight - 1; i++) { for (int j = 1; j < imageWidth - 1; j++) { alphaValue = (int) temp_alpha[(i * imageWidth) + j]; //Apply filter to every color component (RGB) filteredRed = processFiltering(j, i, imageWidth, imageHeight, temp_red, filter); filteredGreen = processFiltering(j, i, imageWidth, imageHeight, temp_green, filter); filteredBlue = processFiltering(j, i, imageWidth, imageHeight, temp_blue, filter); //Copy the processed color values to a RGB integer using bitwise operator. int filteredRGB = (alphaValue << 24) + (filteredRed << 16) + (filteredGreen << 8) + filteredBlue; //Set the RGB back to the exact same pixel position. image.setRGB(j, i, filteredRGB); }//from w w w .ja v a 2s.c om } }
From source file:com.lingxiang2014.util.ImageUtils.java
public static void addWatermark(File srcFile, File destFile, File watermarkFile, int alpha) { Assert.notNull(srcFile);/*w w w . j ava2s. c o m*/ Assert.notNull(destFile); Assert.state(alpha >= 0); Assert.state(alpha <= 100); if (watermarkFile == null || !watermarkFile.exists()) { try { FileUtils.copyFile(srcFile, destFile); } catch (IOException e) { e.printStackTrace(); } return; } if (type == Type.jdk) { Graphics2D graphics2D = null; ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; 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 = 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 / 100F)); BufferedImage watermarkBufferedImage = ImageIO.read(watermarkFile); int watermarkImageWidth = watermarkBufferedImage.getWidth(); int watermarkImageHeight = watermarkBufferedImage.getHeight(); int x = srcWidth - watermarkImageWidth; int y = srcHeight - watermarkImageHeight; graphics2D.drawImage(watermarkBufferedImage, x, y, watermarkImageWidth, watermarkImageHeight, null); imageOutputStream = ImageIO.createImageOutputStream(destFile); imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName())) .next(); imageWriter.setOutput(imageOutputStream); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality(DEST_QUALITY / 100F); imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam); imageOutputStream.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (graphics2D != null) { graphics2D.dispose(); } if (imageWriter != null) { imageWriter.dispose(); } if (imageOutputStream != null) { try { imageOutputStream.close(); } catch (IOException e) { } } } } else { IMOperation operation = new IMOperation(); 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:com.reydentx.core.common.PhotoUtils.java
public static byte[] resizeImage(ByteArrayInputStream data, int img_width, int img_height, boolean isPNG) { BufferedImage originalImage; try {/*from w w w . j a v a 2s .c om*/ originalImage = ImageIO.read(data); Dimension origDimentsion = new Dimension(originalImage.getWidth(), originalImage.getHeight()); Dimension fitDimentsion = new Dimension(img_width, img_height); // Dimension dimentsion = getScaledDimension(origDimentsion, fitDimentsion); Dimension dimentsion = fitDimentsion; if (origDimentsion.width != dimentsion.width || origDimentsion.height != dimentsion.height) { ByteArrayOutputStream outstream = new ByteArrayOutputStream(); BufferedImage resizedImage = Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_EXACT, dimentsion.width, dimentsion.height, Scalr.OP_ANTIALIAS); if (isPNG) { ImageIO.write(resizedImage, "png", outstream); } else { ImageIO.write(resizedImage, "jpg", outstream); } return outstream.toByteArray(); } else { data.reset(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); byte[] read = new byte[2048]; int i = 0; while ((i = data.read(read)) > 0) { byteArray.write(read, 0, i); } data.close(); return byteArray.toByteArray(); } } catch (Exception ex) { } return null; }
From source file:common.utils.ImageUtils.java
/** * resize input image to new dinesions(only smaller) into rez parameter * @param image input image for scaling * @param rez resulting image. must have required width and height * @throws IOException/*from w w w . j a v a 2 s . com*/ */ public static void getScaledImageDimmension(BufferedImage image, BufferedImage rez) throws IOException { Graphics2D g2 = rez.createGraphics(); if (rez.getHeight() > image.getHeight() || rez.getWidth() > image.getWidth()) { //rez image is bigger no resize g2.drawImage(image, 0, 0, null); return; } //1-st getting first side to resize (width or height) double scale_factor; if (getScaling(image.getHeight(), rez.getHeight()) > getScaling(image.getWidth(), rez.getWidth())) { //resize height scale_factor = getScaling(image.getHeight(), rez.getHeight()); int width = (int) (scale_factor * image.getWidth()); //cut width int x = (rez.getWidth() - width) / 2; g2.drawImage(image.getScaledInstance(width, rez.getHeight(), Image.SCALE_SMOOTH), x, 0, null); //System.out.println("resizing height: h="+image.getHeight()+"/"+rez.getHeight()+"; x="+x); } else { //resize width scale_factor = getScaling(image.getWidth(), rez.getWidth()); int height = (int) (scale_factor * image.getHeight()); //cut height int y = (rez.getHeight() - height) / 2; g2.drawImage(image.getScaledInstance(rez.getWidth(), height, Image.SCALE_SMOOTH), 0, y, null); //System.out.println("resizing width: w="+image.getWidth()+"/"+rez.getWidth()+"; y="+y); } g2.dispose(); }
From source file:com.boundlessgeo.geoserver.api.controllers.ThumbnailController.java
/** * Utility method for scaling thumbnails. Scales byte[] image by a scale factor. * Optionally crops images to square.//w w w.ja v a 2 s . c o m * @param src RenderedImage containing the input image * @param scale Scale amount * @param square Boolean flag to crop to a square image * @return RenderedImage containing the transformed image * @throws IOException */ public static BufferedImage scaleImage(BufferedImage image, double scale, boolean square) throws IOException { int sx = 0, sy = 0; int swidth = image.getWidth(); int sheight = image.getHeight(); if (square) { if (image.getHeight() > image.getWidth()) { sy = (int) ((image.getHeight() - image.getWidth()) / 2.0); sheight = swidth; } else if (image.getHeight() < image.getWidth()) { sx = (int) ((image.getWidth() - image.getHeight()) / 2.0); swidth = sheight; } } int width = (int) (swidth * scale); int height = (int) (sheight * scale); BufferedImage scaled = new BufferedImage(image.getColorModel(), image.getRaster().createCompatibleWritableRaster(width, height), image.isAlphaPremultiplied(), null); Graphics g = scaled.getGraphics(); g.drawImage(image, 0, 0, width, height, sx, sy, sx + swidth, sy + sheight, null); g.dispose(); return scaled; }
From source file:com.alvermont.terraj.stargen.ui.UIUtils.java
/** * Combine a list of images horizontally into one new image. * //ww w. ja v a 2 s .co m * @param images The list of images to be combined * @return A new image, as wide horizontally as the sum of the input images, * containing all the input images */ public static BufferedImage combineImagesHorizontal(List<BufferedImage> images) { int imageType = -1; int height = 0; int width = 0; // first work out the sizing and image type, we assume the images // are all compatible. for (BufferedImage image : images) { if (imageType == -1) { imageType = image.getType(); } width += image.getWidth(); height = Math.max(height, image.getHeight()); } // create the new image and clear it to black BufferedImage bi = new BufferedImage(width, height, imageType); Graphics2D g = bi.createGraphics(); g.setColor(Color.BLACK); g.fillRect(0, 0, width, height); // merge the images into the new one int xpos = 0; for (BufferedImage image : images) { int ypos = (height - image.getHeight()) / 2; g.drawImage(image, xpos, ypos, null); xpos += image.getWidth(); } return bi; }
From source file:com.jaeksoft.searchlib.util.ImageUtils.java
public final static BufferedImage reduceImage(BufferedImage image, int width, int height) { int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = (BufferedImage) image; int w = image.getWidth(); int h = image.getHeight(); while (w != width || h != height) { if (w > width) { w /= 2;//from ww w . j a v a 2s.c om if (w < width) w = width; } if (h > height) { h /= 2; if (h < height) h = height; } BufferedImage tmp = new BufferedImage(w, h, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } return ret; }
From source file:common.utils.ImageUtils.java
/** * * @param src images to draw, they must be resized to an appropriate size * @param dst image where given images will be drawen *///w ww. j a v a 2 s. c o m public static void draw4on1(BufferedImage[] src, BufferedImage dst) { Graphics2D g2 = dst.createGraphics(); g2.setColor(java.awt.Color.WHITE); g2.fillRect(0, 0, dst.getWidth(), dst.getHeight()); int dxi; int dyi = 0; int x0 = dst.getWidth() - 5; int y0 = 5; int x = x0; int y = y0; for (int i = 0; i < src.length; i++) { if (i % 2 == 0) { dxi = -10; } else { dxi = 0; } //g2.draw3DRect(dx - 1 , dy-tmp_bi.getHeight() - 1, tmp_bi.getWidth() + 1 , tmp_bi.getHeight() + 1, true); g2.drawImage(src[i], x - src[i].getWidth() + dxi, y + dyi, null); g2.drawString("#" + i, x - src[i].getWidth() + dxi, y + dyi + 20); //g2.rotate(Math.toRadians(4)); y = y + src[i].getHeight() / 2; if (y > dst.getHeight() - src[i].getHeight()) { y = y0; if (dyi == 0) dyi = 10; else dyi = 0; if (x < src[i].getWidth()) { x = dst.getWidth(); } x = x - src[i].getWidth() / 2; } } g2.setColor(Color.gray); g2.drawRect(0, 0, dst.getWidth() - 1, dst.getHeight() - 1); g2.dispose(); }