List of usage examples for java.awt.image BufferedImage getWidth
public int getWidth()
From source file:info.magnolia.filesystembrowser.app.imageprovider.FSImageProvider.java
private Resource scaleImage(File file) { FileInputStream fis = null;/* w ww . j av a 2 s .c o m*/ try { fis = new FileInputStream(file); final BufferedImage img = ImageIO.read(fis); if (img != null) { int width = img.getWidth(); int height = img.getHeight(); BufferedImage result = img; if (width > 200) { double ratio = (double) height / width; int newWidth = 200; int newHeight = (int) (newWidth * ratio); result = new ScaleFilter(newWidth, newHeight).filter(img, null); result = new CropFilter(0, 0, newWidth, newHeight).filter(result, null); } final InputStream is = createStreamSource(result, "png"); StreamResource.StreamSource streamSource = new StreamResource.StreamSource() { @Override public InputStream getStream() { return is; } }; return new StreamResource(streamSource, "image" + System.currentTimeMillis() + ".png"); } } catch (IOException e) { log.error("Failed to scale a resource {}.", file, e); } finally { IOUtils.closeQuietly(fis); } return null; }
From source file:doge.photo.DogePhotoManipulator.java
private BufferedImage manipulate(BufferedImage sourceImage) { double aspectRatio = sourceImage.getHeight() / (double) sourceImage.getWidth(); int height = (int) Math.floor(IMAGE_WIDTH * aspectRatio); BufferedImage destinationImage = new BufferedImage(IMAGE_WIDTH, height, BufferedImage.TYPE_INT_RGB); render(sourceImage, destinationImage); return destinationImage; }
From source file:org.nekorp.workflow.desktop.servicio.imp.ImageServiceImp.java
@Override public BufferedImage resizeToStandarSize(BufferedImage image) { Dimension boundary = new Dimension(imageWidth, imageHeight); Dimension originalDimension = new Dimension(image.getWidth(), image.getHeight()); Dimension newDimension = ImageServiceImp.getScaledDimension(originalDimension, boundary); ResampleOp resampleOp = new ResampleOp((int) newDimension.getWidth(), (int) newDimension.getHeight()); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); BufferedImage rescaled = resampleOp.filter(image, null); return rescaled; }
From source file:org.nekorp.workflow.desktop.servicio.imp.ImageServiceImp.java
@Override public BufferedImage getThumbnail(BufferedImage image) { Dimension boundary = new Dimension(thumbWidth, thumbHeight); Dimension originalDimension = new Dimension(image.getWidth(), image.getHeight()); Dimension newDimension = ImageServiceImp.getScaledDimension(originalDimension, boundary); ResampleOp resampleOp = new ResampleOp((int) newDimension.getWidth(), (int) newDimension.getHeight()); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); BufferedImage rescaled = resampleOp.filter(image, null); return rescaled; }
From source file:DBScan.java
private double minDistance(int x, int y, BufferedImage img1, BufferedImage img2) { if (!(x < img1.getWidth() && y < img1.getHeight())) return 0; int colour = img1.getRGB(x, y); // if (colour != Image.BLACK) // return 0; if (x < img2.getWidth() && y < img2.getHeight() && img2.getRGB(x, y) == colour) return 0; int distance = 6; final int RANGE = 1; for (int x1 = x - RANGE; x1 <= x + RANGE; x1++) { if (distance == 1) break; if (x1 < 0 || x1 >= img2.getWidth()) continue; for (int y1 = y - (x1 - x); y1 <= y + RANGE; y1++) { if (y1 >= 0 && y1 < img2.getHeight() && img2.getRGB(x1, y1) == colour) { distance = Math.min(distance, Math.min(Math.abs(x - x1), Math.abs(y - y1))); }// w ww .j a v a 2s .co m } } return (distance * distance) / 2; }
From source file:io.bci.BitcoinSTLGenerator.java
private static void drawSTL(File qrFile, String filename) { int height = 0; int width = 0; float PLATFORM_RATIO = 2.3f; float PLATFORM_HEIGHT = 1.0f; boolean printPlatform = false; boolean printQRbottom = true; STLDrawer drawer = null;/* w w w . j a va2 s. c om*/ float[] pofloat1 = new float[3]; float[] pofloat2 = new float[3]; float[] pofloat3 = new float[3]; float[] pofloat4 = new float[3]; Color color; int count; BufferedImage image = null; try { drawer = new STLDrawer("STLs/" + filename + ".stl"); } catch (FileNotFoundException e) { System.out.println("ERROR: Unable to create stl file! - " + filename + ".stl"); e.printStackTrace(); } try { image = javax.imageio.ImageIO.read(qrFile); height = image.getHeight(); width = image.getWidth(); } catch (IOException e) { System.out.println("ERROR: Unable to open picture file! - " + qrFile.toString()); e.printStackTrace(); } if (printPlatform) { //Base background pofloat1[0] = 0.0f; pofloat1[1] = 0.0f; pofloat1[2] = 0; pofloat2[0] = 0.0f + (float) width; pofloat2[1] = 0.0f + 0.0f; pofloat2[2] = 0; pofloat3[0] = 0.0f + (float) width; pofloat3[1] = 0.0f + (float) height; pofloat3[2] = 0; pofloat4[0] = 0.0f + 0.0f; pofloat4[1] = 0.0f + (float) height; pofloat4[2] = 0; drawer.drawCube(pofloat1, pofloat2, pofloat3, pofloat4, PLATFORM_HEIGHT); //end base background } int[][] drawArray = new int[width + 2][height + 2]; for (int jj = 0; jj < height; jj++) { for (int ii = 0; ii < width; ii++) { color = new Color(image.getRGB(ii, jj)); if (color.equals(Color.BLACK)) { drawArray[ii + 1][height - jj + 1] = 1; //PNG origin starts at top left of image, my origin is bottom left } } } count = 0; for (int jj = 1; jj < height + 2; jj++) //drawTop { for (int ii = 1; ii < width + 2; ii++) { if (drawArray[ii][jj] == 1) { while ((ii + count < width + 2) && (drawArray[ii + count][jj] == 1) && (drawArray[ii + count][jj + 1] == 0)) { count++; } pofloat1[0] = (float) ii; pofloat1[1] = (float) jj; pofloat1[2] = PLATFORM_HEIGHT; pofloat4[0] = (float) ii + 0.0f; pofloat4[1] = (float) jj + 1.0f; pofloat4[2] = PLATFORM_HEIGHT; pofloat3[0] = (float) ii + count;//1.0f; pofloat3[1] = (float) jj + 1.0f; pofloat3[2] = PLATFORM_HEIGHT; pofloat2[0] = (float) ii + 1.0f; pofloat2[1] = (float) jj + 0.0f; pofloat2[2] = PLATFORM_HEIGHT; ii = ii + count; if (count != 0) { drawer.drawTop(pofloat1, pofloat2, pofloat3, pofloat4, PLATFORM_RATIO); } count = 0; } } } count = 0; for (int jj = 1; jj < height + 2; jj++) //bottom { for (int ii = 1; ii < width + 2; ii++) { if (drawArray[ii][jj] == 1) { while ((ii + count < width + 2) && (drawArray[ii + count][jj] == 1) && (drawArray[ii + count][jj - 1] == 0)) { count++; } pofloat1[0] = (float) ii; pofloat1[1] = (float) jj; pofloat1[2] = PLATFORM_HEIGHT; pofloat4[0] = (float) ii + 0.0f; pofloat4[1] = (float) jj + 1.0f; pofloat4[2] = PLATFORM_HEIGHT; pofloat3[0] = (float) ii + 1.0f; pofloat3[1] = (float) jj + 1.0f; pofloat3[2] = PLATFORM_HEIGHT; pofloat2[0] = (float) ii + count;//1.0f; pofloat2[1] = (float) jj + 0.0f; pofloat2[2] = PLATFORM_HEIGHT; if (count != 0) { drawer.drawBottom(pofloat1, pofloat2, pofloat3, pofloat4, PLATFORM_RATIO); } ii = ii + count; count = 0; } } } for (int ii = 1; ii < width + 2; ii++) //draw right { for (int jj = 1; jj < height + 2; jj++) { if (drawArray[ii][jj] == 1) { while ((jj + count < height + 2) && (drawArray[ii][jj + count] == 1) && (drawArray[ii + 1][jj + count] == 0)) { count++; } pofloat1[0] = (float) ii; pofloat1[1] = (float) jj; pofloat1[2] = PLATFORM_HEIGHT; pofloat4[0] = (float) ii + 0.0f; pofloat4[1] = (float) jj + 1.0f; pofloat4[2] = PLATFORM_HEIGHT; pofloat3[0] = (float) ii + 1.0f; pofloat3[1] = (float) jj + count;//1.0f; pofloat3[2] = PLATFORM_HEIGHT; pofloat2[0] = (float) ii + 1.0f; pofloat2[1] = (float) jj + 0.0f; pofloat2[2] = PLATFORM_HEIGHT; if (count != 0) { drawer.drawRight(pofloat1, pofloat2, pofloat3, pofloat4, PLATFORM_RATIO); } jj = jj + count; count = 0; } } } for (int ii = 1; ii < width + 2; ii++) //draw left { for (int jj = 1; jj < height + 2; jj++) { if (drawArray[ii][jj] == 1) { while ((jj + count < height + 2) && (drawArray[ii][jj + count] == 1) && (drawArray[ii - 1][jj + count] == 0)) { count++; } pofloat1[0] = (float) ii; pofloat1[1] = (float) jj; pofloat1[2] = PLATFORM_HEIGHT; pofloat4[0] = (float) ii + 0.0f; pofloat4[1] = (float) jj + count;//1.0f; pofloat4[2] = PLATFORM_HEIGHT; pofloat3[0] = (float) ii + 1.0f; pofloat3[1] = (float) jj + 1.0f; pofloat3[2] = PLATFORM_HEIGHT; pofloat2[0] = (float) ii + 1.0f; pofloat2[1] = (float) jj + 0.0f; pofloat2[2] = PLATFORM_HEIGHT; if (count != 0) { drawer.drawLeft(pofloat1, pofloat2, pofloat3, pofloat4, PLATFORM_RATIO); } jj = jj + count; count = 0; } } } for (int jj = 1; jj < height + 2; jj++) //draw back { for (int ii = 1; ii < width + 2; ii++) { if (drawArray[ii][jj] == 1) { while ((ii + count < width + 2) && (drawArray[ii + count][jj] == 1)) { count++; } pofloat1[0] = (float) ii; pofloat1[1] = (float) jj; pofloat1[2] = PLATFORM_HEIGHT; pofloat4[0] = (float) ii + 0.0f; pofloat4[1] = (float) jj + 1.0f; pofloat4[2] = PLATFORM_HEIGHT; pofloat3[0] = (float) ii + count;//1.0f; pofloat3[1] = (float) jj + 1.0f; pofloat3[2] = PLATFORM_HEIGHT; pofloat2[0] = (float) ii + count;//1.0f; pofloat2[1] = (float) jj + 0.0f; pofloat2[2] = PLATFORM_HEIGHT; if (count != 0) { drawer.drawBack(pofloat1, pofloat2, pofloat3, pofloat4, PLATFORM_RATIO); } ii = ii + count; count = 0; } } } if (printQRbottom) { for (int jj = 1; jj < height + 2; jj++) //draw front { for (int ii = 1; ii < width + 2; ii++) { if (drawArray[ii][jj] == 1) { while ((ii + count < width + 2) && (drawArray[ii + count][jj] == 1)) { count++; } pofloat1[0] = (float) ii; pofloat1[1] = (float) jj; pofloat1[2] = PLATFORM_HEIGHT; pofloat4[0] = (float) ii + 0.0f; pofloat4[1] = (float) jj + 1.0f; pofloat4[2] = PLATFORM_HEIGHT; pofloat3[0] = (float) ii + count;//1.0f; pofloat3[1] = (float) jj + 1.0f; pofloat3[2] = PLATFORM_HEIGHT; pofloat2[0] = (float) ii + count;//1.0f; pofloat2[1] = (float) jj + 0.0f; pofloat2[2] = PLATFORM_HEIGHT; if (count != 0) { drawer.drawFront(pofloat1, pofloat2, pofloat3, pofloat4, PLATFORM_RATIO); } ii = ii + count; count = 0; } } } } drawer.resizeNumTriangles(); System.out.println("STL drawn: " + filename + ".stl"); drawer.closeFile(); }
From source file:net.sf.jabref.gui.PdfPreviewPanel.java
private BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) { int h = originalImage.getHeight(); int w = originalImage.getWidth(); if ((height == 0) || (width == 0)) { height = h;/*w ww. jav a2 s.c o m*/ width = w; } else { float factorH = (float) height / (float) h; float factorW = (float) width / (float) w; if (factorH < factorW) { // use factorH, only width has to be changed as height is // already correct width = Math.round(w * factorH); } else { width = Math.round(h * factorW); } } BufferedImage resizedImage = new BufferedImage(width, height, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, height, null); return resizedImage; }
From source file:com.gst.infrastructure.documentmanagement.data.ImageData.java
public void resizeImage(InputStream in, OutputStream out, int maxWidth, int maxHeight) throws IOException { BufferedImage src = ImageIO.read(in); if (src.getWidth() <= maxWidth && src.getHeight() <= maxHeight) { out.write(getContent());/*www .j av a 2 s .c o m*/ return; } float widthRatio = (float) src.getWidth() / maxWidth; float heightRatio = (float) src.getHeight() / maxHeight; float scaleRatio = widthRatio > heightRatio ? widthRatio : heightRatio; // TODO(lindahl): Improve compressed image quality (perhaps quality // ratio) int newWidth = (int) (src.getWidth() / scaleRatio); int newHeight = (int) (src.getHeight() / scaleRatio); int colorModel = fileExtension == ContentRepositoryUtils.IMAGE_FILE_EXTENSION.JPEG ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage target = new BufferedImage(newWidth, newHeight, colorModel); Graphics2D g = target.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(src, 0, 0, newWidth, newHeight, Color.BLACK, null); g.dispose(); ImageIO.write(target, fileExtension != null ? fileExtension.getValueWithoutDot() : "jpeg", out); }
From source file:main.java.whiteSocket.Bloop.java
public static BufferedImage loadSketch() throws Exception { /**/*from w w w.java 2s . c o m*/ * load image from DB table - either an input sketch or a compiled blooprint image * sketch arg = "null" * BLOB object to binary stream to BufferedImage object * */ System.out.println("loadSketch() from " + sketchFile); InputStream stream = null; BufferedImage some = null; try { /** * http://stackoverflow.com/questions/39081215/access-a-resource-outside-a-jar-from-the-jar * */ stream = Bloop.class.getClass().getResourceAsStream(sketchFile); // stream = null; if (!jarMode) { some = ImageIO.read(new File(sketchFile)); Stretch.fx = some.getWidth() - 1; Stretch.gx = some.getWidth() - 1; Stretch.fy = some.getHeight() - 1; Stretch.hy = some.getHeight() - 1; return some; } System.out.println("sketch stream = " + stream); } catch (Exception e) { System.out.print("error sketch to stream: "); System.out.println(e.getMessage()); } try { some = ImageIO.read(stream); Stretch.fx = some.getWidth() - 1; Stretch.gx = some.getWidth() - 1; Stretch.fy = some.getHeight() - 1; Stretch.hy = some.getHeight() - 1; } catch (Exception exc) { System.out.print("error stream to image: "); System.out.println(exc.getMessage()); } return some; }
From source file:net.rptools.maptool.util.PersistenceUtil.java
public static void saveToken(Token token, File file, boolean doWait) throws IOException { // Thumbnail/*from w w w . j av a2 s .c o m*/ BufferedImage image = null; if (doWait) image = ImageManager.getImageAndWait(token.getImageAssetId()); else image = ImageManager.getImage(token.getImageAssetId()); Dimension sz = new Dimension(image.getWidth(), image.getHeight()); SwingUtil.constrainTo(sz, 50); BufferedImage thumb = new BufferedImage(sz.width, sz.height, BufferedImage.TRANSLUCENT); Graphics2D g = thumb.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(image, 0, 0, sz.width, sz.height, null); g.dispose(); PackedFile pakFile = null; try { pakFile = new PackedFile(file); saveAssets(token.getAllImageAssets(), pakFile); pakFile.putFile(Token.FILE_THUMBNAIL, ImageUtil.imageToBytes(thumb, "png")); pakFile.setContent(token); pakFile.setProperty(PROP_VERSION, MapTool.getVersion()); pakFile.save(); } finally { if (pakFile != null) pakFile.close(); } }