ImageAsset + ReuseableImageAsset
* JAI ersetzt durch ImageIO (#1941) * ReusableImageAsset ausgeräumt und alle unnötigen Code-Dopplungen entfernt * ImageAsset umgestellt auf ImageIO. Fehlerhafte guessImageSize durch eine zuverlässiger funktionierende Methode ersetzt. git-svn-id: https://svn.libreccm.org/ccm/trunk@2461 8810af33-2d31-482b-a856-94f89814c4dfmaster
parent
e85fde631b
commit
c8be91ff92
|
|
@ -21,15 +21,12 @@ package com.arsdigita.cms;
|
||||||
import com.arsdigita.domain.DataObjectNotFoundException;
|
import com.arsdigita.domain.DataObjectNotFoundException;
|
||||||
import com.arsdigita.mimetypes.ImageMimeType;
|
import com.arsdigita.mimetypes.ImageMimeType;
|
||||||
import com.arsdigita.mimetypes.MimeType;
|
import com.arsdigita.mimetypes.MimeType;
|
||||||
import com.arsdigita.mimetypes.image.ImageSizer;
|
|
||||||
import com.arsdigita.mimetypes.image.ImageSizerFactory;
|
|
||||||
import com.arsdigita.persistence.DataCollection;
|
import com.arsdigita.persistence.DataCollection;
|
||||||
import com.arsdigita.persistence.DataObject;
|
import com.arsdigita.persistence.DataObject;
|
||||||
import com.arsdigita.persistence.Filter;
|
import com.arsdigita.persistence.Filter;
|
||||||
import com.arsdigita.persistence.OID;
|
import com.arsdigita.persistence.OID;
|
||||||
import com.arsdigita.persistence.SessionManager;
|
import com.arsdigita.persistence.SessionManager;
|
||||||
import com.arsdigita.versioning.VersionedACSObject;
|
import com.arsdigita.versioning.VersionedACSObject;
|
||||||
import java.awt.Dimension;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
|
@ -42,10 +39,9 @@ import javax.imageio.ImageIO;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>An {@link com.arsdigita.cms.Asset asset} representing an
|
* <p>An {@link com.arsdigita.cms.Asset asset} representing an image. An
|
||||||
* image. An ImageAsset is deleted when its parent content item is
|
* ImageAsset is deleted when its parent content item is deleted and is not
|
||||||
* deleted and is not intended to be reused between content
|
* intended to be reused between content items..</p>
|
||||||
* items..</p>
|
|
||||||
*
|
*
|
||||||
* @see com.arsdigita.cms.ReusableImageAsset
|
* @see com.arsdigita.cms.ReusableImageAsset
|
||||||
* @see com.arsdigita.cms.BinaryAsset
|
* @see com.arsdigita.cms.BinaryAsset
|
||||||
|
|
@ -75,9 +71,10 @@ public class ImageAsset extends BinaryAsset {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor. The contained <code>DataObject</code> is retrieved
|
* Constructor. The contained
|
||||||
* from the persistent storage mechanism with an <code>OID</code>
|
* <code>DataObject</code> is retrieved from the persistent storage
|
||||||
* specified by <i>oid</i>.
|
* mechanism with an
|
||||||
|
* <code>OID</code> specified by <i>oid</i>.
|
||||||
*
|
*
|
||||||
* @param oid The <code>OID</code> for the retrieved
|
* @param oid The <code>OID</code> for the retrieved
|
||||||
* <code>DataObject</code>.
|
* <code>DataObject</code>.
|
||||||
|
|
@ -87,14 +84,15 @@ public class ImageAsset extends BinaryAsset {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor. The contained <code>DataObject</code> is retrieved
|
* Constructor. The contained
|
||||||
* from the persistent storage mechanism with an <code>OID</code>
|
* <code>DataObject</code> is retrieved from the persistent storage
|
||||||
* specified by <i>id</i> and
|
* mechanism with an
|
||||||
|
* <code>OID</code> specified by <i>id</i> and
|
||||||
* <code>ImageAsset.BASE_DATA_OBJECT_TYPE</code>.
|
* <code>ImageAsset.BASE_DATA_OBJECT_TYPE</code>.
|
||||||
*
|
*
|
||||||
* @param id The <code>id</code> for the retrieved
|
* @param id The <code>id</code> for the retrieved <code>DataObject</code>.
|
||||||
* <code>DataObject</code>.
|
*
|
||||||
**/
|
*/
|
||||||
public ImageAsset(BigDecimal id) throws DataObjectNotFoundException {
|
public ImageAsset(BigDecimal id) throws DataObjectNotFoundException {
|
||||||
this(new OID(BASE_DATA_OBJECT_TYPE, id));
|
this(new OID(BASE_DATA_OBJECT_TYPE, id));
|
||||||
}
|
}
|
||||||
|
|
@ -111,6 +109,7 @@ public class ImageAsset extends BinaryAsset {
|
||||||
* @return the base PDL object type for this item. Child classes should
|
* @return the base PDL object type for this item. Child classes should
|
||||||
* override this method to return the correct value
|
* override this method to return the correct value
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public String getBaseDataObjectType() {
|
public String getBaseDataObjectType() {
|
||||||
return BASE_DATA_OBJECT_TYPE;
|
return BASE_DATA_OBJECT_TYPE;
|
||||||
}
|
}
|
||||||
|
|
@ -150,42 +149,46 @@ public class ImageAsset extends BinaryAsset {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the image asset from the specified file. Automatically guesses
|
* Load the image asset from the specified file. Automatically guesses the
|
||||||
* the mime type of the file. If the file is a jpeg, tries to automatically
|
* mime type of the file. If the file is a jpeg, tries to automatically
|
||||||
* determine width and height, as well.
|
* determine width and height, as well.
|
||||||
*
|
*
|
||||||
* @param fileName The original name of the file
|
* @param fileName The original name of the file
|
||||||
* @param file The actual file on the server
|
* @param file The actual file on the server
|
||||||
* @param defaultMimeType The default mime type for the file
|
* @param defaultMimeType The default mime type for the file (ignored)
|
||||||
*/
|
*/
|
||||||
public void loadFromFile(String fileName, File file, String defaultMimeType)
|
public void loadFromFile(String fileName, File file, String defaultMimeType)
|
||||||
throws IOException {
|
throws IOException, IllegalArgumentException {
|
||||||
|
|
||||||
|
BufferedImage image;
|
||||||
|
|
||||||
|
if (file == null) {
|
||||||
|
throw new IllegalArgumentException("Parameter file must not be null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
image = ImageIO.read(file);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new IOException("Can't read image format.");
|
||||||
|
}
|
||||||
|
|
||||||
// Guess mime type
|
// Guess mime type
|
||||||
MimeType mime = MimeType.guessMimeTypeFromFile(fileName);
|
MimeType mime = MimeType.guessMimeTypeFromFile(fileName);
|
||||||
|
if (mime == null && !(mime instanceof ImageMimeType)) {
|
||||||
if (mime != null && mime instanceof ImageMimeType) {
|
throw new IOException("Unsupported image format.");
|
||||||
guessSize(file, (ImageMimeType) mime);
|
|
||||||
} else {
|
|
||||||
// Set default mime type
|
|
||||||
mime = MimeType.loadMimeType(defaultMimeType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setMimeType(mime);
|
setMimeType(mime);
|
||||||
|
|
||||||
// Extract the filename
|
// Image size
|
||||||
int i = fileName.lastIndexOf("/");
|
readImageSize(image);
|
||||||
if (i > 0) {
|
|
||||||
fileName = fileName.substring(i + 1);
|
|
||||||
}
|
|
||||||
i = fileName.lastIndexOf("\\"); // DOS-style
|
|
||||||
if (i > 0) {
|
|
||||||
fileName = fileName.substring(i + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
setName(fileName);
|
// Extract filename
|
||||||
|
setName(extractFilename(fileName));
|
||||||
|
|
||||||
|
// Create InputStream
|
||||||
FileInputStream in = new FileInputStream(file);
|
FileInputStream in = new FileInputStream(file);
|
||||||
|
|
||||||
|
// Save image data
|
||||||
readBytes(in);
|
readBytes(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -208,31 +211,6 @@ public class ImageAsset extends BinaryAsset {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Guess image size by loading it from file. Set the WIDTH and HEIGHT
|
|
||||||
* attributes, if possible
|
|
||||||
*/
|
|
||||||
protected void guessSize(File file, ImageMimeType mime) {
|
|
||||||
BigDecimal width = null, height = null;
|
|
||||||
ImageSizer sizer = ImageSizerFactory.getImageSizer(mime.getMimeType());
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (sizer != null) {
|
|
||||||
Dimension d = sizer.computeImageSize(file);
|
|
||||||
if (d != null) {
|
|
||||||
width = new BigDecimal((int) d.getWidth());
|
|
||||||
height = new BigDecimal((int) d.getHeight());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException ex) {
|
|
||||||
s_log.error("IOException guessing file size", ex);
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
setWidth(width);
|
|
||||||
setHeight(height);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve all images in the database. Extremely expensive !
|
* Retrieve all images in the database. Extremely expensive !
|
||||||
*
|
*
|
||||||
|
|
@ -275,8 +253,9 @@ public class ImageAsset extends BinaryAsset {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resize this ImageAsset proportional to maxThumbnailWidth, if this ImageAsset
|
* Resize this ImageAsset proportional to maxThumbnailWidth, if this
|
||||||
* is wider then maxThumbnailWidth. Else just return this ImageAsset.
|
* ImageAsset is wider then maxThumbnailWidth. Else just return this
|
||||||
|
* ImageAsset.
|
||||||
*
|
*
|
||||||
* @param maxThumbnailWidth max image width
|
* @param maxThumbnailWidth max image width
|
||||||
* @return
|
* @return
|
||||||
|
|
@ -319,4 +298,32 @@ public class ImageAsset extends BinaryAsset {
|
||||||
return imageAsset;
|
return imageAsset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract filename from path
|
||||||
|
*
|
||||||
|
* @param fileName
|
||||||
|
* @return filename
|
||||||
|
*/
|
||||||
|
protected String extractFilename(String fileName) {
|
||||||
|
//
|
||||||
|
// Extract the filename
|
||||||
|
int i = fileName.lastIndexOf("/");
|
||||||
|
if (i > 0) {
|
||||||
|
fileName = fileName.substring(i + 1);
|
||||||
|
}
|
||||||
|
i = fileName.lastIndexOf("\\"); // DOS-style
|
||||||
|
if (i > 0) {
|
||||||
|
fileName = fileName.substring(i + 1);
|
||||||
|
}
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read image size from file.
|
||||||
|
*/
|
||||||
|
private void readImageSize(BufferedImage image) {
|
||||||
|
setWidth(new BigDecimal(image.getWidth()));
|
||||||
|
setHeight(new BigDecimal(image.getHeight()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,22 +20,14 @@ package com.arsdigita.cms;
|
||||||
|
|
||||||
import com.arsdigita.domain.DataObjectNotFoundException;
|
import com.arsdigita.domain.DataObjectNotFoundException;
|
||||||
import com.arsdigita.kernel.ACSObject;
|
import com.arsdigita.kernel.ACSObject;
|
||||||
import com.arsdigita.mimetypes.ImageMimeType;
|
|
||||||
import com.arsdigita.mimetypes.MimeType;
|
|
||||||
import com.arsdigita.persistence.DataCollection;
|
import com.arsdigita.persistence.DataCollection;
|
||||||
import com.arsdigita.persistence.DataObject;
|
import com.arsdigita.persistence.DataObject;
|
||||||
import com.arsdigita.persistence.Filter;
|
import com.arsdigita.persistence.Filter;
|
||||||
import com.arsdigita.persistence.OID;
|
import com.arsdigita.persistence.OID;
|
||||||
import com.arsdigita.persistence.SessionManager;
|
import com.arsdigita.persistence.SessionManager;
|
||||||
import com.arsdigita.versioning.VersionedACSObject;
|
import com.arsdigita.versioning.VersionedACSObject;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
import java.awt.image.RenderedImage;
|
|
||||||
import javax.media.jai.JAI;
|
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
|
@ -101,122 +93,11 @@ public class ReusableImageAsset extends ImageAsset {
|
||||||
* @return the base PDL object type for this item. Child classes should
|
* @return the base PDL object type for this item. Child classes should
|
||||||
* override this method to return the correct value
|
* override this method to return the correct value
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public String getBaseDataObjectType() {
|
public String getBaseDataObjectType() {
|
||||||
return BASE_DATA_OBJECT_TYPE;
|
return BASE_DATA_OBJECT_TYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BigDecimal getWidth() {
|
|
||||||
return (BigDecimal) get(WIDTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setWidth(BigDecimal width) {
|
|
||||||
set(WIDTH, width);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BigDecimal getHeight() {
|
|
||||||
return (BigDecimal) get(HEIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setHeight(BigDecimal height) {
|
|
||||||
set(HEIGHT, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the Blob content.
|
|
||||||
*
|
|
||||||
* @return the Blob content
|
|
||||||
*/
|
|
||||||
protected byte[] getContent() {
|
|
||||||
return (byte[]) get(CONTENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the Blob content.
|
|
||||||
*/
|
|
||||||
protected void setContent(byte[] content) {
|
|
||||||
set(CONTENT, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load the image asset from the specified file. Automatically guesses
|
|
||||||
* the mime type of the file. If the file is a jpeg, tries to automatically
|
|
||||||
* determine width and height, as well.
|
|
||||||
*
|
|
||||||
* @param fileName The original name of the file
|
|
||||||
* @param File The actual file on the server
|
|
||||||
* @param defaultMimeType The default mime type for the file
|
|
||||||
*/
|
|
||||||
public void loadFromFile(String fileName, File file, String defaultMimeType)
|
|
||||||
throws IOException {
|
|
||||||
|
|
||||||
// Guess mime type
|
|
||||||
MimeType mime = MimeType.guessMimeTypeFromFile(fileName);
|
|
||||||
if (s_log.isDebugEnabled()) {
|
|
||||||
s_log.debug("Mime type is " + (null == mime ? "null" : mime.
|
|
||||||
getMimeType()));
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderedImage image = JAI.create("FileLoad", file.getPath());
|
|
||||||
|
|
||||||
int width = image.getWidth();
|
|
||||||
int height = image.getHeight();
|
|
||||||
|
|
||||||
if (s_log.isDebugEnabled()) {
|
|
||||||
s_log.debug("Width: " + width);
|
|
||||||
s_log.debug("Height: " + height);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s_log.isDebugEnabled()) {
|
|
||||||
String[] props = image.getPropertyNames();
|
|
||||||
for (int i = 0; i < props.length; i++) {
|
|
||||||
String prop = props[i];
|
|
||||||
s_log.debug(prop + ": " + image.getProperty(prop));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
setWidth(new BigDecimal(width));
|
|
||||||
setHeight(new BigDecimal(height));
|
|
||||||
|
|
||||||
if (mime == null || !(mime instanceof ImageMimeType)) {
|
|
||||||
mime = MimeType.loadMimeType(defaultMimeType);
|
|
||||||
}
|
|
||||||
|
|
||||||
setMimeType(mime);
|
|
||||||
|
|
||||||
// Extract the filename
|
|
||||||
int i = fileName.lastIndexOf("/");
|
|
||||||
if (i > 0) {
|
|
||||||
fileName = fileName.substring(i + 1);
|
|
||||||
}
|
|
||||||
i = fileName.lastIndexOf("\\"); // DOS-style
|
|
||||||
if (i > 0) {
|
|
||||||
fileName = fileName.substring(i + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
setName(fileName);
|
|
||||||
|
|
||||||
FileInputStream in = new FileInputStream(file);
|
|
||||||
readBytes(in);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write the image asset content to a file.
|
|
||||||
*
|
|
||||||
* @param file The file on the server to write to.
|
|
||||||
*/
|
|
||||||
public void writeToFile(File file)
|
|
||||||
throws IOException {
|
|
||||||
FileOutputStream fs = new FileOutputStream(file);
|
|
||||||
try {
|
|
||||||
fs.write(getContent());
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
if (null != fs) {
|
|
||||||
fs.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve all images in the database. Expensive operation.
|
* Retrieve all images in the database. Expensive operation.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue