Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

Commit

Permalink
Document Photo object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
langfingaz committed Feb 1, 2021
1 parent 1625640 commit 5c527b9
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 86 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/wahlzeit/main/ModelMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ protected void startUp(String rootDir) throws Exception {

loadGlobals();

/**
* @cw11 0. PhotoManager and PhotoFactory singletons are initialized,
* in this case with the domain specific variants
*/
LandscapePhotoFactory.initialize();
LandscapePhotoManager.initialize();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/wahlzeit/model/PhotoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public Photo createPhoto() {
}

/**
*
* @cw11 2.1.2. The factory instantiates a new photo object in-code.
*/
public Photo createPhoto(PhotoId id) {
return new Photo(id);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/wahlzeit/model/PhotoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public class PhotoManager extends ObjectManager {
protected PhotoTagCollector photoTagCollector;

/**
* @cw11 1. Get the PhotoManager/LandscapePhotoManager singleton. Depending on if PhotoManager.initialize()
* or LandscapePhotoManager.initialize() was initially called, this will return a reference to the corresponding
* manager singleton.
* <p>
* Public singleton access method.
*/
public static synchronized PhotoManager getInstance() {
Expand Down Expand Up @@ -359,7 +363,7 @@ protected void updateDependents(Persistent obj) throws SQLException {
}

/**
*
* @cw11 2. Reserve an unique photo id and delegate the object creation to the PhotoUtil class
*/
public Photo createPhoto(File file) throws Exception {
PhotoId id = PhotoId.getNextId();
Expand Down
176 changes: 93 additions & 83 deletions src/main/java/org/wahlzeit/model/PhotoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,88 +32,98 @@
* Images are created from a source in different sizes as needed by the app.
*/
public class PhotoUtil {

/**
*
*/
public static Photo createPhoto(File source, PhotoId id) throws Exception {
Photo result = PhotoFactory.getInstance().createPhoto(id);

Image sourceImage = createImageFiles(source, id);

int sourceWidth = sourceImage.getWidth(null);
int sourceHeight = sourceImage.getHeight(null);
result.setWidthAndHeight(sourceWidth, sourceHeight);

return result;
}

/**
*
*/
public static Image createImageFiles(File source, PhotoId id) throws Exception {
Image sourceImage = ImageIO.read(source);
assertIsValidImage(sourceImage);

int sourceWidth = sourceImage.getWidth(null);
int sourceHeight = sourceImage.getHeight(null);
assertHasValidSize(sourceWidth, sourceHeight);

for (PhotoSize size : PhotoSize.values()) {
if (!size.isWiderAndHigher(sourceWidth, sourceHeight)) {
createImageFile(sourceImage, id, size);
}
}

return sourceImage;
}

/**
*
*/
protected static void createImageFile(Image source, PhotoId id, PhotoSize size) throws Exception {
int sourceWidth = source.getWidth(null);
int sourceHeight = source.getHeight(null);

int targetWidth = size.calcAdjustedWidth(sourceWidth, sourceHeight);
int targetHeight = size.calcAdjustedHeight(sourceWidth, sourceHeight);

BufferedImage targetImage = scaleImage(source, targetWidth, targetHeight);
File target = new File(SysConfig.getPhotosDir().asString() + File.separator + id.asString() + size.asInt() + ".jpg");
ImageIO.write(targetImage, "jpg", target);

SysLog.logSysInfo("created image file for id: " + id.asString() + " of size: " + size.asString());
}

/**
*
*/
protected static BufferedImage scaleImage(Image source, int width, int height) {
source = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = result.createGraphics();
g2d.setBackground(Color.WHITE);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(source, 0, 0, null);
return result;
}

/**
* @methodtype assertion
*/
protected static void assertIsValidImage(Image image) {
if (image == null) {
throw new IllegalArgumentException("Not a valid photo!");
}
}

/**
* @methodtype assertion
*/
protected static void assertHasValidSize(int cw, int ch) {
if (PhotoSize.THUMB.isWiderAndHigher(cw, ch)) {
throw new IllegalArgumentException("Photo too small!");
}
}

/**
* @cw11 2.1. Create a photo object using the PhotoFactory/LandscapePhotoFactory and configure the object
* afterwards (set the image size)
*/
public static Photo createPhoto(File source, PhotoId id) throws Exception {
/**
* @cw11 2.1.1. Get the PhotoFactory/LandscapePhotoFactory singleton. Depending on if PhotoFactory.initialize()
* or LandscapePhotoFactory.initialize() was initially called, this will return a reference to the corresponding
* factory singleton.
* @cw11 2.1.2. Create a new object using the factory. The factory instantiates a new photo object in-code.
*/
Photo result = PhotoFactory.getInstance().createPhoto(id);

Image sourceImage = createImageFiles(source, id);

int sourceWidth = sourceImage.getWidth(null);
int sourceHeight = sourceImage.getHeight(null);
/**
* @cw11 2.1.3. In a second step, finish the initialization of the photo object by setting it's correct size
*/
result.setWidthAndHeight(sourceWidth, sourceHeight);

return result;
}

/**
*
*/
public static Image createImageFiles(File source, PhotoId id) throws Exception {
Image sourceImage = ImageIO.read(source);
assertIsValidImage(sourceImage);

int sourceWidth = sourceImage.getWidth(null);
int sourceHeight = sourceImage.getHeight(null);
assertHasValidSize(sourceWidth, sourceHeight);

for (PhotoSize size : PhotoSize.values()) {
if (!size.isWiderAndHigher(sourceWidth, sourceHeight)) {
createImageFile(sourceImage, id, size);
}
}

return sourceImage;
}

/**
*
*/
protected static void createImageFile(Image source, PhotoId id, PhotoSize size) throws Exception {
int sourceWidth = source.getWidth(null);
int sourceHeight = source.getHeight(null);

int targetWidth = size.calcAdjustedWidth(sourceWidth, sourceHeight);
int targetHeight = size.calcAdjustedHeight(sourceWidth, sourceHeight);

BufferedImage targetImage = scaleImage(source, targetWidth, targetHeight);
File target = new File(SysConfig.getPhotosDir().asString() + File.separator + id.asString() + size.asInt() + ".jpg");
ImageIO.write(targetImage, "jpg", target);

SysLog.logSysInfo("created image file for id: " + id.asString() + " of size: " + size.asString());
}

/**
*
*/
protected static BufferedImage scaleImage(Image source, int width, int height) {
source = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = result.createGraphics();
g2d.setBackground(Color.WHITE);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(source, 0, 0, null);
return result;
}

/**
* @methodtype assertion
*/
protected static void assertIsValidImage(Image image) {
if (image == null) {
throw new IllegalArgumentException("Not a valid photo!");
}
}

/**
* @methodtype assertion
*/
protected static void assertHasValidSize(int cw, int ch) {
if (PhotoSize.THUMB.isWiderAndHigher(cw, ch)) {
throw new IllegalArgumentException("Photo too small!");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public LandscapePhoto createPhoto() {
}

/**
*
* @cw11 2.1.2. The factory instantiates a new photo object in-code.
*/
@Override
public LandscapePhoto createPhoto(PhotoId id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class LandscapePhotoManager extends PhotoManager {
*/

/**
* @cw11 1. Get the PhotoManager/LandscapePhotoManager singleton. Depending on if PhotoManager.initialize()
* or LandscapePhotoManager.initialize() was initially called, this will return a reference to the corresponding
* manager singleton.
* <p>
* Public singleton access method.
*/
public static synchronized PhotoManager getInstance() {
Expand Down

0 comments on commit 5c527b9

Please sign in to comment.