Permalink
Browse files

CLEREZZA-716: Added Support alternate image processing libraries for …

…reading image data

git-svn-id: https://svn.apache.org/repos/asf/incubator/clerezza/trunk@1379493 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent a9d6220 commit 1de963e3a87b8c27e511d263cf2d687085b212c8 Daniel Spicar committed Aug 31, 2012
@@ -45,6 +45,10 @@
</dependency>
<dependency>
<groupId>org.apache.clerezza</groupId>
+ <artifactId>utils.imageprocessing</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.clerezza</groupId>
<artifactId>rdf.metadata</artifactId>
</dependency>
<dependency>
@@ -21,14 +21,15 @@
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import javax.imageio.ImageIO;
import javax.ws.rs.core.MediaType;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Service;
-import org.apache.clerezza.rdf.utils.GraphNode;
import org.apache.clerezza.rdf.core.LiteralFactory;
import org.apache.clerezza.rdf.metadata.MetaDataGenerator;
import org.apache.clerezza.rdf.ontologies.EXIF;
+import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.utils.imageprocessing.ImageReaderService;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
/**
* This class generates metadata about image data.
@@ -39,12 +40,16 @@
@Service(MetaDataGenerator.class)
public class ImageMetaDataGenerator implements MetaDataGenerator {
+ @Reference
+ private ImageReaderService imageReaderService;
+
@Override
public void generate(GraphNode node, byte[] data, MediaType mediaType) {
if (mediaType.getType().startsWith("image")) {
try {
- BufferedImage buffImage = ImageIO.read(new ByteArrayInputStream(data));
+ BufferedImage buffImage = imageReaderService.getBufferedImage(
+ new ByteArrayInputStream(data));
node.deleteProperties(EXIF.width);
node.deleteProperties(EXIF.height);
node.addProperty(EXIF.width, LiteralFactory.getInstance().
@@ -27,16 +27,16 @@
import javax.ws.rs.core.MediaType;
import org.apache.clerezza.platform.content.DiscobitsHandler;
import org.apache.clerezza.platform.content.InfoDiscobit;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.metadata.MetaDataGenerator;
+import org.apache.clerezza.rdf.ontologies.DISCOBITS;
+import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.utils.imageprocessing.ImageProcessor;
+import org.apache.clerezza.utils.imageprocessing.ImageReaderService;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
-import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.utils.imageprocessing.ImageProcessor;
-import org.apache.clerezza.rdf.ontologies.DISCOBITS;
-import org.apache.clerezza.rdf.utils.GraphNode;
-import org.apache.clerezza.rdf.metadata.MetaDataGenerator;
-import org.apache.felix.scr.annotations.Services;
import org.osgi.service.component.ComponentContext;
import org.osgi.util.tracker.ServiceTracker;
@@ -50,9 +50,9 @@
* @author mir
*/
@Component(metatype=true)
-@Services({
- @Service(MetaDataGenerator.class),
- @Service(AlternativeRepresentationGenerator.class)
+@Service({
+ MetaDataGenerator.class,
+ AlternativeRepresentationGenerator.class
})
public class AlternativeRepresentationGenerator implements MetaDataGenerator {
@@ -97,6 +97,9 @@ public int getWidth() {
@Reference
private ImageProcessor imageProcessor;
+
+ @Reference
+ private ImageReaderService imageReaderService;
@Property(value="100x100,200x200", description="Specifies the resolutions of alternative" +
" representations in the format [width]x[height]. Multiple resolutions" +
@@ -202,7 +205,8 @@ public UriRef generateAlternativeImage(GraphNode infoBitNode, int width, int hei
try {
isAltRepresentation.set(Boolean.TRUE);
InfoDiscobit infoBit = InfoDiscobit.createInstance(infoBitNode);
- BufferedImage buffImage = ImageIO.read(new ByteArrayInputStream(infoBit.getData()));
+ BufferedImage buffImage = imageReaderService.getBufferedImage(
+ new ByteArrayInputStream(infoBit.getData()));
return generateAlternativeImage(buffImage, new Resolution(width, height),
MediaType.valueOf(infoBit.getContentType()), infoBitNode, exact);
} catch (IOException ex) {
@@ -216,7 +220,8 @@ private void generateAlternativeImages(byte[] data, MediaType mediaType,
GraphNode node) throws RuntimeException {
try {
isAltRepresentation.set(Boolean.TRUE);
- BufferedImage buffImage = ImageIO.read(new ByteArrayInputStream(data));
+ BufferedImage buffImage = imageReaderService.getBufferedImage(
+ new ByteArrayInputStream(data));
int imgWidth = buffImage.getWidth();
int imgHeigth = buffImage.getHeight();
for (Resolution resolution : resolutions) {
@@ -35,16 +35,12 @@
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-
import javax.imageio.ImageIO;
-
-import org.osgi.service.component.ComponentContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.apache.clerezza.rdf.core.TripleCollection;
import org.apache.clerezza.rdf.core.serializedform.Parser;
import org.apache.clerezza.rdf.core.serializedform.Serializer;
import org.apache.clerezza.utils.imageprocessing.ImageProcessor;
+import org.apache.clerezza.utils.imageprocessing.ImageReaderService;
import org.apache.clerezza.utils.imageprocessing.metadataprocessing.ExifTagDataSet;
import org.apache.clerezza.utils.imageprocessing.metadataprocessing.IptcDataSet;
import org.apache.clerezza.utils.imageprocessing.metadataprocessing.MetaData;
@@ -54,7 +50,9 @@
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
-import org.apache.felix.scr.annotations.Services;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class implements interfaces that execute system calls to imageMagick.
@@ -78,9 +76,9 @@
@Property(name="minor_release_number", intValue=10, description="Specifies ImageMagick minor revision number (Syntax: release.version.majorRevision-minorRevision)."),
@Property(name="service.ranking", value="100")
})
-@Services({
- @Service(ImageProcessor.class),
- @Service(MetaDataProcessor.class)
+@Service({
+ ImageProcessor.class,
+ MetaDataProcessor.class
})
public class ImageMagickProvider extends ImageProcessor implements MetaDataProcessor {
@@ -94,6 +92,9 @@
@Reference
private Serializer serializer;
+
+ @Reference
+ private ImageReaderService imageReaderService;
private final Logger logger = LoggerFactory.getLogger(getClass());
@@ -564,8 +565,8 @@ private BufferedImage processImage(List<String> command, int quality,
return null;
}
- return ImageIO.read(execCommand(command, baos.toByteArray()).
- getInputStream());
+ return imageReaderService.getBufferedImage(execCommand(
+ command, baos.toByteArray()).getInputStream());
} catch (InterruptedException ex) {
logger.warn("ImageMagick has been interrupted");
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2012 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.clerezza.utils.imageprocessing;
+
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import javax.imageio.ImageIO;
+import javax.imageio.stream.ImageInputStream;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.osgi.framework.Constants;
+
+/**
+ * A service using Java Image IO to read images.
+ *
+ * @author daniel
+ */
+@Component(label="Java Image IO Image Reader",
+ description="Reads images using default Java Image IO.",
+ metatype=true)
+@Service(ImageReader.class)
+@Property(name=Constants.SERVICE_RANKING, intValue=0, propertyPrivate=false)
+public class ImageIOImageReader implements ImageReader {
+
+ @Override
+ public BufferedImage getBufferedImage(File file) throws IOException {
+ return ImageIO.read(file);
+ }
+
+ @Override
+ public BufferedImage getBufferedImage(InputStream in) throws IOException {
+ if(in instanceof ImageInputStream) {
+ return ImageIO.read((ImageInputStream) in);
+ }
+ return ImageIO.read(in);
+ }
+
+ @Override
+ public BufferedImage getBufferedImage(URL url) throws IOException {
+ return ImageIO.read(url);
+ }
+}
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2012 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.clerezza.utils.imageprocessing;
+
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * A service that reads image data and provides {@link BufferedImage}s.
+ *
+ * By providing a custom service it is easy to extend the platform's image
+ * processing capabilities with new image formats or use alternate processing
+ * libraries for reading image data.
+ *
+ * @author daniel
+ */
+public interface ImageReader {
+
+ /**
+ * Creates a {@link BufferedImage} from an {@link File}.
+ *
+ * @param file the image file.
+ * @return a {@link BufferedImage} containing the data of the specified
+ * image.
+ */
+ public BufferedImage getBufferedImage(File file) throws IOException;
+
+ /**
+ * Creates a {@link BufferedImage} from an {@link InputStream}.
+ *
+ * @param in a stream from which the image data can be read.
+ * @return a {@link BufferedImage} containing the data of the specified
+ * image.
+ */
+ public BufferedImage getBufferedImage(InputStream in) throws IOException;
+
+ /**
+ * Creates a {@link BufferedImage} from a {@link URL}.
+ *
+ * @param url the location of the image described as a URL.
+ * @return a {@link BufferedImage} containing the data of the specified
+ * image.
+ */
+ public BufferedImage getBufferedImage(URL url) throws IOException;
+}
Oops, something went wrong.

0 comments on commit 1de963e

Please sign in to comment.