tx = session.beginTransaction();
Photo photo = (Photo) fileObject;
photo.setWidth(bi.getWidth());
photo.setHeight(bi.getHeight());
PhotoThumbnail thumbFile = new PhotoThumbnail();
thumbFile.setHidden(true);
thumbFile.setOwner(owner);
thumbFile.setParent(parentFolder);
thumbFile.setName(createFileName("", name, "_thumb"));
thumbFile.setDescription("Thumbnail for " + name);
session.save(thumbFile);
PhotoThumbnail previewFile = new PhotoThumbnail();
previewFile.setHidden(true);
previewFile.setOwner(owner);
previewFile.setParent(parentFolder);
previewFile.setName(createFileName("", name, "_preview"));
previewFile.setDescription("Preview image for " + name);
session.save(previewFile);
PhotoThumbnail slideFile = new PhotoThumbnail();
slideFile.setHidden(true);
slideFile.setOwner(owner);
slideFile.setParent(parentFolder);
slideFile.setName(createFileName("", name, "_slideshow"));
slideFile.setDescription("Slideshow image for " + name);
session.save(slideFile);
photo.setThumbnailImage(thumbFile);
photo.setSlideshowImage(slideFile);
photo.setPreviewImage(previewFile);
ByteArrayInputStream pngIn = new ByteArrayInputStream(pngOut.toByteArray());
ByteArrayOutputStream thumbOutStream = new ByteArrayOutputStream();
BufferedImage thumbbi = ImageIO.read(pngIn);
BufferedImage convertedThumbImage = createThumbnail(thumbbi, 128, 128, true);
thumbFile.setWidth(convertedThumbImage.getWidth());
thumbFile.setHeight(convertedThumbImage.getHeight());
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
// instantiate an ImageWriteParam object with default compression options
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.75f);
writer.setOutput(new MemoryCacheImageOutputStream(thumbOutStream));
IIOImage thumbimage = new IIOImage(convertedThumbImage, null, null);
writer.write(null, thumbimage, iwp);
pngIn.reset();
ByteArrayOutputStream slideOutStream = new ByteArrayOutputStream();
BufferedImage slidebi = ImageIO.read(pngIn);
BufferedImage convertedSlideImage = createThumbnail(slidebi, 640, 400, true);
slideFile.setWidth(convertedSlideImage.getWidth());
slideFile.setHeight(convertedSlideImage.getHeight());
writer.setOutput(new MemoryCacheImageOutputStream(slideOutStream));
IIOImage slideimage = new IIOImage(convertedSlideImage, null, null);
writer.write(null, slideimage, iwp);
pngIn.reset();
ByteArrayOutputStream previewOutStream = new ByteArrayOutputStream();
BufferedImage previewbi = ImageIO.read(pngIn);
BufferedImage convertedPreviewImage = createThumbnail(previewbi, 320, 200, true);
previewFile.setWidth(convertedPreviewImage.getWidth());
previewFile.setHeight(convertedPreviewImage.getHeight());
writer.setOutput(new MemoryCacheImageOutputStream(previewOutStream));
IIOImage previewimage = new IIOImage(convertedPreviewImage, null, null);
writer.write(null, previewimage, iwp);
thumbFile.setContentType("image/jpeg");
slideFile.setContentType("image/jpeg");
previewFile.setContentType("image/jpeg");
thumbFile.setSize(thumbOutStream.size());
slideFile.setSize(slideOutStream.size());
previewFile.setSize(previewOutStream.size());
session.save(thumbFile);
session.save(slideFile);
session.save(previewFile);
tx.commit();
tx = session.beginTransaction();
thumbFile.setNameOnDisk(createFileName(thumbFile.getId().toString() + "_", name, "_thumb"));
slideFile.setNameOnDisk(createFileName(slideFile.getId().toString() + "_", name, "_slide"));
previewFile.setNameOnDisk(createFileName(previewFile.getId().toString() + "_", name, "_preview"));
tx.commit();
saveFileFromStream(thumbFile, new ByteArrayInputStream(thumbOutStream.toByteArray()));
saveFileFromStream(slideFile, new ByteArrayInputStream(slideOutStream.toByteArray()));