|
From: <mol...@us...> - 2009-10-08 16:40:18
|
Revision: 1478
http://openutils.svn.sourceforge.net/openutils/?rev=1478&view=rev
Author: molaschi
Date: 2009-10-08 16:40:09 +0000 (Thu, 08 Oct 2009)
Log Message:
-----------
MEDIA-27 add image quality support
Modified Paths:
--------------
trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java
Modified: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java
===================================================================
--- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2009-10-08 15:39:13 UTC (rev 1477)
+++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2009-10-08 16:40:09 UTC (rev 1478)
@@ -56,9 +56,10 @@
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
+import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
-import javax.imageio.stream.MemoryCacheImageOutputStream;
+import javax.imageio.stream.ImageOutputStream;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
@@ -329,13 +330,15 @@
* @param image image to save
* @param saveTo node to save to
* @param extension extension
+ * @param quality image quality
+ * @param forceProgressive true to force progressive mode
* @throws RepositoryException exception in jcr operations
* @throws IOException exception converting image to jpg
*/
- public static void saveResolution(BufferedImage image, Content saveTo, String extension)
- throws RepositoryException, IOException
+ public static void saveResolution(BufferedImage image, Content saveTo, String extension, float quality,
+ boolean forceProgressive) throws RepositoryException, IOException
{
- saveResolution(image, saveTo, null, extension);
+ saveResolution(image, saveTo, null, extension, quality, forceProgressive);
}
/**
@@ -344,11 +347,13 @@
* @param saveTo node to save to
* @param name name for this resolution
* @param extension extension
+ * @param quality image quality
+ * @param forceProgressive true to force progressive mode
* @throws RepositoryException exception in jcr operations
* @throws IOException exception converting image to jpg
*/
- public static void saveResolution(BufferedImage image, Content saveTo, String name, String extension)
- throws RepositoryException, IOException
+ public static void saveResolution(BufferedImage image, Content saveTo, String name, String extension,
+ float quality, boolean forceProgressive) throws RepositoryException, IOException
{
Content resolutions = saveTo.getChildByName("resolutions");
if (resolutions == null)
@@ -375,7 +380,7 @@
}
NodeData nd = resolutions.createNodeData(resolution, PropertyType.BINARY);
- nd.setValue(getStream(image, extension));
+ nd.setValue(getStream(image, extension, quality, forceProgressive));
String mimetype = "image/" + extension;
if ("jpg".equals(extension))
{
@@ -414,37 +419,73 @@
* @return inputstream
* @throws IOException
*/
- public static InputStream getStream(BufferedImage image, String extension) throws IOException
+ public static InputStream getStream(BufferedImage image, String extension, float quality, boolean forceProgressive)
+ throws IOException
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BufferedOutputStream out = new BufferedOutputStream(bos);
- if ("jpg".equals(extension))
+ try
{
- Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
- ImageWriter writer = iter.next();
- ImageWriteParam iwp = writer.getDefaultWriteParam();
- iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
- iwp.setCompressionQuality(1.0f); // an integer between 0 and 1
- // 1 specifies minimum compression and maximum quality
- MemoryCacheImageOutputStream output = new MemoryCacheImageOutputStream(out);
- writer.setOutput(output);
- IIOImage iioimage = new IIOImage(image, null, null);
- writer.write(null, iioimage, iwp);
- }
- else
- {
- try
+ Iterator writers;
+ ImageOutputStream imageOutputStream;
+ ImageWriteParam params;
+ ImageWriter imageWriter;
+
+ writers = ImageIO.getImageWritersBySuffix(extension);
+
+ if (writers != null && writers.hasNext())
{
- ImageIO.write(image, extension, out);
+ // Fetch the first writer in the list
+ imageWriter = (ImageWriter) writers.next();
+
+ // Specify the parameters according to those the output file will be written
+
+ // Get Default parameters
+ params = imageWriter.getDefaultWriteParam();
+
+ // Define compression mode
+ params.setCompressionMode(javax.imageio.ImageWriteParam.MODE_EXPLICIT);
+
+ // Define compression quality
+ params.setCompressionQuality(quality);
+
+ // Define progressive mode
+ if (forceProgressive)
+ {
+ params.setProgressiveMode(javax.imageio.ImageWriteParam.MODE_DEFAULT);
+ }
+ else
+ {
+ params.setProgressiveMode(javax.imageio.ImageWriteParam.MODE_COPY_FROM_METADATA);
+ }
+
+ // Deine destination type - used the ColorModel and SampleModel of the Input Image
+ params.setDestinationType(new ImageTypeSpecifier(image.getColorModel(), image.getSampleModel()));
+
+ // Set the output stream to Second Argument
+ // imageOutputStream = ImageIO.createImageOutputStream( new FileOutputStream(args[1]) );
+ imageOutputStream = ImageIO.createImageOutputStream(out);
+ imageWriter.setOutput(imageOutputStream);
+
+ // Write the changed Image
+ imageWriter.write(null, new IIOImage(image, null, null), params);
+
+ // Close the streams
+ imageOutputStream.close();
+ imageWriter.dispose();
}
- catch (IOException ex)
+ else
{
- log.error("Error writing image to buffer", ex);
- throw ex;
+ ImageIO.write(image, extension, out);
}
}
+ catch (IOException ex)
+ {
+ log.error("Error writing image to buffer", ex);
+ throw ex;
+ }
return new ByteArrayInputStream(bos.toByteArray());
}
@@ -569,17 +610,41 @@
String resolutionName = "res-" + resolution;
- BufferedImage img = ImageUtils.getImageForResolution(original, resolution);
+ Map<String, String> params = parseParameters(resolution);
+ BufferedImage img = ImageUtils.getImageForResolution(original, resolution, params);
+
try
{
+ float quality = 0.8F;
+ if (StringUtils.isNotEmpty(params.get("quality")))
+ {
+ try
+ {
+ quality = NumberUtils.toFloat(params.get("quality"));
+ if (quality > 1.0F)
+ {
+ quality = 1.0F;
+ }
+ }
+ catch (NumberFormatException ex)
+ {
+ log.error("quality parameter must be a float number but was {}", params.get("quality"));
+ }
+ }
+ boolean forceProgressive = false;
+ if (StringUtils.isNotEmpty(params.get("progressive")))
+ {
+ forceProgressive = true;
+ }
if ("thumbnail".equals(resolutionTarget) || "preview".equals(resolutionTarget))
{
- ImageUtils.saveResolution(img, node, resolutionTarget, extension);
+ ImageUtils
+ .saveResolution(img, node, resolutionTarget, extension, quality, forceProgressive);
}
else
{
- ImageUtils.saveResolution(img, node, resolutionName, extension);
+ ImageUtils.saveResolution(img, node, resolutionName, extension, quality, forceProgressive);
}
}
catch (RepositoryException e)
@@ -617,28 +682,9 @@
return true;
}
- /**
- * Get image for a resolution
- * @param original original image
- * @param resolution resolution
- * @return new image
- */
- public static BufferedImage getImageForResolution(BufferedImage original, String resolution)
+ private static Map<String, String> parseParameters(String resolution)
{
- if (original == null)
- {
- throw new IllegalArgumentException("input image is null");
- }
- if (resolution == null || resolution.length() < 1)
- {
- throw new IllegalArgumentException("Invalid resolution: " + resolution);
- }
-
- BufferedImage img = null;
-
- resolution = StringUtils.lowerCase(resolution);
-
Map<String, String> params = new HashMap<String, String>();
if (StringUtils.contains(resolution, ";"))
{
@@ -659,7 +705,32 @@
}
}
}
+ return params;
+ }
+ /**
+ * Get image for a resolution
+ * @param original original image
+ * @param resolution resolution
+ * @return new image
+ */
+ public static BufferedImage getImageForResolution(BufferedImage original, String resolution,
+ Map<String, String> params)
+ {
+
+ if (original == null)
+ {
+ throw new IllegalArgumentException("input image is null");
+ }
+ if (resolution == null || resolution.length() < 1)
+ {
+ throw new IllegalArgumentException("Invalid resolution: " + resolution);
+ }
+
+ BufferedImage img = null;
+
+ resolution = StringUtils.lowerCase(resolution);
+
char controlChar = resolution.charAt(0);
Point size = parseForSize(resolution);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|