1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.geom
.AffineTransform
;
4 import java
.awt
.image
.AffineTransformOp
;
5 import java
.awt
.image
.BufferedImage
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
10 import javax
.imageio
.ImageIO
;
12 import be
.nikiroo
.utils
.Image
;
13 import be
.nikiroo
.utils
.ImageUtils
;
16 * This class offer some utilities based around images and uses java.awt.
20 public class ImageUtilsAwt
extends ImageUtils
{
22 protected boolean check() {
23 // If we can get the class, it means we have access to it
24 @SuppressWarnings("unused")
25 Object test
= ImageIO
.class;
30 public void saveAsImage(Image img
, File target
, String format
)
33 BufferedImage image
= fromImage(img
);
38 ok
= ImageIO
.write(image
, format
, target
);
39 } catch (IOException e
) {
43 // Some formats are not reliable
45 if (!ok
&& !format
.equals("png")) {
47 ok
= ImageIO
.write(image
, "png", target
);
48 } catch (IllegalArgumentException e
) {
50 } catch (Exception e
) {
51 throw new IOException("Undocumented exception occured, "
52 + "converting to IOException", e
);
57 throw new IOException(
58 "Cannot find a writer for this image and format: "
61 } catch (IOException e
) {
62 throw new IOException("Cannot write image to " + target
, e
);
67 * Convert the given {@link Image} into a {@link BufferedImage} object,
68 * respecting the EXIF transformations if any.
73 * @return the {@link Image} object
78 public static BufferedImage
fromImage(Image img
) throws IOException
{
79 InputStream in
= img
.newInputStream();
84 orientation
= getExifTransorm(in
);
85 } catch (Exception e
) {
86 // no EXIF transform, ok
93 image
= ImageIO
.read(in
);
94 } catch (IllegalArgumentException e
) {
96 } catch (Exception e
) {
97 throw new IOException("Undocumented exception occured, "
98 + "converting to IOException", e
);
102 throw new IOException("Failed to convert input to image");
105 // Note: this code has been found on Internet;
106 // thank you anonymous coder.
107 int width
= image
.getWidth();
108 int height
= image
.getHeight();
109 AffineTransform affineTransform
= new AffineTransform();
111 switch (orientation
) {
113 affineTransform
= null;
116 affineTransform
.scale(-1.0, 1.0);
117 affineTransform
.translate(-width
, 0);
119 case 3: // PI rotation
120 affineTransform
.translate(width
, height
);
121 affineTransform
.rotate(Math
.PI
);
124 affineTransform
.scale(1.0, -1.0);
125 affineTransform
.translate(0, -height
);
127 case 5: // - PI/2 and Flip X
128 affineTransform
.rotate(-Math
.PI
/ 2);
129 affineTransform
.scale(-1.0, 1.0);
131 case 6: // -PI/2 and -width
132 affineTransform
.translate(height
, 0);
133 affineTransform
.rotate(Math
.PI
/ 2);
135 case 7: // PI/2 and Flip
136 affineTransform
.scale(-1.0, 1.0);
137 affineTransform
.translate(-height
, 0);
138 affineTransform
.translate(0, width
);
139 affineTransform
.rotate(3 * Math
.PI
/ 2);
142 affineTransform
.translate(0, width
);
143 affineTransform
.rotate(3 * Math
.PI
/ 2);
146 affineTransform
= null;
150 if (affineTransform
!= null) {
151 AffineTransformOp affineTransformOp
= new AffineTransformOp(
152 affineTransform
, AffineTransformOp
.TYPE_BILINEAR
);
154 BufferedImage transformedImage
= new BufferedImage(width
,
155 height
, image
.getType());
156 transformedImage
= affineTransformOp
.filter(image
,
159 image
= transformedImage
;