List of usage examples for java.awt.image BufferedImage getSubimage
public BufferedImage getSubimage(int x, int y, int w, int h)
From source file:uk.co.modularaudio.service.bufferedimageallocation.impl.cache.AllocationCacheForImageType.java
public TiledBufferedImage makeSubimageFromExistingFree(final String allocationSource, final AllocationMatch allocationMatchToUse, final FreeEntry freeEntryToChopUp, final int imageWidthToUse, final int imageHeightToUse) { // Remove the existing free entry, it'll be replaced with what's necessary unsynchronisedRemoveFreeEntry(freeEntryToChopUp); final long assignedId = imageAllocationAssignedIdCounter++; final RawImage rawImage = freeEntryToChopUp.getSourceRawImage(); final int freeEntryStartX = freeEntryToChopUp.getX(); final int freeEntryWidth = freeEntryToChopUp.getWidth(); final int freeEntryStartY = freeEntryToChopUp.getY(); final int freeEntryHeight = freeEntryToChopUp.getHeight(); final BufferedImage rootBufferedImage = rawImage.getRootBufferedImage(); // Don't clear the image, leave it up to consumer to fix up properly // Since we don't know if they want it transparent, black etc final BufferedImage subImage = rootBufferedImage.getSubimage(freeEntryStartX, freeEntryStartY, imageWidthToUse, imageHeightToUse); final UsedEntry usedEntry = new UsedEntry(allocationSource, rawImage, assignedId, freeEntryStartX, freeEntryStartY, imageWidthToUse, imageHeightToUse, subImage); unsychronisedAddUsedEntry(usedEntry); // Create free entries for any left space if (imageWidthToUse != freeEntryWidth || imageHeightToUse != freeEntryHeight) { // Initial assignment is always top left // int widthLeft = freeEntryWidth - imageWidthToUse; // int heightLeft = freeEntryHeight - imageHeightToUse; // log.debug("Creating free entries after sub image of ( " + imageWidthToUse+", " + imageHeightToUse + " ) sizesleft ( " + widthLeft + ", " + heightLeft + " )"); // One of the dimensions is probably smaller than the other - we want to create one big space and // one little one. BlockDecomposer.decomposeImage(decompositionForAllocation, freeEntryWidth, freeEntryHeight, imageWidthToUse, imageHeightToUse); // log.debug("Smaller block is " + decompositionForAllocation.smallBlockToString() ); // log.debug("Larger block is " + decompositionForAllocation.largeBlockToString() ); // Need to add back on the original offsets final FreeEntry smallerFe = new FreeEntry(rawImage, freeEntryStartX + decompositionForAllocation.smallerBlockX, freeEntryStartY + decompositionForAllocation.smallerBlockY, decompositionForAllocation.smallerBlockWidth, decompositionForAllocation.smallerBlockHeight); unsynchronisedAddFreeEntry(smallerFe); if (decompositionForAllocation.hasLarger) { final FreeEntry largerFe = new FreeEntry(rawImage, freeEntryStartX + decompositionForAllocation.largerBlockX, freeEntryStartY + decompositionForAllocation.largerBlockY, decompositionForAllocation.largerBlockWidth, decompositionForAllocation.largerBlockHeight); unsynchronisedAddFreeEntry(largerFe); }//from www . j av a 2s .c o m } else { // The allocation exactly matches the raw image, don't add any free entries for it. // log.debug("Allocation matches image - no free entry creation."); } // Return the necessary structure final TiledBufferedImageImpl theImpl = new TiledBufferedImageImpl(this, usedEntry, subImage); allocationMatchToUse.setFoundTile(theImpl); return allocationMatchToUse.getFoundTile(); }
From source file:uk.co.modularaudio.service.bufferedimageallocation.impl.cache.AllocationCacheForImageType.java
public TiledBufferedImage allocateNewRawImageMakeSubimage(final String allocationSource, final AllocationMatch allocationMatchToUse, final int imageWidthToUse, final int imageHeightToUse) throws DatastoreException { internalLock.lock();/*from ww w . j a va2s .c o m*/ try { // Check if the image falls in the bounds of what we cache. int widthToAlloc = -1; int heightToAlloc = -1; if (stdAllocImageWidth >= imageWidthToUse && stdAllocImageHeight >= imageHeightToUse) { widthToAlloc = stdAllocImageWidth; heightToAlloc = stdAllocImageHeight; } else { // The requested image is larger in one or both dimensions of what we cache. // Create a custom assignment widthToAlloc = imageWidthToUse; heightToAlloc = imageHeightToUse; if (log.isDebugEnabled()) { log.debug("Allocating image larger than cache dimensions: " + imageWidthToUse + ", " + imageHeightToUse); } } // Allocate raw image final BufferedImage bufferedImage = doRealAllocation(widthToAlloc, heightToAlloc); final Graphics2D rawGraphics = bufferedImage.createGraphics(); final long rawImageId = imageAllocationRawIdCounter++; final RawImage rawImage = new RawImage(bufferedImage, rawGraphics, rawImageId); unsynchronisedAddRawImage(rawImageId, rawImage); // Create a subimage and used entry final long assignedId = imageAllocationAssignedIdCounter++; final BufferedImage subImage = bufferedImage.getSubimage(0, 0, imageWidthToUse, imageHeightToUse); final UsedEntry usedEntry = new UsedEntry(allocationSource, rawImage, assignedId, 0, 0, imageWidthToUse, imageHeightToUse, subImage); unsychronisedAddUsedEntry(usedEntry); // Create free entries for any left space if (imageWidthToUse != widthToAlloc || imageHeightToUse != heightToAlloc) { // Initial assignment is always top left // int widthLeft = widthToAlloc - imageWidthToUse; // int heightLeft = heightToAlloc - imageHeightToUse; // log.debug("Creating free entries after sub image of ( " + imageWidthToUse+", " + imageHeightToUse + " ) sizesleft ( " + widthLeft + ", " + heightLeft + " )"); // One of the dimensions is probably smaller than the other - we want to create one big space and // one little one. BlockDecomposer.decomposeImage(decompositionForAllocation, widthToAlloc, heightToAlloc, imageWidthToUse, imageHeightToUse); // log.debug("Smaller block is " + decompositionForAllocation.smallBlockToString() ); // log.debug("Larger block is " + decompositionForAllocation.largeBlockToString() ); final FreeEntry smallerFe = new FreeEntry(rawImage, decompositionForAllocation.smallerBlockX, decompositionForAllocation.smallerBlockY, decompositionForAllocation.smallerBlockWidth, decompositionForAllocation.smallerBlockHeight); unsynchronisedAddFreeEntry(smallerFe); if (decompositionForAllocation.hasLarger) { final FreeEntry largerFe = new FreeEntry(rawImage, decompositionForAllocation.largerBlockX, decompositionForAllocation.largerBlockY, decompositionForAllocation.largerBlockWidth, decompositionForAllocation.largerBlockHeight); unsynchronisedAddFreeEntry(largerFe); } } else { // The allocation exactly matches the raw image, don't add any free entries for it. // log.debug("Allocation matches image - no free entry creation."); } // Return the necessary structure final TiledBufferedImageImpl theImpl = new TiledBufferedImageImpl(this, usedEntry, subImage); allocationMatchToUse.setFoundTile(theImpl); } finally { internalLock.unlock(); } return allocationMatchToUse.getFoundTile(); }
From source file:zz.pseas.ghost.utils.BrowserUtil.java
public static byte[] captureScreenShotById(WebDriver driver, String id) throws IOException { byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); WebElement ele = driver.findElement(By.id(id)); ByteArrayInputStream in = new ByteArrayInputStream(bytes); BufferedImage image = ImgUtil.bytesToBImage(bytes); ImageIO.write(image, "jpg", new File("d:/big.jpg")); in.close();/* w w w .j a va 2 s .co m*/ System.out.println(image.getType()); Point p = ele.getLocation(); Dimension xy = ele.getSize(); BufferedImage subImage = image.getSubimage(p.getX(), p.getY(), xy.getWidth(), xy.getHeight()); ImageIO.write(subImage, "jpg", new File("d:/sc.jpg")); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(subImage, "jpg", out); out.flush(); byte[] res = out.toByteArray(); out.close(); return res; }