IOUtils.write(in, data);
}
+ /**
+ * Generate an {@link InputStream} for this {@link Image}.
+ * <p>
+ * This {@link InputStream} will (always) be a new one, and <b>you</b> are
+ * responsible for it.
+ * <p>
+ * Note: take care that the {@link InputStream} <b>must not</b> live past
+ * the {@link Image} life time!
+ *
+ * @return the stream
+ *
+ * @throws IOException
+ * in case of I/O error
+ */
+ public InputStream newInputStream() throws IOException {
+ return new FileInputStream(data);
+ }
+
/**
* <b>Read</b> the actual image data, as a byte array.
+ * <p>
+ * Note: if possible, prefer the {@link Image#newInputStream()} method, as
+ * it can be more efficient.
*
* @return the image data
*/
public byte[] getData() {
try {
- FileInputStream in = new FileInputStream(data);
+ InputStream in = newInputStream();
try {
return IOUtils.toByteArray(in);
} finally {
/**
* Convert the given {@link Image} object into a Base64 representation of
* the same {@link Image} object.
+ * <p>
+ * Note: if possible, prefer the {@link Image#newInputStream()} method, as
+ * it can be more efficient.
*
* @return the Base64 representation
*/
public String toBase64() {
- return Base64.encodeBytes(getData());
+ try {
+ return StringUtils.base64(getData(), false);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
/**
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
-import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
* in case of IO error
*/
public static BufferedImage fromImage(Image img) throws IOException {
- InputStream in = new ByteArrayInputStream(img.getData());
-
- int orientation;
- try {
- orientation = getExifTransorm(in);
- } catch (Exception e) {
- // no EXIF transform, ok
- orientation = -1;
- }
-
- in.reset();
+ InputStream in = img.newInputStream();
BufferedImage image;
try {
- image = ImageIO.read(in);
- } catch (IllegalArgumentException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Undocumented exception occured, "
- + "converting to IOException", e);
- }
+ int orientation;
+ try {
+ orientation = getExifTransorm(in);
+ } catch (Exception e) {
+ // no EXIF transform, ok
+ orientation = -1;
+ }
- if (image == null) {
- throw new IOException("Failed to convert input to image");
- }
+ in.reset();
- // Note: this code has been found on Internet;
- // thank you anonymous coder.
- int width = image.getWidth();
- int height = image.getHeight();
- AffineTransform affineTransform = new AffineTransform();
-
- switch (orientation) {
- case 1:
- affineTransform = null;
- break;
- case 2: // Flip X
- affineTransform.scale(-1.0, 1.0);
- affineTransform.translate(-width, 0);
- break;
- case 3: // PI rotation
- affineTransform.translate(width, height);
- affineTransform.rotate(Math.PI);
- break;
- case 4: // Flip Y
- affineTransform.scale(1.0, -1.0);
- affineTransform.translate(0, -height);
- break;
- case 5: // - PI/2 and Flip X
- affineTransform.rotate(-Math.PI / 2);
- affineTransform.scale(-1.0, 1.0);
- break;
- case 6: // -PI/2 and -width
- affineTransform.translate(height, 0);
- affineTransform.rotate(Math.PI / 2);
- break;
- case 7: // PI/2 and Flip
- affineTransform.scale(-1.0, 1.0);
- affineTransform.translate(-height, 0);
- affineTransform.translate(0, width);
- affineTransform.rotate(3 * Math.PI / 2);
- break;
- case 8: // PI / 2
- affineTransform.translate(0, width);
- affineTransform.rotate(3 * Math.PI / 2);
- break;
- default:
- affineTransform = null;
- break;
- }
+ try {
+ image = ImageIO.read(in);
+ } catch (IllegalArgumentException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new IOException("Undocumented exception occured, "
+ + "converting to IOException", e);
+ }
+
+ if (image == null) {
+ throw new IOException("Failed to convert input to image");
+ }
+
+ // Note: this code has been found on Internet;
+ // thank you anonymous coder.
+ int width = image.getWidth();
+ int height = image.getHeight();
+ AffineTransform affineTransform = new AffineTransform();
+
+ switch (orientation) {
+ case 1:
+ affineTransform = null;
+ break;
+ case 2: // Flip X
+ affineTransform.scale(-1.0, 1.0);
+ affineTransform.translate(-width, 0);
+ break;
+ case 3: // PI rotation
+ affineTransform.translate(width, height);
+ affineTransform.rotate(Math.PI);
+ break;
+ case 4: // Flip Y
+ affineTransform.scale(1.0, -1.0);
+ affineTransform.translate(0, -height);
+ break;
+ case 5: // - PI/2 and Flip X
+ affineTransform.rotate(-Math.PI / 2);
+ affineTransform.scale(-1.0, 1.0);
+ break;
+ case 6: // -PI/2 and -width
+ affineTransform.translate(height, 0);
+ affineTransform.rotate(Math.PI / 2);
+ break;
+ case 7: // PI/2 and Flip
+ affineTransform.scale(-1.0, 1.0);
+ affineTransform.translate(-height, 0);
+ affineTransform.translate(0, width);
+ affineTransform.rotate(3 * Math.PI / 2);
+ break;
+ case 8: // PI / 2
+ affineTransform.translate(0, width);
+ affineTransform.rotate(3 * Math.PI / 2);
+ break;
+ default:
+ affineTransform = null;
+ break;
+ }
- if (affineTransform != null) {
- AffineTransformOp affineTransformOp = new AffineTransformOp(
- affineTransform, AffineTransformOp.TYPE_BILINEAR);
+ if (affineTransform != null) {
+ AffineTransformOp affineTransformOp = new AffineTransformOp(
+ affineTransform, AffineTransformOp.TYPE_BILINEAR);
- BufferedImage transformedImage = new BufferedImage(width, height,
- image.getType());
- transformedImage = affineTransformOp
- .filter(image, transformedImage);
+ BufferedImage transformedImage = new BufferedImage(width,
+ height, image.getType());
+ transformedImage = affineTransformOp.filter(image,
+ transformedImage);
- image = transformedImage;
+ image = transformedImage;
+ }
+ //
+ } finally {
+ in.close();
}
- //
return image;
}