List of usage examples for javax.imageio ImageReader setInput
public void setInput(Object input, boolean seekForwardOnly)
From source file:nl.b3p.kaartenbalie.service.KBImageTool.java
/** Reads an image from an http input stream. * * @param is Inputstream/*from w ww . ja va 2s .c o m*/ * @param mime String representing the mime type of the image. * * @return BufferedImage * * @throws Exception */ // <editor-fold defaultstate="" desc="readImage(GetMethod method, String mime) method."> public static BufferedImage readImage(InputStream is, String mime, ServiceProviderRequest wmsRequest) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int bytesRead = 0; byte[] buffer = new byte[2048]; while (bytesRead != -1) { bytesRead = is.read(buffer, 0, buffer.length); if (bytesRead > 0) baos.write(buffer, 0, bytesRead); } if (mime.indexOf(";") != -1) { mime = mime.substring(0, mime.indexOf(";")); } String mimeType = getMimeType(mime); if (mimeType == null) { String message = baos.toString(); message = message.replaceAll("(\\r|\\n)", ""); log.error("Response from server not understood (mime = " + mime + "): " + message); throw new Exception("Response from server not understood (mime = " + mime + "): " + message); } ImageReader ir = getReader(mimeType); if (ir == null) { log.error("no reader available for imageformat: " + mimeType.substring(mimeType.lastIndexOf("/") + 1)); throw new Exception( "no reader available for imageformat: " + mimeType.substring(mimeType.lastIndexOf("/") + 1)); } //TODO Make smarter.. Possibly faster... But keep reporting! wmsRequest.setBytesReceived(new Long(baos.size())); ImageInputStream stream = ImageIO.createImageInputStream(new ByteArrayInputStream(baos.toByteArray())); ir.setInput(stream, true); try { //if image is a png, has no alpha and has a tRNS then make that color transparent. BufferedImage i = ir.read(0); if (!i.getColorModel().hasAlpha() && ir.getImageMetadata(0) instanceof PNGMetadata) { PNGMetadata metadata = (PNGMetadata) ir.getImageMetadata(0); if (metadata.tRNS_present) { int alphaPix = (metadata.tRNS_red << 16) | (metadata.tRNS_green << 8) | (metadata.tRNS_blue); BufferedImage tmp = new BufferedImage(i.getWidth(), i.getHeight(), BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < i.getWidth(); x++) { for (int y = 0; y < i.getHeight(); y++) { int rgb = i.getRGB(x, y); rgb = (rgb & 0xFFFFFF) == alphaPix ? alphaPix : rgb; tmp.setRGB(x, y, rgb); } } i = tmp; } } return i; } finally { ir.dispose(); } }
From source file:com.funambol.foundation.util.MediaUtils.java
/** * Creates the thumbnail./* w ww . j a v a2 s . com*/ * * @param imageFile the image file * @param thumbFile the empty thumbnail file * @param thumbX the width of the thumbnail * @param thumbY the height of the thumbnail * @param imageName the image file name with extension * @param tolerance the percentage of tolerance before creating a thumbnail * @return true is the thumbnail has been created, false otherwise * @throws IOException if an error occurs */ private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName, double tolerance) throws IOException { FileInputStream fileis = null; ImageInputStream imageis = null; Iterator readers = null; try { readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1)); if (readers == null || (!readers.hasNext())) { throw new IOException("File not supported"); } ImageReader reader = (ImageReader) readers.next(); fileis = new FileInputStream(imageFile); imageis = ImageIO.createImageInputStream(fileis); reader.setInput(imageis, true); // Determines thumbnail height, width and quality int thumbWidth = thumbX; int thumbHeight = thumbY; double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = reader.getWidth(0); int imageHeight = reader.getHeight(0); // // Don't create the thumbnail if the original file is smaller // than required size increased by % tolerance // if (imageWidth <= (thumbWidth * (1 + tolerance / 100)) && imageHeight <= (thumbHeight * (1 + tolerance / 100))) { return false; } double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(3, 3, 0, 0); BufferedImage bi = reader.read(0, param); Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH); BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null); FileOutputStream fileOutputStream = new FileOutputStream(thumbFile); ImageIO.write(thumbImage, "jpg", fileOutputStream); thumb.flush(); thumbImage.flush(); fileOutputStream.flush(); fileOutputStream.close(); graphics2D.dispose(); } finally { if (fileis != null) { fileis.close(); } if (imageis != null) { imageis.close(); } } return true; }
From source file:Main.java
public Main(String filename) throws Exception { FileInputStream fin = new FileInputStream(filename); String suffix = filename.substring(filename.lastIndexOf('.') + 1); System.out.println("suf " + suffix); Iterator readers = ImageIO.getImageReadersBySuffix(suffix); ImageReader imageReader = (ImageReader) readers.next(); ImageInputStream iis = ImageIO.createImageInputStream(fin); imageReader.setInput(iis, false); int num = imageReader.getNumImages(true); images = new BufferedImage[num]; for (int i = 0; i < num; ++i) { images[i] = imageReader.read(i); }//from www . ja v a 2s . c om fin.close(); }
From source file:Main.java
public Main() throws Exception { String filename = "a.png"; FileInputStream inputStream = new FileInputStream(filename); String extensionName = filename.substring(filename.lastIndexOf('.') + 1); Iterator readers = ImageIO.getImageReadersBySuffix(extensionName); ImageReader imageReader = (ImageReader) readers.next(); ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream); imageReader.setInput(imageInputStream, false); int num = imageReader.getNumImages(true); images = new BufferedImage[num]; for (int i = 0; i < num; ++i) { images[i] = imageReader.read(i); }/*www. ja va 2s . c o m*/ inputStream.close(); }
From source file:com.google.testing.web.screenshotter.Screenshot.java
/** * Returns a BufferedImage of the screenshot. Every call to this returns a new copy of the image. *//*from w w w .j a v a2s . com*/ public BufferedImage asImage() throws IOException { ImageReader imageReader = ImageIO.getImageReadersByFormatName(FORMAT).next(); imageReader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(asBytes())), true); return imageReader.read(0); }
From source file:edu.ku.brc.specify.datamodel.busrules.AttachmentBusRules.java
protected void addImageAttributeIfNecessary() { if (browser != null) { Integer width = null;//from w w w .j a v a 2 s .com Integer height = null; File file = new File(browser.getValue().toString()); String mimeType = file == null ? null : AttachmentUtils.getMimeType(file.getName()); boolean isImage = mimeType != null && mimeType.startsWith("image"); if (isImage) { try { ImageInputStream iis = ImageIO.createImageInputStream(file); Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); if (readers.hasNext()) { // pick the first available ImageReader ImageReader reader = readers.next(); // attach source to the reader reader.setInput(iis, true); // read metadata of first image // IIOMetadata metadata = reader.getImageMetadata(0); width = reader.getWidth(0); height = reader.getHeight(0); } } catch (IOException ex) { //XXX does this execption necessarily mean the file is bad? //XXX throw or log this exception ex.printStackTrace(); } } //MultiView mvobj = formViewObj.getKids().get(0); FormViewObj aiafv = imageAttributeMultiView == null ? null : imageAttributeMultiView.getCurrentViewAsFormViewObj(); if (aiafv != null) { //hide add/delete buttons. aiafv.getNewRecBtn().setVisible(false); aiafv.getDelRecBtn().setVisible(false); if (isImage) { if (aiafv.getDataObj() == null) { aiafv.getNewRecBtn().doClick(); } System.out.println(browser.getValue() + "height " + height + " width " + width); try { aiafv.setDataIntoUIComp("height", height); aiafv.setControlChanged("height"); aiafv.setDataIntoUIComp("width", width); aiafv.setControlChanged("width"); } catch (Exception e) { log.error("Unable set image attribute data. Controls may be missing from form definition"); } } else { if (aiafv.getDataObj() != null) { //delete the imageAttribute rec //XXX suppress "confirm delete" dlg? aiafv.getDelRecBtn().doClick(); } } } setupImageAttributeView(); } }
From source file:org.exoplatform.wcm.notification.plugin.FileActivityChildPlugin.java
private int getImageWidth(Node node) { int imageWidth = 0; try {/*from ww w . j av a 2 s. c o m*/ if (node.hasNode(NodetypeConstant.JCR_CONTENT)) node = node.getNode(NodetypeConstant.JCR_CONTENT); ImageReader reader = ImageIO.getImageReadersByMIMEType(mimeType).next(); ImageInputStream iis = ImageIO.createImageInputStream(node.getProperty("jcr:data").getStream()); reader.setInput(iis, true); imageWidth = reader.getWidth(0); iis.close(); reader.dispose(); } catch (RepositoryException | IOException e) { if (LOG.isWarnEnabled()) { LOG.warn("Can not get image width in " + this.getClass().getName()); } } return imageWidth; }
From source file:org.exoplatform.wcm.notification.plugin.FileActivityChildPlugin.java
private int getImageHeight(Node node) { int imageHeight = 0; try {/*from w ww .j a v a 2 s. com*/ if (node.hasNode(NodetypeConstant.JCR_CONTENT)) node = node.getNode(NodetypeConstant.JCR_CONTENT); ImageReader reader = ImageIO.getImageReadersByMIMEType(mimeType).next(); ImageInputStream iis = ImageIO.createImageInputStream(node.getProperty("jcr:data").getStream()); reader.setInput(iis, true); imageHeight = reader.getHeight(0); iis.close(); reader.dispose(); } catch (RepositoryException | IOException e) { if (LOG.isWarnEnabled()) { LOG.warn("Can not get image height in " + this.getClass().getName()); } } return imageHeight; }
From source file:org.photovault.imginfo.ImageFile.java
/** * Opens the image file specified by fname & dirname properties, read * the rest of fields from that and create the instances * @param f File from which to read the information * @throws org.photovault.common.PhotovaultException If the file is not of recognized type * @throws IOException if the image cannot be read. *///from w w w . java2 s .c o m protected void readImageFile(File f) throws PhotovaultException, IOException { String fname = f.getName(); int lastDotPos = fname.lastIndexOf("."); if (lastDotPos <= 0 || lastDotPos >= fname.length() - 1) { throw new PhotovaultException("Cannot determine file type extension of " + f.getAbsolutePath()); } String suffix = fname.substring(lastDotPos + 1); Iterator readers = ImageIO.getImageReadersBySuffix(suffix); if (readers.hasNext()) { ImageReader reader = (ImageReader) readers.next(); ImageInputStream iis = null; try { iis = ImageIO.createImageInputStream(f); if (iis != null) { reader.setInput(iis, true); // int imageCount = reader.getNumImages( true ); // for now... int imageCount = 1; for (int n = 0; n < imageCount; n++) { ImageDescriptorBase img = new OriginalImageDescriptor(this, "image#" + n); img.setWidth(reader.getWidth(n)); img.setHeight(reader.getHeight(n)); // img.setInstanceType( ImageInstance.INSTANCE_TYPE_ORIGINAL ); img.setFile(this); } reader.dispose(); } } catch (IOException ex) { log.debug("Exception in readImageFile: " + ex.getMessage()); throw ex; } finally { if (iis != null) { try { iis.close(); } catch (IOException ex) { log.warn("Cannot close image stream: " + ex.getMessage()); } } } } else { RawImage ri = new RawImage(f); if (ri.isValidRawFile()) { // PlanarImage img = ri.getCorrectedImage(); ImageDescriptorBase img = new OriginalImageDescriptor(this, "image#0"); img.setWidth(ri.getWidth()); img.setHeight(ri.getHeight()); } else { throw new PhotovaultException( "Unknown image file extension " + suffix + "\nwhile reading " + f.getAbsolutePath()); } } }
From source file:jails.http.converter.BufferedImageHttpMessageConverter.java
public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { ImageInputStream imageInputStream = null; ImageReader imageReader = null; try {// w w w. j a v a 2 s . c o m imageInputStream = createImageInputStream(inputMessage.getBody()); MediaType contentType = inputMessage.getHeaders().getContentType(); Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString()); if (imageReaders.hasNext()) { imageReader = imageReaders.next(); ImageReadParam irp = imageReader.getDefaultReadParam(); process(irp); imageReader.setInput(imageInputStream, true); return imageReader.read(0, irp); } else { throw new HttpMessageNotReadableException( "Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]"); } } finally { if (imageReader != null) { imageReader.dispose(); } if (imageInputStream != null) { try { imageInputStream.close(); } catch (IOException ex) { // ignore } } } }