List of usage examples for java.awt.image BufferedImage getType
public int getType()
From source file:algorithm.ImageImageFrameExpanding.java
/** * Append restoration metadata. Size of carrier and payload files is added. * //ww w. j av a 2s . co m * @param carrier * @param payload * @return metadata image * @throws IOException */ protected BufferedImage getMetadataImage(File carrier, File payload) throws IOException { BufferedImage carrierBuffered = ImageIO.read(carrier); BufferedImage payloadBuffered = ImageIO.read(payload); BufferedImage metadataImage = new BufferedImage( Math.max(carrierBuffered.getWidth(), payloadBuffered.getWidth()), METADATA_HEIGHT, carrierBuffered.getType()); colorizeImage(metadataImage, Color.blue.getRGB()); File metadataImageFile = new File(OUTPUT_DIRECTORY + "metadataImage.png"); writeImage(metadataImageFile, metadataImage); File metadataFile = new File(OUTPUT_DIRECTORY + "tmpMetadataText.txt"); tmpFiles.add(metadataFile); PayloadSegment payloadSegment = new PayloadSegment(carrier, payload, this); // add height and width of carrier and payload: payloadSegment.addOptionalProperty("carrierWidth", "" + carrierBuffered.getWidth()); payloadSegment.addOptionalProperty("carrierHeight", "" + carrierBuffered.getHeight()); payloadSegment.addOptionalProperty("payloadWidth", "" + payloadBuffered.getWidth()); payloadSegment.addOptionalProperty("payloadHeight", "" + payloadBuffered.getHeight()); byte[] metadata = payloadSegment.getRestorationMetadataBytes(); FileUtils.writeByteArrayToFile(metadataFile, metadata); OpenStegoRandomLSBSteganography lsbAlgorithm = new OpenStegoRandomLSBSteganography(); File tmpOutputFile = lsbAlgorithm.encapsulate(metadataImageFile, metadataFile); tmpFiles.add(tmpOutputFile); metadataImage = ImageIO.read(tmpOutputFile); return metadataImage; }
From source file:it.tidalwave.imageio.test.ImageReaderTestSupport.java
/******************************************************************************************************************* * //w w w .j a v a 2 s . com * ******************************************************************************************************************/ @Nonnull protected BufferedImage assertLoadImage(final @Nonnull ImageReader ir, final @CheckForNull RAWImageReadParam readParam, final @Nonnegative int width, final @Nonnegative int height, final @Nonnegative int bandCount, final @Nonnegative int sampleSize, final int type) throws IOException { final BufferedImage image = ir.read(0, readParam); assertNotNull(image); final Dimension imageSize = new Dimension(image.getWidth(), image.getHeight()); final Dimension expectedSize = new Dimension(width, height); assertEquals("loaded image size: ", expectedSize, imageSize); assertEquals("loaded image band count", bandCount, image.getData().getNumBands()); assertEquals(type, image.getType()); for (int i = 0; i < bandCount; i++) { assertEquals("loaded image sample size for band " + i, sampleSize, image.getData().getSampleModel().getSampleSize(i)); } return image; }
From source file:com.mikenimer.familydam.services.photos.ThumbnailService.java
/** * Resizes an image using a Graphics2D object backed by a BufferedImage. * @param src - source image to scale//from w w w . j a v a 2 s.c om * @param w - desired width * @param h - desired height * @return - the new resized image */ private BufferedImage getScaledImage(BufferedImage src, int orientation, int w, int h) throws Exception { int finalW = w; int finalH = h; if (h == -1) finalH = w; if (w == -1) finalW = h; double factor = 1.0d; if (src.getWidth() > src.getHeight()) { factor = ((double) src.getHeight() / (double) src.getWidth()); finalH = (int) (finalW * factor); } else { factor = ((double) src.getWidth() / (double) src.getHeight()); finalW = (int) (finalH * factor); } BufferedImage scaledImage = new BufferedImage(finalW, finalH, src.getType()); Graphics2D g = scaledImage.createGraphics(); try { //AffineTransform at = AffineTransform.getScaleInstance((double) finalw / src.getWidth(), (double) finalh/ src.getHeight()); AffineTransform at = getExifTransformation(orientation, (double) finalW, (double) finalH, (double) finalW / src.getWidth(), (double) finalH / src.getHeight()); return transformImage(src, at); //g.drawRenderedImage(src, at); //return scaledImage; } finally { g.dispose(); } /** BufferedImage resizedImg = new BufferedImage(finalw, finalh, src.getType()); Graphics2D g2 = resizedImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(src, 0, 0, finalw, finalh, null); g2.dispose(); return resizedImg; **/ }
From source file:TextureByReference.java
public void setByReference(boolean b) { // double check that changing is necessary if (b != images[0].isByReference()) { // these are the same for all images so get info once int format = images[0].getFormat(); boolean yUp = images[0].isYUp(); // reset yUp on all the images // have to new ImageComponents because cannot set value for (int i = 0; i < images.length; i++) { // if the bImage type does not match currentType and we are // setting // to byRef we need to convert it BufferedImage bImage = images[i].getImage(); if (bImage.getType() != currentType && b) { // bImage = ImageOps.convertImage(bImage, currentType); if (currentType != BufferedImage.TYPE_CUSTOM) { bImage = ImageOps.convertImage(bImage, currentType); } else if (customType == this.TYPE_CUSTOM_RGBA) { bImage = ImageOps.convertToCustomRGBA(bImage); } else { bImage = ImageOps.convertToCustomRGB(bImage); }/* w w w. j a v a 2 s . com*/ } images[i] = new ImageComponent2D(format, bImage, b, yUp); images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ); images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ); } } }
From source file:de.uni_siegen.wineme.come_in.thumbnailer.thumbnailers.PDFBoxThumbnailer.java
private BufferedImage convertToImage(final PDPage page, final int imageType, final int thumbWidth, final int thumbHeight)/* */ throws IOException /* */ {//from w w w . j av a 2 s. c om /* 707 */ final PDRectangle mBox = page.findMediaBox(); /* 708 */ final float widthPt = mBox.getWidth(); /* 709 */ final float heightPt = mBox.getHeight(); /* 711 */ final int widthPx = thumbWidth; // Math.round(widthPt * scaling); /* 712 */ final int heightPx = thumbHeight; // Math.round(heightPt * scaling); /* 710 */ final double scaling = Math.min((double) thumbWidth / widthPt, (double) thumbHeight / heightPt); // resolution / 72.0F; /* */ /* 714 */ final Dimension pageDimension = new Dimension((int) widthPt, (int) heightPt); /* */ /* 716 */ final BufferedImage retval = new BufferedImage(widthPx, heightPx, imageType); /* 717 */ final Graphics2D graphics = (Graphics2D) retval.getGraphics(); /* 718 */ graphics.setBackground(PDFBoxThumbnailer.TRANSPARENT_WHITE); /* 719 */ graphics.clearRect(0, 0, retval.getWidth(), retval.getHeight()); /* 720 */ graphics.scale(scaling, scaling); /* 721 */ final PageDrawer drawer = new PageDrawer(); /* 722 */ drawer.drawPage(graphics, page, pageDimension); /* */ try /* */ { /* 728 */ final int rotation = page.findRotation(); /* 729 */ if (rotation == 90 || rotation == 270) /* */ { /* 731 */ final int w = retval.getWidth(); /* 732 */ final int h = retval.getHeight(); /* 733 */ final BufferedImage rotatedImg = new BufferedImage(w, h, retval.getType()); /* 734 */ final Graphics2D g = rotatedImg.createGraphics(); /* 735 */ g.rotate(Math.toRadians(rotation), w / 2, h / 2); /* 736 */ g.drawImage(retval, null, 0, 0); /* */ } /* */ } /* */ catch (final ImagingOpException e) /* */ { /* 741 */ //log.warn("Unable to rotate page image", e); /* */ } /* */ /* 744 */ return retval; /* */ }
From source file:TextureByReference.java
public void setYUp(boolean b) { // double check that changing yUp is necessary if (b != images[0].isYUp()) { // these are the same for all images so get info once int format = images[0].getFormat(); boolean byRef = images[0].isByReference(); // reset yUp on all the images -- have to new ImageComponents // because // cannot change the value at runtime for (int i = 0; i < images.length; i++) { // if we are byRef and the bImage type does not match // currentType // we need to convert it. If we are not byRef we will // save converting until it is changed to byRef BufferedImage bImage = images[i].getImage(); if (byRef && bImage.getType() != currentType) { // bImage = ImageOps.convertImage(bImage, currentType); if (currentType != BufferedImage.TYPE_CUSTOM) { bImage = ImageOps.convertImage(bImage, currentType); } else if (customType == this.TYPE_CUSTOM_RGBA) { bImage = ImageOps.convertToCustomRGBA(bImage); } else { bImage = ImageOps.convertToCustomRGB(bImage); }// www . j av a 2 s .c o m } images[i] = new ImageComponent2D(format, bImage, byRef, b); images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ); images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ); } } }
From source file:algo.PlotBar.java
private BufferedImage bufferResize(BufferedImage original, double widthFactor, double heightFactor) { // original image width & height int w, h;/*from w w w . j a v a2 s. com*/ w = original.getHeight(); h = original.getWidth(); // System.out.println(original.getHeight()); // System.out.println(original.getWidth()); // new width & height calculated by multiplying factor int newWidth = new Double(original.getWidth() * widthFactor).intValue(); int newHeight = new Double(original.getWidth() * heightFactor).intValue(); // new resized image BufferedImage buffResized = new BufferedImage(newWidth, newHeight, original.getType()); Graphics2D g = buffResized.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(original, 0, 0, newWidth, newHeight, 0, 0, w, h, null); g.dispose(); return buffResized; }
From source file:mod.rankshank.arbitraria.client.texture.TextureTiled.java
@Override public void loadTexture(IResourceManager resourceManager) throws IOException { this.deleteGlTexture(); IResource iresource = null;/* ww w. j av a 2 s .c o m*/ try { iresource = resourceManager.getResource(tileSource); BufferedImage img = TextureUtil.readBufferedImage(iresource.getInputStream()); boolean blur = false; boolean clamp = false; if (iresource.hasMetadata()) { try { TextureMetadataSection texturemetadatasection = iresource.getMetadata("texture"); if (texturemetadatasection != null) { blur = texturemetadatasection.getTextureBlur(); clamp = texturemetadatasection.getTextureClamp(); } } catch (RuntimeException runtimeexception) { Arbitraria.error(new Throwable( String.format("Issue loading tiled texture for %s, aborting", tileSource.toString()), runtimeexception)); } } int width = img.getWidth(); int height = img.getHeight(); if (width != height) { Arbitraria.error(new Throwable(String.format("Tiled image failed for %s [Width = %s, Height = %s", tileSource, width, height))); return; } int factor = targetTile / width; BufferedImage finalImg = new BufferedImage(width * factor, height * factor, img.getType()); int num = 0; for (int i = 0; i < factor; i++) { for (int j = 0; j < factor; j++) { finalImg.createGraphics().drawImage(img, width * j, height * i, null); num++; } } TextureUtil.uploadTextureImageAllocate(this.getGlTextureId(), finalImg, blur, clamp); } finally { IOUtils.closeQuietly(iresource); } }
From source file:TextureByReference.java
public void setFlipImages(boolean b) { // double check that flipping is necessary if (b != flip) { BufferedImage bImage; // these are the same for all images so get info once int format = images[0].getFormat(); boolean byRef = images[0].isByReference(); boolean yUp = images[0].isYUp(); // flip all the images // have to new ImageComponents because can't set the image at // runtime for (int i = 0; i < images.length; i++) { bImage = images[i].getImage(); ImageOps.flipImage(bImage);/*from w w w . j av a2 s . c o m*/ // if we are byRef and the bImage type does not match // currentType // we need to convert it. If we are not byRef we will // save converting until it is changed to byRef if (byRef && bImage.getType() != currentType) { if (currentType != BufferedImage.TYPE_CUSTOM) { bImage = ImageOps.convertImage(bImage, currentType); } else if (customType == this.TYPE_CUSTOM_RGBA) { bImage = ImageOps.convertToCustomRGBA(bImage); } else { bImage = ImageOps.convertToCustomRGB(bImage); } } images[i] = new ImageComponent2D(format, bImage, byRef, yUp); images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ); images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ); } // set flip to new value flip = b; } }
From source file:com.chiorichan.factory.event.PostImageProcessor.java
@EventHandler() public void onEvent(PostEvalEvent event) { try {/*from w ww. j a va2 s . co m*/ if (event.context().contentType() == null || !event.context().contentType().toLowerCase().startsWith("image")) return; float x = -1; float y = -1; boolean cacheEnabled = AppConfig.get().getBoolean("advanced.processors.imageProcessorCache", true); boolean grayscale = false; ScriptingContext context = event.context(); HttpRequestWrapper request = context.request(); Map<String, String> rewrite = request.getRewriteMap(); if (rewrite != null) { if (rewrite.get("serverSideOptions") != null) { String[] params = rewrite.get("serverSideOptions").trim().split("_"); for (String p : params) if (p.toLowerCase().startsWith("width") && x < 0) x = Integer.parseInt(p.substring(5)); else if ((p.toLowerCase().startsWith("x") || p.toLowerCase().startsWith("w")) && p.length() > 1 && x < 0) x = Integer.parseInt(p.substring(1)); else if (p.toLowerCase().startsWith("height") && y < 0) y = Integer.parseInt(p.substring(6)); else if ((p.toLowerCase().startsWith("y") || p.toLowerCase().startsWith("h")) && p.length() > 1 && y < 0) y = Integer.parseInt(p.substring(1)); else if (p.toLowerCase().equals("thumb")) { x = 150; y = 0; break; } else if (p.toLowerCase().equals("bw") || p.toLowerCase().equals("grayscale")) grayscale = true; } if (request.getArgument("width") != null && request.getArgument("width").length() > 0) x = request.getArgumentInt("width"); if (request.getArgument("height") != null && request.getArgument("height").length() > 0) y = request.getArgumentInt("height"); if (request.getArgument("w") != null && request.getArgument("w").length() > 0) x = request.getArgumentInt("w"); if (request.getArgument("h") != null && request.getArgument("h").length() > 0) y = request.getArgumentInt("h"); if (request.getArgument("thumb") != null) { x = 150; y = 0; } if (request.hasArgument("bw") || request.hasArgument("grayscale")) grayscale = true; } // Tests if our Post Processor can process the current image. List<String> readerFormats = Arrays.asList(ImageIO.getReaderFormatNames()); List<String> writerFormats = Arrays.asList(ImageIO.getWriterFormatNames()); if (context.contentType() != null && !readerFormats.contains(context.contentType().split("/")[1].toLowerCase())) return; int inx = event.context().buffer().readerIndex(); BufferedImage img = ImageIO.read(new ByteBufInputStream(event.context().buffer())); event.context().buffer().readerIndex(inx); if (img == null) return; float w = img.getWidth(); float h = img.getHeight(); float w1 = w; float h1 = h; if (x < 1 && y < 1) { x = w; y = h; } else if (x > 0 && y < 1) { w1 = x; h1 = x * (h / w); } else if (y > 0 && x < 1) { w1 = y * (w / h); h1 = y; } else if (x > 0 && y > 0) { w1 = x; h1 = y; } boolean resize = w1 > 0 && h1 > 0 && w1 != w && h1 != h; boolean argb = request.hasArgument("argb") && request.getArgument("argb").length() == 8; if (!resize && !argb && !grayscale) return; // Produce a unique encapsulated id based on this image processing request String encapId = SecureFunc.md5(context.filename() + w1 + h1 + request.getArgument("argb") + grayscale); File tmp = context.site() == null ? AppConfig.get().getDirectoryCache() : context.site().directoryTemp(); File file = new File(tmp, encapId + "_" + new File(context.filename()).getName()); if (cacheEnabled && file.exists()) { event.context().resetAndWrite(FileUtils.readFileToByteArray(file)); return; } Image image = resize ? img.getScaledInstance(Math.round(w1), Math.round(h1), AppConfig.get().getBoolean("advanced.processors.useFastGraphics", true) ? Image.SCALE_FAST : Image.SCALE_SMOOTH) : img; // TODO Report malformed parameters to user if (argb) { FilteredImageSource filteredSrc = new FilteredImageSource(image.getSource(), new RGBColorFilter((int) Long.parseLong(request.getArgument("argb"), 16))); image = Toolkit.getDefaultToolkit().createImage(filteredSrc); } BufferedImage rtn = new BufferedImage(Math.round(w1), Math.round(h1), img.getType()); Graphics2D graphics = rtn.createGraphics(); graphics.drawImage(image, 0, 0, null); graphics.dispose(); if (grayscale) { ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); op.filter(rtn, rtn); } if (resize) Log.get().info(EnumColor.GRAY + "Resized image from " + Math.round(w) + "px by " + Math.round(h) + "px to " + Math.round(w1) + "px by " + Math.round(h1) + "px"); if (rtn != null) { ByteArrayOutputStream bs = new ByteArrayOutputStream(); if (context.contentType() != null && writerFormats.contains(context.contentType().split("/")[1].toLowerCase())) ImageIO.write(rtn, context.contentType().split("/")[1].toLowerCase(), bs); else ImageIO.write(rtn, "png", bs); if (cacheEnabled && !file.exists()) FileUtils.writeByteArrayToFile(file, bs.toByteArray()); event.context().resetAndWrite(bs.toByteArray()); } } catch (Throwable e) { e.printStackTrace(); } return; }