1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.geom
.AffineTransform
;
4 import java
.awt
.image
.AffineTransformOp
;
5 import java
.awt
.image
.BufferedImage
;
6 import java
.io
.ByteArrayInputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStream
;
11 import javax
.imageio
.ImageIO
;
13 import be
.nikiroo
.utils
.Image
;
14 import be
.nikiroo
.utils
.ImageUtils
;
17 * This class offer some utilities based around images and uses java.awt.
21 public class ImageUtilsAwt
extends ImageUtils
{
23 public void saveAsImage(Image img
, File target
, String format
)
26 BufferedImage image
= fromImage(img
);
31 ok
= ImageIO
.write(image
, format
, target
);
32 } catch (IOException e
) {
36 // Some formats are not reliable
38 if (!ok
&& !format
.equals("png")) {
40 ok
= ImageIO
.write(image
, "png", target
);
41 } catch (IllegalArgumentException e
) {
43 } catch (Exception e
) {
44 throw new IOException("Undocumented exception occured, "
45 + "converting to IOException", e
);
50 throw new IOException(
51 "Cannot find a writer for this image and format: "
54 } catch (IOException e
) {
55 throw new IOException("Cannot write image to " + target
, e
);
60 * Convert the given {@link Image} into a {@link BufferedImage} object,
61 * respecting the EXIF transformations if any.
66 * @return the {@link Image} object
71 public static BufferedImage
fromImage(Image img
) throws IOException
{
72 InputStream in
= new ByteArrayInputStream(img
.getData());
76 orientation
= getExifTransorm(in
);
77 } catch (Exception e
) {
78 // no EXIF transform, ok
83 BufferedImage image
= ImageIO
.read(in
);
86 throw new IOException("Failed to convert input to image");
89 // Note: this code has been found on Internet;
90 // thank you anonymous coder.
91 int width
= image
.getWidth();
92 int height
= image
.getHeight();
93 AffineTransform affineTransform
= new AffineTransform();
95 switch (orientation
) {
97 affineTransform
= null;
100 affineTransform
.scale(-1.0, 1.0);
101 affineTransform
.translate(-width
, 0);
103 case 3: // PI rotation
104 affineTransform
.translate(width
, height
);
105 affineTransform
.rotate(Math
.PI
);
108 affineTransform
.scale(1.0, -1.0);
109 affineTransform
.translate(0, -height
);
111 case 5: // - PI/2 and Flip X
112 affineTransform
.rotate(-Math
.PI
/ 2);
113 affineTransform
.scale(-1.0, 1.0);
115 case 6: // -PI/2 and -width
116 affineTransform
.translate(height
, 0);
117 affineTransform
.rotate(Math
.PI
/ 2);
119 case 7: // PI/2 and Flip
120 affineTransform
.scale(-1.0, 1.0);
121 affineTransform
.translate(-height
, 0);
122 affineTransform
.translate(0, width
);
123 affineTransform
.rotate(3 * Math
.PI
/ 2);
126 affineTransform
.translate(0, width
);
127 affineTransform
.rotate(3 * Math
.PI
/ 2);
130 affineTransform
= null;
134 if (affineTransform
!= null) {
135 AffineTransformOp affineTransformOp
= new AffineTransformOp(
136 affineTransform
, AffineTransformOp
.TYPE_BILINEAR
);
138 BufferedImage transformedImage
= new BufferedImage(width
, height
,
140 transformedImage
= affineTransformOp
141 .filter(image
, transformedImage
);
143 image
= transformedImage
;