List of usage examples for java.awt.image BufferedImage getWidth
public int getWidth()
From source file:com.fluidops.iwb.deepzoom.DZConvert.java
/** * Returns resized image/*from w w w.ja v a 2s . c o m*/ * NB - useful reference on high quality image resizing can be found here: * http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html * @param width the required width * @param height the frequired height * @param img the image to be resized */ private static BufferedImage resizeImage(BufferedImage img, double width, double height) { int w = (int) width; int h = (int) height; BufferedImage result = new BufferedImage(w, h, getType(img)); Graphics2D g = result.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.drawImage(img, 0, 0, w, h, 0, 0, img.getWidth(), img.getHeight(), null); return result; }
From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java
/** * Flip the image horizontal/*from w w w . j a v a2 s .co m*/ * * @param bufferedImage original image * @return horizontally flipped image */ private static BufferedImage flipVertical(BufferedImage bufferedImage) { AffineTransform at = AffineTransform.getTranslateInstance(0, bufferedImage.getHeight()); at.scale(1.0, -1.0); BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType()); return biOp.filter(bufferedImage, imgRes); }
From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java
private static BufferedImage rescaleBorder(BufferedImage image, int targetWidth, int targetHeight) throws IOException { if (targetWidth == 0) { targetWidth = 1;//w ww . j a va 2 s. c om } if (targetHeight == 0) { targetHeight = 1; } if (targetHeight > 1 && targetWidth > 1) { throw new IOException(); } int w = image.getWidth(); int h = image.getHeight(); int[] data = image.getRGB(0, 0, w, h, null, 0, w); int[] newData = new int[targetWidth * targetHeight]; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { newData[y * targetWidth + x] = 0x00; } } List<Integer> startPositions = new ArrayList<Integer>(); List<Integer> endPositions = new ArrayList<Integer>(); boolean inBlock = false; if (targetHeight == 1) { for (int x = 0; x < w; x++) { if ((0xff000000 & data[x]) != 0) { if (!inBlock) { inBlock = true; startPositions.add(x); } } else if (inBlock) { endPositions.add(x - 1); inBlock = false; } } if (inBlock) { endPositions.add(w - 1); } } else { for (int y = 0; y < h; y++) { if ((0xff000000 & data[y]) != 0) { if (!inBlock) { inBlock = true; startPositions.add(y); } } else if (inBlock) { endPositions.add(y - 1); inBlock = false; } } if (inBlock) { endPositions.add(h - 1); } } try { SplineInterpolator interpolator = new SplineInterpolator(); PolynomialSplineFunction function = interpolator.interpolate( new double[] { 0f, 1f, Math.max(w - 1, h - 1) }, new double[] { 0f, 1f, Math.max(targetHeight - 1, targetWidth - 1) }); for (int i = 0; i < startPositions.size(); i++) { int start = startPositions.get(i); int end = endPositions.get(i); for (int j = (int) function.value(start); j <= (int) function.value(end); j++) { newData[j] = 0xff000000; } } BufferedImage img = UIUtil.createImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB); img.setRGB(0, 0, targetWidth, targetHeight, newData, 0, targetWidth); return img; } catch (Exception e) { Logger.getInstance(ImageUtils.class).error("resizeBorder", e); } return null; }
From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
private static BufferedImage rescaleBorder(BufferedImage image, int targetWidth, int targetHeight) { if (targetWidth == 0) { targetWidth = 1;/*ww w . j a v a 2 s . c om*/ } if (targetHeight == 0) { targetHeight = 1; } if (targetHeight > 1 && targetWidth > 1) { throw new Wrong9PatchException(); } int w = image.getWidth(); int h = image.getHeight(); int[] data = image.getRGB(0, 0, w, h, null, 0, w); int[] newData = new int[targetWidth * targetHeight]; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { newData[y * targetWidth + x] = 0x00; } } List<Integer> startPositions = new ArrayList<Integer>(); List<Integer> endPositions = new ArrayList<Integer>(); boolean inBlock = false; if (targetHeight == 1) { for (int x = 0; x < w; x++) { if ((0xff000000 & data[x]) != 0) { if (!inBlock) { inBlock = true; startPositions.add(x); } } else if (inBlock) { endPositions.add(x - 1); inBlock = false; } } if (inBlock) { endPositions.add(w - 1); } } else { for (int y = 0; y < h; y++) { if ((0xff000000 & data[y]) != 0) { if (!inBlock) { inBlock = true; startPositions.add(y); } } else if (inBlock) { endPositions.add(y - 1); inBlock = false; } } if (inBlock) { endPositions.add(h - 1); } } try { SplineInterpolator interpolator = new SplineInterpolator(); PolynomialSplineFunction function = interpolator.interpolate( new double[] { 0f, 1f, Math.max(w - 1, h - 1) }, new double[] { 0f, 1f, Math.max(targetHeight - 1, targetWidth - 1) }); for (int i = 0; i < startPositions.size(); i++) { int start = startPositions.get(i); int end = endPositions.get(i); for (int j = (int) function.value(start); j <= (int) function.value(end); j++) { newData[j] = 0xff000000; } } BufferedImage img = UIUtil.createImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB); img.setRGB(0, 0, targetWidth, targetHeight, newData, 0, targetWidth); return img; } catch (Exception e) { Logger.getInstance(ImageUtils.class).error("resizeBorder", e); } return null; }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
/** * ???//from www . j a v a2 s .c o m * * @param imgfile * @param width * @param height * @return */ public static BufferedImage shrinkAndTrimImage(BufferedImage imgfile, int width, int height) { int iwidth = imgfile.getWidth(); int iheight = imgfile.getHeight(); double ratio = Math.max((double) width / (double) iwidth, (double) height / (double) iheight); int shrinkedWidth; int shrinkedHeight; if ((iwidth <= width) || (iheight < height)) { shrinkedWidth = iwidth; shrinkedHeight = iheight; } else { shrinkedWidth = (int) (iwidth * ratio); shrinkedHeight = (int) (iheight * ratio); } // ?? Image targetImage = imgfile.getScaledInstance(shrinkedWidth, shrinkedHeight, Image.SCALE_AREA_AVERAGING); int w_size = targetImage.getWidth(null); int h_size = targetImage.getHeight(null); if (targetImage.getWidth(null) < width) { w_size = width; } if (targetImage.getHeight(null) < height) { h_size = height; } BufferedImage tmpImage = new BufferedImage(w_size, h_size, BufferedImage.TYPE_INT_RGB); Graphics2D g = tmpImage.createGraphics(); g.setBackground(Color.WHITE); g.setColor(Color.WHITE); // ?????????????? g.fillRect(0, 0, w_size, h_size); int diff_w = 0; int diff_h = 0; if (width > shrinkedWidth) { diff_w = (width - shrinkedWidth) / 2; } if (height > shrinkedHeight) { diff_h = (height - shrinkedHeight) / 2; } g.drawImage(targetImage, diff_w, diff_h, null); int _iwidth = tmpImage.getWidth(); int _iheight = tmpImage.getHeight(); BufferedImage _tmpImage; if (_iwidth > _iheight) { int diff = _iwidth - width; _tmpImage = tmpImage.getSubimage(diff / 2, 0, width, height); } else { int diff = _iheight - height; _tmpImage = tmpImage.getSubimage(0, diff / 2, width, height); } return _tmpImage; }
From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java
/** * Calculates and returns band histograms of a BufferedImage * * @param image/* w w w. j a v a2 s .com*/ * @return */ public static int[][] getChannelHistograms(BufferedImage image) { WritableRaster raster = image.getRaster(); int[][] histograms = new int[raster .getNumBands()][(int) (Math.pow(2, image.getColorModel().getPixelSize()) - 1)]; int width = image.getWidth(); int height = image.getHeight(); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { for (int band = 0; band < raster.getNumBands(); band++) { histograms[band][raster.getSample(col, row, band)]++; } } } return histograms; }
From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java
private static List<RectangularRegion> findSubImageInImage(BufferedImage subImage, BufferedImage image, int max) { Map<Integer, List<Point>> rgb2offsets = new HashMap<Integer, List<Point>>(); int sw = subImage.getWidth(); int sh = subImage.getHeight(); for (int x = 0; x < sw; ++x) { for (int y = 0; y < sh; ++y) { int argb = subImage.getRGB(x, y); int a = argb >>> 24; if (a == 255) { Integer rgb = argb & 0xFFFFFF; List<Point> offsets = rgb2offsets.get(rgb); if (offsets == null) { offsets = new ArrayList<Point>(); rgb2offsets.put(rgb, offsets); }/*from w w w . ja va 2 s .c o m*/ offsets.add(new Point(x, y)); } } } List<RectangularRegion> result = new ArrayList<RectangularRegion>(); int w = image.getWidth(); int h = image.getHeight(); int[][] p = new int[w][h]; Raster raster = image.getRaster(); if (raster.getTransferType() == DataBuffer.TYPE_BYTE) { byte[] bytes = (byte[]) raster.getDataElements(0, 0, w, h, null); int bytesPerPixel = (bytes.length / (w * h)); ColorModel colorModel = image.getColorModel(); byte[] buf = new byte[bytesPerPixel]; for (int x = 0; x < w; ++x) { for (int y = 0; y < h; ++y) { System.arraycopy(bytes, (x + y * w) * bytesPerPixel, buf, 0, bytesPerPixel); p[x][y] = colorModel.getRGB(buf) & 0xFFFFFF; } } } else if (raster.getTransferType() == DataBuffer.TYPE_INT) { for (int x = 0; x < w; ++x) { p[x] = (int[]) raster.getDataElements(x, 0, 1, h, null); } } else { throw new RuntimeException("findSubImageInImage not implemented for image transfer type " + raster.getTransferType() + " yet."); } for (int x = 0; x < w; ++x) { for (int y = 0; y < h; ++y) { Iterator<Map.Entry<Integer, List<Point>>> i = rgb2offsets.entrySet().iterator(); compareWithSubImageLoop: while (i.hasNext()) { Map.Entry<Integer, List<Point>> mapEntry = i.next(); int expectedRgb = mapEntry.getKey(); for (Point offset : mapEntry.getValue()) { int xx = x + offset.x; int yy = y + offset.y; if (xx >= w || yy >= h || expectedRgb != p[xx][yy]) { break compareWithSubImageLoop; } } if (!i.hasNext()) { result.add(new RectangularRegion(x, y, x + (sw - 1), y + (sh - 1))); if (result.size() == max) { return result; } } } } } return result; }
From source file:com.silverpeas.gallery.MediaHelper.java
/** * Sets the resolution of a photo.// ww w.j a va 2s. c om * @param image * @param photo */ private static void setResolution(final BufferedImage image, final Photo photo) { if (image == null) { photo.setDefinition(Definition.fromZero()); } else { photo.setDefinition(Definition.of(image.getWidth(), image.getHeight())); } }
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);/* ww w .j a va2s. c om*/ return returnValue; }
From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java
public static JSONObject createThumbnail(File projectDir, File pyramidDir, String imageName, BufferedImage refImage) throws IOException, JSONException { final Path projectScope = projectDir.toPath(); final BufferedImage tmbImage = toThumbnailSize(refImage); final File thumbnailDir = new File(pyramidDir.getParentFile(), ".thumbnail"); thumbnailDir.mkdir();/* www. ja v a 2 s . c o m*/ final File refFile = File.createTempFile(imageName + ".", ".jpg", thumbnailDir); final File tmbFile = tmbImage != refImage ? File.createTempFile(imageName + ".tmb.", ".jpg", thumbnailDir) : refFile; final JSONObject thumbnail = new JSONObject(); thumbnail.put("scope", "Project"); thumbnail.put("width", tmbImage.getWidth()); thumbnail.put("height", tmbImage.getHeight()); thumbnail.put("src", toRef(projectScope, tmbFile.toPath())); ImageIO.write(refImage, "JPEG", refFile); if (tmbFile != refFile) { thumbnail.put("ref", toRef(projectScope, refFile.toPath())); ImageIO.write(tmbImage, "JPEG", tmbFile); } return thumbnail; }