List of usage examples for javax.imageio ImageWriter setOutput
public void setOutput(Object output)
From source file:tvbrowserdataservice.file.ProgramField.java
/** * This Function loads an image using imageio, * resizes it to the Max-Size of Images in TVBrowser and * stores it as an compressed jpg.//from ww w. j a va 2 s . com * <p/> * If the Image is smaller than the Max-Size, it isn't altered * * @param data Image-Data * @return resized Image-Data */ private static byte[] recreateImage(byte[] data) { byte[] newdata = null; BufferedImage image = null; try { // Read Image image = ImageIO.read(new ByteArrayInputStream(data)); int curx = image.getWidth(null); int cury = image.getHeight(null); // If the Size is < than max, use the original Data to reduce compression // artefacts if ((curx <= MAX_IMAGE_SIZE_X) && (cury <= MAX_IMAGE_SIZE_Y)) { return data; } int newx = MAX_IMAGE_SIZE_X; int newy = (int) ((MAX_IMAGE_SIZE_X / (float) curx) * cury); if (newy > MAX_IMAGE_SIZE_Y) { newy = MAX_IMAGE_SIZE_Y; newx = (int) ((MAX_IMAGE_SIZE_Y / (float) cury) * curx); } BufferedImage tempPic = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2d = tempPic.createGraphics(); g2d.drawImage(image, null, null); g2d.dispose(); BufferedImage newImage = UiUtilities.scaleIconToBufferedImage(tempPic, newx, newy); ByteArrayOutputStream out = new ByteArrayOutputStream(); // Find a jpeg writer ImageWriter writer = null; Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg"); if (iter.hasNext()) { writer = iter.next(); } if (writer != null) { // Prepare output file ImageOutputStream ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); JPEGImageWriteParam param = new JPEGImageWriteParam(null); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.85f); // Write the image writer.write(null, new IIOImage(newImage, null, null), param); // Cleanup ios.flush(); writer.dispose(); ios.close(); newdata = out.toByteArray(); } else { mLog.severe("No JPEG-Exporter found. Image is not stored in Data"); } } catch (IOException e) { e.printStackTrace(); newdata = null; } return newdata; }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
/** * ???????/*from www . ja va 2 s. co m*/ * * @param org_id * @param folderName * @param uid * @param fileBean * @param acceptExts * @param msgList * @return */ public static byte[] getBytesShrink(InputStream is, int width, int height, List<String> msgList) { byte[] result = null; try { BufferedImage orgImage = ImageIO.read(is); BufferedImage shrinkImage = FileuploadUtils.shrinkImage(orgImage, width, height); Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("png"); ImageWriter writer = writers.next(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); writer.write(shrinkImage); result = out.toByteArray(); } catch (Exception e) { logger.error("fileupload", e); result = null; } finally { try { if (is != null) { is.close(); } } catch (Exception e) { logger.error("fileupload", e); result = null; } } return result; }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
/** * ???????//from ww w .j ava2s.co m * * @param org_id * @param folderName * @param uid * @param fileBean * @param acceptExts * @param msgList * @return */ public static ShrinkImageSet getBytesShrinkFilebean(String org_id, String folderName, int uid, FileuploadLiteBean fileBean, String[] acceptExts, int width, int height, List<String> msgList, boolean isFixOrgImage) { byte[] result = null; byte[] fixResult = null; InputStream is = null; boolean fixed = false; try { String file_name = fileBean.getFileName(); String ext = ""; if (acceptExts != null && acceptExts.length > 0) { // ??? // ???? boolean isAccept = false; String tmpExt = null; int len = acceptExts.length; for (int i = 0; i < len; i++) { if (!acceptExts[i].startsWith(".")) { tmpExt = "." + acceptExts[i]; } if (file_name.toLowerCase().endsWith(tmpExt)) { isAccept = true; ext = tmpExt.replace(".", ""); ; break; } } if (!isAccept) { // ???????null ? return null; } } is = ALStorageService.getFile(FOLDER_TMP_FOR_ATTACHMENT_FILES, uid + ALStorageService.separator() + folderName, String.valueOf(fileBean.getFileId())); byte[] imageInBytes = IOUtils.toByteArray(is); ImageInformation readImageInformation = readImageInformation(new ByteArrayInputStream(imageInBytes)); BufferedImage bufferdImage = ImageIO.read(new ByteArrayInputStream(imageInBytes)); if (readImageInformation != null) { bufferdImage = transformImage(bufferdImage, getExifTransformation(readImageInformation), readImageInformation.orientation >= 5 ? bufferdImage.getHeight() : bufferdImage.getWidth(), readImageInformation.orientation >= 5 ? bufferdImage.getWidth() : bufferdImage.getHeight()); fixed = isFixOrgImage; } if (bufferdImage == null) { // ?bufferdImage???????????,null?. return null; } BufferedImage shrinkImage = FileuploadUtils.shrinkAndTrimImage(bufferdImage, width, height); Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("jpeg"); ImageWriter writer = writers.next(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); writer.write(shrinkImage); result = out.toByteArray(); if (fixed) { Iterator<ImageWriter> writers2 = ImageIO.getImageWritersBySuffix(ext); ImageWriter writer2 = writers2.next(); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); ImageOutputStream ios2 = ImageIO.createImageOutputStream(out2); writer2.setOutput(ios2); writer2.write(bufferdImage); fixResult = out2.toByteArray(); } } catch (Exception e) { logger.error("fileupload", e); result = null; } finally { try { if (is != null) { is.close(); } } catch (Exception e) { logger.error("fileupload", e); result = null; } } return new ShrinkImageSet(result, fixed ? fixResult : null); }
From source file:net.intelliant.util.UtilCommon.java
private static String generateCompressedImageForInputFile(String locationInFileSystem, String srcFileName) throws NoSuchAlgorithmException, IOException { String outputFileName = srcFileName; File srcFile = new File(locationInFileSystem, srcFileName); File compressedFile = null;/*www . ja v a2 s. c o m*/ FileImageOutputStream outputStream = null; try { if (srcFile.exists()) { String md5sum = getMD5SumForFile(srcFile.getAbsolutePath()); int extentionIndex = srcFileName.lastIndexOf("."); // find index of extension. if (extentionIndex != -1) { outputFileName = outputFileName.replaceFirst("\\.", "_" + md5sum + "."); } compressedFile = new File(locationInFileSystem, outputFileName); if (Debug.infoOn()) { Debug.logInfo("[generateCompressedImageFor] sourceFile >> " + srcFile.getAbsolutePath(), module); Debug.logInfo("[generateCompressedImageFor] md5sum >> " + md5sum, module); Debug.logInfo( "[generateCompressedImageFor] compressedFile >> " + compressedFile.getAbsolutePath(), module); } if (!compressedFile.exists()) { if (Debug.infoOn()) { Debug.logInfo("[generateCompressedImageFor] compressed file does NOT exist..", module); } Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter imageWriter = (ImageWriter) iter.next(); ImageWriteParam iwp = imageWriter.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(Float.parseFloat(imageCompressionQuality)); // an integer between 0 and 1, 1 specifies minimum compression and maximum quality BufferedImage bufferedImage = ImageIO.read(srcFile); outputStream = new FileImageOutputStream(compressedFile); imageWriter.setOutput(outputStream); IIOImage image = new IIOImage(bufferedImage, null, null); imageWriter.write(null, image, iwp); } else { if (Debug.infoOn()) { Debug.logInfo( "[generateCompressedImageFor] compressed file exists, not compressing again..", module); } } } else { Debug.logWarning(String.format("Source image file does NOT exist..", srcFile), module); } } finally { if (outputStream != null) { outputStream.close(); outputStream = null; } } return outputFileName; }
From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java
/** * Can change this to choose a better compression level as the default * @param image/*from w w w .j av a2 s . c om*/ * @param scaledImage * @return */ public static boolean writeTo(BufferedImage image, File scaledImage, Size scaledSize, String outputFormat) { try { if (!StringHelper.containsNonWhitespace(outputFormat)) { outputFormat = OUTPUT_FORMAT; } Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat); if (writers.hasNext()) { ImageWriter writer = writers.next(); ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize); IIOImage iiOImage = new IIOImage(image, null, null); ImageOutputStream iOut = new FileImageOutputStream(scaledImage); writer.setOutput(iOut); writer.write(null, iiOImage, iwp); writer.dispose(); iOut.flush(); iOut.close(); return true; } else { return ImageIO.write(image, outputFormat, scaledImage); } } catch (IOException e) { return false; } }
From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java
/** * Can change this to choose a better compression level as the default * @param image//from w ww . j av a 2s . c o m * @param scaledImage * @return */ private static boolean writeTo(BufferedImage image, OutputStream scaledImage, Size scaledSize, String outputFormat) { try { if (!StringHelper.containsNonWhitespace(outputFormat)) { outputFormat = OUTPUT_FORMAT; } Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat); if (writers.hasNext()) { ImageWriter writer = writers.next(); ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize); IIOImage iiOImage = new IIOImage(image, null, null); ImageOutputStream iOut = new MemoryCacheImageOutputStream(scaledImage); writer.setOutput(iOut); writer.write(null, iiOImage, iwp); writer.dispose(); iOut.flush(); iOut.close(); return true; } else { return ImageIO.write(image, outputFormat, scaledImage); } } catch (IOException e) { return false; } }
From source file:com.oneis.utils.ImageColouring.java
/** * Transform the colours in an image./*from w ww . ja v a2s . c o m*/ * * @param Filename Full pathname to the source file * @param Method Which colouring method to use, use METHOD_* constants * @param Colours Array of 0xRRGGBB colours for recolouring * @param JPEGQuality If the image is a JPEG, the output quality for * reencoding * @param Output An OutputStream to write the file. Will be closed. */ static public void colourImage(String Filename, int Method, int[] Colours, int JPEGQuality, OutputStream Output) throws IOException { ColouringInfo colouringInfo = prepareForColouring(Method, Colours); String extension = Filename.substring(Filename.lastIndexOf('.') + 1, Filename.length()); String outputFormat = extension; if (outputFormat.equals("gif")) { if (!colourImageGIF(Filename, colouringInfo, Output)) { throw new RuntimeException("Failed to directly colour GIF file"); } return; } BufferedImage image = ImageIO.read(new File(Filename)); int width = image.getWidth(); int height = image.getHeight(); // Rewrite the pixels int[] pixelBuffer = new int[width]; for (int y = 0; y < height; ++y) { image.getRGB(0, y, width, 1, pixelBuffer, 0, width); doColouring(pixelBuffer, colouringInfo); image.setRGB(0, y, width, 1, pixelBuffer, 0, width); } // Writing is done the hard way to enable the JPEG quality to be set. // Get a writer Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(outputFormat); if (!iter.hasNext()) { throw new RuntimeException("Couldn't write image of type " + outputFormat); } ImageWriter writer = iter.next(); ImageWriteParam iwparam = null; // Is it JPEG? If so, might want to set the quality if (outputFormat.equals("jpeg") || outputFormat.equals("jpg")) { // Clamp value int q = JPEGQuality; if (q < 10) { q = 10; } if (q > 100) { q = 100; } JPEGImageWriteParam jparam = new JPEGImageWriteParam(null); jparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); jparam.setCompressionQuality((float) (((float) q) / 100.0)); iwparam = jparam; } ImageOutputStream output = ImageIO.createImageOutputStream(Output); writer.setOutput(output); writer.write(null, new IIOImage(image, null, null), iwparam); output.close(); }
From source file:com.t3.net.LocalLocation.java
@Override public void putContent(ImageWriter writer, BufferedImage image) { try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(getFile()))) { writer.setOutput(out); writer.write(image);// ww w.j ava 2s .c o m } catch (IOException e) { e.printStackTrace(); } }
From source file:com.t3.net.FTPLocation.java
@Override public void putContent(ImageWriter writer, BufferedImage image) { try (OutputStream os = new URL(composeFileLocation()).openConnection().getOutputStream()) { writer.setOutput(os); writer.write(image);//w w w . java 2 s . c o m } catch (IOException e) { e.printStackTrace(); } }
From source file:de.jwic.ecolib.controls.chart.ChartControl.java
public void renderImage() throws IOException { // create image to draw into BufferedImage bi = new BufferedImage(width < 10 ? 10 : width, height < 10 ? 10 : height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bi.createGraphics(); if (chart != null) { chart.setBackgroundPaint(Color.WHITE); chart.draw(g2d, new Rectangle2D.Double(0, 0, width < 10 ? 10 : width, height < 10 ? 10 : height)); } else {//from w w w .ja v a 2 s . c om g2d.setColor(Color.BLACK); g2d.drawString("No chart has been assigned.", 1, 20); } // finish drawing g2d.dispose(); // write image data into output stream ByteArrayOutputStream out = getImageOutputStream(); out.reset(); // create a PNG image ImageWriter imageWriter = new PNGImageWriter(null); ImageWriteParam param = imageWriter.getDefaultWriteParam(); imageWriter.setOutput(new MemoryCacheImageOutputStream(out)); imageWriter.write(null, new IIOImage(bi, null, null), param); imageWriter.dispose(); setMimeType(MIME_TYPE_PNG); }