NIHVIVO-161 Compensate for an image that was reduced for display.
This commit is contained in:
parent
8ae2563312
commit
14eb8a1311
2 changed files with 47 additions and 6 deletions
|
@ -536,13 +536,28 @@ public class ImageUploadController extends FreeMarkerHttpServlet {
|
||||||
final int height;
|
final int height;
|
||||||
final int width;
|
final int width;
|
||||||
|
|
||||||
private CropRectangle(int x, int y, int height, int width) {
|
CropRectangle(int x, int y, int height, int width) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Produce a new crop rectangle that compensates for scaling. */
|
||||||
|
public CropRectangle unscale(float scale) {
|
||||||
|
int newX = (int) (x / scale);
|
||||||
|
int newY = (int) (y / scale);
|
||||||
|
int newHeight = (int) (height / scale);
|
||||||
|
int newWidth = (int) (width / scale);
|
||||||
|
return new CropRectangle(newX, newY, newHeight, newWidth);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "CropRectangle[x=" + x + ", y=" + y + ", w=" + width
|
||||||
|
+ ", h=" + height + "]";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -28,6 +28,7 @@ import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadController.CropRectangle;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadController.Dimensions;
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadController.Dimensions;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadController.UserMistakeException;
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadController.UserMistakeException;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||||
|
@ -42,6 +43,12 @@ import edu.cornell.mannlib.vitro.webapp.filestorage.uploadrequest.FileUploadServ
|
||||||
public class ImageUploadHelper {
|
public class ImageUploadHelper {
|
||||||
private static final Log log = LogFactory.getLog(ImageUploadHelper.class);
|
private static final Log log = LogFactory.getLog(ImageUploadHelper.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the main image is larger than this, it will be displayed at reduced
|
||||||
|
* scale.
|
||||||
|
*/
|
||||||
|
public static final int MAXIMUM_IMAGE_DISPLAY_WIDTH = 500;
|
||||||
|
|
||||||
/** Recognized file extensions mapped to MIME-types. */
|
/** Recognized file extensions mapped to MIME-types. */
|
||||||
private static final Map<String, String> RECOGNIZED_FILE_TYPES = createFileTypesMap();
|
private static final Map<String, String> RECOGNIZED_FILE_TYPES = createFileTypesMap();
|
||||||
|
|
||||||
|
@ -325,9 +332,12 @@ public class ImageUploadHelper {
|
||||||
* width, height).
|
* width, height).
|
||||||
*/
|
*/
|
||||||
private InputStream scaleImageForThumbnail(InputStream source,
|
private InputStream scaleImageForThumbnail(InputStream source,
|
||||||
ImageUploadController.CropRectangle crop) throws IOException {
|
CropRectangle crop) throws IOException {
|
||||||
BufferedImage bsrc = ImageIO.read(source);
|
BufferedImage bsrc = ImageIO.read(source);
|
||||||
|
|
||||||
|
// If the image was displayed in reduced form, adjust the crop info.
|
||||||
|
crop = adjustForScaledImageDisplay(bsrc.getWidth(), crop);
|
||||||
|
|
||||||
// Insure that x and y fall within the image dimensions.
|
// Insure that x and y fall within the image dimensions.
|
||||||
int x = Math.max(0, Math.min(bsrc.getWidth(), Math.abs(crop.x)));
|
int x = Math.max(0, Math.min(bsrc.getWidth(), Math.abs(crop.x)));
|
||||||
int y = Math.max(0, Math.min(bsrc.getHeight(), Math.abs(crop.y)));
|
int y = Math.max(0, Math.min(bsrc.getHeight(), Math.abs(crop.y)));
|
||||||
|
@ -340,10 +350,8 @@ public class ImageUploadHelper {
|
||||||
double scaleWidth = ((double) THUMBNAIL_WIDTH) / ((double) w);
|
double scaleWidth = ((double) THUMBNAIL_WIDTH) / ((double) w);
|
||||||
double scaleHeight = ((double) THUMBNAIL_HEIGHT) / ((double) h);
|
double scaleHeight = ((double) THUMBNAIL_HEIGHT) / ((double) h);
|
||||||
|
|
||||||
log.debug("Generating a thumbnail, initial crop info: " + crop.x + ", "
|
log.debug("Generating a thumbnail, initial crop info: " + crop);
|
||||||
+ crop.y + ", " + crop.width + ", " + crop.height);
|
log.debug("Generating a thumbnail, bounded crop info: " + crop);
|
||||||
log.debug("Generating a thumbnail, bounded crop info: " + x + ", " + y
|
|
||||||
+ ", " + w + ", " + h);
|
|
||||||
log.debug("Generating a thumbnail, scales: " + scaleWidth + ", "
|
log.debug("Generating a thumbnail, scales: " + scaleWidth + ", "
|
||||||
+ scaleHeight);
|
+ scaleHeight);
|
||||||
|
|
||||||
|
@ -364,6 +372,24 @@ public class ImageUploadHelper {
|
||||||
return new ByteArrayInputStream(buffer.toByteArray());
|
return new ByteArrayInputStream(buffer.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the source image was too big to fit in the page, then it was displayed
|
||||||
|
* at a reduced scale. The crop values must expand to apply to the
|
||||||
|
* full-sized image.
|
||||||
|
*/
|
||||||
|
private CropRectangle adjustForScaledImageDisplay(int imageWidth,
|
||||||
|
CropRectangle crop) {
|
||||||
|
if (imageWidth <= MAXIMUM_IMAGE_DISPLAY_WIDTH) {
|
||||||
|
return crop;
|
||||||
|
}
|
||||||
|
|
||||||
|
float displayScale = ((float) MAXIMUM_IMAGE_DISPLAY_WIDTH)
|
||||||
|
/ ((float) imageWidth);
|
||||||
|
log.debug("Generating a thumbnail, unscaled crop info: " + crop
|
||||||
|
+ ", displayScale=" + displayScale);
|
||||||
|
return crop.unscale(displayScale);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find out how big the main image is.
|
* Find out how big the main image is.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue