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 // Will not work if ImageIO is not available
24 ImageIO
.getCacheDirectory();
29 public void saveAsImage(Image img
, File target
, String format
)
32 BufferedImage image
= fromImage(img
);
37 ok
= ImageIO
.write(image
, format
, target
);
38 } catch (IOException e
) {
42 // Some formats are not reliable
44 if (!ok
&& !format
.equals("png")) {
46 ok
= ImageIO
.write(image
, "png", target
);
47 } catch (IllegalArgumentException e
) {
49 } catch (Exception e
) {
50 throw new IOException("Undocumented exception occured, "
51 + "converting to IOException", e
);
56 throw new IOException(
57 "Cannot find a writer for this image and format: "
60 } catch (IOException e
) {
61 throw new IOException("Cannot write image to " + target
, e
);
66 * Convert the given {@link Image} into a {@link BufferedImage} object,
67 * respecting the EXIF transformations if any.
72 * @return the {@link Image} object
77 public static BufferedImage
fromImage(Image img
) throws IOException
{
78 InputStream in
= img
.newInputStream();
83 orientation
= getExifTransorm(in
);
84 } catch (Exception e
) {
85 // no EXIF transform, ok
92 image
= ImageIO
.read(in
);
93 } catch (IllegalArgumentException e
) {
95 } catch (Exception e
) {
96 throw new IOException("Undocumented exception occured, "
97 + "converting to IOException", e
);
101 throw new IOException("Failed to convert input to image");
104 // Note: this code has been found on Internet;
105 // thank you anonymous coder.
106 int width
= image
.getWidth();
107 int height
= image
.getHeight();
108 AffineTransform affineTransform
= new AffineTransform();
110 switch (orientation
) {
112 affineTransform
= null;
115 affineTransform
.scale(-1.0, 1.0);
116 affineTransform
.translate(-width
, 0);
118 case 3: // PI rotation
119 affineTransform
.translate(width
, height
);
120 affineTransform
.rotate(Math
.PI
);
123 affineTransform
.scale(1.0, -1.0);
124 affineTransform
.translate(0, -height
);
126 case 5: // - PI/2 and Flip X
127 affineTransform
.rotate(-Math
.PI
/ 2);
128 affineTransform
.scale(-1.0, 1.0);
130 case 6: // -PI/2 and -width
131 affineTransform
.translate(height
, 0);
132 affineTransform
.rotate(Math
.PI
/ 2);
134 case 7: // PI/2 and Flip
135 affineTransform
.scale(-1.0, 1.0);
136 affineTransform
.translate(-height
, 0);
137 affineTransform
.translate(0, width
);
138 affineTransform
.rotate(3 * Math
.PI
/ 2);
141 affineTransform
.translate(0, width
);
142 affineTransform
.rotate(3 * Math
.PI
/ 2);
145 affineTransform
= null;
149 if (affineTransform
!= null) {
150 AffineTransformOp affineTransformOp
= new AffineTransformOp(
151 affineTransform
, AffineTransformOp
.TYPE_BILINEAR
);
153 BufferedImage transformedImage
= new BufferedImage(width
,
154 height
, image
.getType());
155 transformedImage
= affineTransformOp
.filter(image
,
158 image
= transformedImage
;