List of usage examples for org.apache.commons.imaging Imaging getBufferedImage
public static BufferedImage getBufferedImage(final File file) throws ImageReadException, IOException
From source file:lucee.runtime.img.coder.SanselanCoder.java
/** * translate a file resource to a buffered image * @param res/* w w w. j a va 2 s . co m*/ * @return * @throws IOException */ @Override public final BufferedImage toBufferedImage(Resource res, String format) throws IOException { InputStream is = null; try { return Imaging.getBufferedImage(is = res.getInputStream()); } catch (ImageReadException e) { throw ExceptionUtil.toIOException(e); } finally { IOUtil.closeEL(is); } }
From source file:lucee.runtime.img.coder.SanselanCoder.java
/** * translate a binary array to a buffered image * @param binary// w w w .ja v a 2s. c om * @return * @throws IOException */ @Override public final BufferedImage toBufferedImage(byte[] bytes, String format) throws IOException { try { return Imaging.getBufferedImage(new ByteArrayInputStream(bytes)); } catch (ImageReadException e) { throw ExceptionUtil.toIOException(e); } }
From source file:com.hygenics.imaging.ImageCleanup.java
/** * Set the image type to jpg/* w ww . j a v a 2 s . com*/ */ public void setImageType(String inurl, byte[] ibytes) { String imgType = null; String urltst = inurl; // get the image type from the url if (inurl != null) { urltst = inurl.toLowerCase(); } // get the image type from the url which should contain the MIME type if (!urltst.toLowerCase().contains("jpg") | !urltst.toLowerCase().contains("jpeg")) { ByteArrayInputStream bis = new ByteArrayInputStream(ibytes); if (bis != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); // convert to jpeg for compression try { // use apache to read to a buffered image with certain // metadata and then convert to a jpg BufferedImage image = Imaging.getBufferedImage(bis); ImageIO.write(image, "jpg", bos); ibytes = bos.toByteArray(); } catch (ImageReadException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } imgType = "jpg"; }
From source file:adams.data.io.input.ApacheCommonsImageReader.java
/** * Performs the actual reading of the image file. * // w ww.j a v a2 s . c o m * @param file the file to read * @return the image container, null if failed to read */ @Override protected BufferedImageContainer doRead(PlaceholderFile file) { BufferedImageContainer result; BufferedImage image; result = null; try { image = Imaging.getBufferedImage(file.getAbsoluteFile()); } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to load: " + file, e); image = null; } if (image != null) { result = new BufferedImageContainer(); result.setImage(image); } return result; }
From source file:com.hygenics.imaging.ImageCleanup.java
public byte[] writeMetaData(String data, byte[] ibytes) { // write metadata based on the metadata columns list TiffOutputSet outset = null;//w w w . ja v a2 s . c o m BufferedImage bi; ImageMetadata metadata; ByteArrayOutputStream bos = null; try { // get the buffered image to write to bi = Imaging.getBufferedImage(ibytes); metadata = Imaging.getMetadata(ibytes); JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { // get the image exif data TiffImageMetadata exif = jpegMetadata.getExif(); outset = exif.getOutputSet(); } if (outset == null) { // get a new set (directory structured to write to) outset = new TiffOutputSet(); } TiffOutputDirectory exdir = outset.getOrCreateExifDirectory(); exdir.removeField(ExifTagConstants.EXIF_TAG_USER_COMMENT); exdir.add(ExifTagConstants.EXIF_TAG_USER_COMMENT, data.trim()); bos = new ByteArrayOutputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(ibytes); ExifRewriter exrw = new ExifRewriter(); // read to a byte stream exrw.updateExifMetadataLossy(bis, bos, outset); // read the input from the byte buffer ibytes = bos.toByteArray(); bis.close(); bos.close(); } catch (ImageReadException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ImageWriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (bos != null) { try { bos.flush(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return ibytes; }
From source file:com.vividsolutions.jump.workbench.imagery.graphic.CommonsImage.java
protected void initImage() throws ReferencedImageException { BufferedImage image = getImage(); if (image != null) return;/*from w ww . j a v a2 s. c om*/ String uri = getUri(); try { is = CompressedFile.openFile(uri); ImageInfo info = Imaging.getImageInfo(is, UriUtil.getFileName(CompressedFile.getTargetFileWithPath(new URI(uri)))); type = info.getFormatName(); close(is); is = CompressedFile.openFile(uri); image = Imaging.getBufferedImage(is); close(is); setImage(image); } catch (Exception e) { throw new ReferencedImageException(e); } finally { close(is); } }
From source file:com.github.pitzcarraldo.openjpeg.OpenJPEGLoader.java
/** * Method to load a JP2 file using OpenJPEG executable * @param pFile jp2 file to load//from w w w .j a va 2 s .com * @return decoded buffered image from file */ private static BufferedImage loadJP2_Executable(File pFile) { logger.trace("executable decoder: " + pFile.getAbsolutePath()); File tempOutput = null; try { tempOutput = File.createTempFile("dissimilar_" + pFile.getName() + "_", ".tif"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //FIXME: null check? tempOutput.deleteOnExit(); List<String> commandLine = new LinkedList<String>(); commandLine.add(OPENJPEGEXE.getAbsolutePath()); commandLine.add("-i"); commandLine.add(pFile.getAbsolutePath()); commandLine.add("-o"); commandLine.add(tempOutput.getAbsolutePath()); logger.trace("running: " + commandLine.toString()); ToolRunner runner = new ToolRunner(true); int exitCode = 0; try { exitCode = runner.runCommand(commandLine); logger.trace("exit code: " + exitCode); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (exitCode != 0) { //some error BufferedReader log = runner.getStdout(); try { while (log.ready()) { logger.error("log: " + log.readLine()); } } catch (IOException e) { } } else { try { BufferedImage image = Imaging.getBufferedImage(tempOutput); //force a delete tempOutput.delete(); return image; } catch (ImageReadException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }
From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java
/** * Create the front cover/*ww w . ja v a 2s .c o m*/ * * @param coverId * @param logoBytes * @param authorStr * @param titleStr * @param textColor * - ex. #FFFFFF * @param width * @param height * @param type * - ex. BufferedImage.TYPE_INT_ARGB * @param imgFormat * - ex. ImageFormat.IMAGE_FORMAT_PNG * @return * @throws Exception */ public byte[] createFrontCover(String coverId, byte[] logoBytes, String authorStr, String titleStr, String spineColor, String authorTextColor, String titleTextColor, int width, int height, int type, ImageFormat imgFormat, int maxColors) throws Exception { Graphics2D g = null; try { // Front cover first // Read in base cover image BufferedImage coverImg = Imaging.getBufferedImage(coverMap.get(coverId)); // Resize cover image to the basic 300 X 400 for front cover BufferedImage frontCoverImg = resize(coverImg, 300, 400, type); g = (Graphics2D) frontCoverImg.getGraphics(); // Draw logo if present if (logoBytes != null && logoBytes.length > 0) { // Resize logo to 200x100 BufferedImage logoImg = Imaging.getBufferedImage(logoBytes); BufferedImage outLogo = null; int logoHeight = logoImg.getHeight(); int logoWidth = logoImg.getWidth(); if (logoHeight > 100 || logoWidth > 200) { outLogo = this.resize(logoImg, logoWidth > 200 ? 200 : logoWidth, logoHeight > 100 ? 100 : logoHeight, type); } else { outLogo = logoImg; } // Add to coverImg g.drawImage(outLogo, 32, 25, null); } // Add spine if present if (spineColor != null && spineColor.length() > 0) { g.setColor(Color.decode(spineColor)); g.fillRect(0, 0, 2, frontCoverImg.getHeight()); } // Add author if present if (authorStr != null && authorStr.length() > 0) { // Add author text to image BufferedImage authorTextImg = createText(40, 220, authorStr, authorTextColor, false, authorFontMap, type); g.drawImage(authorTextImg, 30, 215, null); } // Add title if present if (titleStr != null && titleStr.length() > 0) { BufferedImage titleTextImg = createText(100, 220, titleStr, titleTextColor, false, titleFontMap, type); g.drawImage(titleTextImg, 30, 240, null); } // If the requested size is not 300X400 convert the image BufferedImage outImg = null; if (width != 300 || height != 400) { outImg = resize(frontCoverImg, width, height, type); } else { outImg = frontCoverImg; } // Do we want a PNG with a fixed number of colors if (maxColors >= 2 && imgFormat == ImageFormat.IMAGE_FORMAT_PNG) { outImg = ImageUtils.reduce32(outImg, maxColors); } // Return bytes Map<String, Object> params = new HashMap<String, Object>(); byte[] outBytes = Imaging.writeImageToBytes(outImg, imgFormat, params); return outBytes; } finally { if (g != null) { g.dispose(); } } }
From source file:com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpRenderListener.java
private byte[] processImage(byte[] imageBytes, List<Rectangle> areasToBeCleaned) { if (areasToBeCleaned.isEmpty()) { return imageBytes; }/*from ww w . j a va 2s. c om*/ try { BufferedImage image = Imaging.getBufferedImage(imageBytes); ImageInfo imageInfo = Imaging.getImageInfo(imageBytes); cleanImage(image, areasToBeCleaned); // Apache can only read JPEG, so we should use awt for writing in this format if (imageInfo.getFormat() == ImageFormats.JPEG) { return getJPGBytes(image); } else { Map<String, Object> params = new HashMap<String, Object>(); if (imageInfo.getFormat() == ImageFormats.TIFF) { params.put(ImagingConstants.PARAM_KEY_COMPRESSION, TiffConstants.TIFF_COMPRESSION_LZW); } return Imaging.writeImageToBytes(image, imageInfo.getFormat(), params); } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java
/** * Calculate the PSNR between two files/*from w ww . jav a 2 s . c om*/ * @param pOne first image to compare * @param pTwo second image to compare * @return calculated psnr */ public static double calcPSNR(final File pOne, final File pTwo) { BufferedImage imageOne = null; try { imageOne = Imaging.getBufferedImage(pOne); } catch (IOException e) { printError(pOne, false, false, pTwo, false); return -1; } catch (NullPointerException e) { printError(pOne, false, false, pTwo, false); return -1; } catch (ImageReadException e) { printError(pOne, false, false, pTwo, false); return -1; } //getRGB only returns 8 bits per component, so what about 16-bit images? final int[] oneA = imageOne.getRGB(0, 0, imageOne.getWidth(), imageOne.getHeight(), null, 0, imageOne.getWidth()); final boolean greyscale = (imageOne.getType() == BufferedImage.TYPE_BYTE_GRAY || imageOne.getType() == BufferedImage.TYPE_USHORT_GRAY); imageOne = null; BufferedImage imageTwo = null; try { imageTwo = Imaging.getBufferedImage(pTwo); } catch (IOException e) { printError(pOne, true, true, pTwo, false); return -1; } catch (NullPointerException e) { printError(pOne, true, true, pTwo, false); return -1; } catch (ImageReadException e) { printError(pOne, true, true, pTwo, false); return -1; } //getRGB only returns 8 bits per component, so what about 16-bit images? final int[] twoA = imageTwo.getRGB(0, 0, imageTwo.getWidth(), imageTwo.getHeight(), null, 0, imageTwo.getWidth()); imageTwo = null; final double psnr = calcPSNR(oneA, twoA, greyscale); return psnr; }