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 public void saveAsImage(Image img
, File target
, String format
)
25 BufferedImage image
= fromImage(img
);
30 ok
= ImageIO
.write(image
, format
, target
);
31 } catch (IOException e
) {
35 // Some formats are not reliable
37 if (!ok
&& !format
.equals("png")) {
39 ok
= ImageIO
.write(image
, "png", target
);
40 } catch (IllegalArgumentException e
) {
42 } catch (Exception e
) {
43 throw new IOException("Undocumented exception occured, "
44 + "converting to IOException", e
);
49 throw new IOException(
50 "Cannot find a writer for this image and format: "
53 } catch (IOException e
) {
54 throw new IOException("Cannot write image to " + target
, e
);
59 * Convert the given {@link Image} into a {@link BufferedImage} object,
60 * respecting the EXIF transformations if any.
65 * @return the {@link Image} object
70 public static BufferedImage
fromImage(Image img
) throws IOException
{
71 InputStream in
= img
.newInputStream();
76 orientation
= getExifTransorm(in
);
77 } catch (Exception e
) {
78 // no EXIF transform, ok
85 image
= ImageIO
.read(in
);
86 } catch (IllegalArgumentException e
) {
88 } catch (Exception e
) {
89 throw new IOException("Undocumented exception occured, "
90 + "converting to IOException", e
);
94 throw new IOException("Failed to convert input to image");
97 // Note: this code has been found on Internet;
98 // thank you anonymous coder.
99 int width
= image
.getWidth();
100 int height
= image
.getHeight();
101 AffineTransform affineTransform
= new AffineTransform();
103 switch (orientation
) {
105 affineTransform
= null;
108 affineTransform
.scale(-1.0, 1.0);
109 affineTransform
.translate(-width
, 0);
111 case 3: // PI rotation
112 affineTransform
.translate(width
, height
);
113 affineTransform
.rotate(Math
.PI
);
116 affineTransform
.scale(1.0, -1.0);
117 affineTransform
.translate(0, -height
);
119 case 5: // - PI/2 and Flip X
120 affineTransform
.rotate(-Math
.PI
/ 2);
121 affineTransform
.scale(-1.0, 1.0);
123 case 6: // -PI/2 and -width
124 affineTransform
.translate(height
, 0);
125 affineTransform
.rotate(Math
.PI
/ 2);
127 case 7: // PI/2 and Flip
128 affineTransform
.scale(-1.0, 1.0);
129 affineTransform
.translate(-height
, 0);
130 affineTransform
.translate(0, width
);
131 affineTransform
.rotate(3 * Math
.PI
/ 2);
134 affineTransform
.translate(0, width
);
135 affineTransform
.rotate(3 * Math
.PI
/ 2);
138 affineTransform
= null;
142 if (affineTransform
!= null) {
143 AffineTransformOp affineTransformOp
= new AffineTransformOp(
144 affineTransform
, AffineTransformOp
.TYPE_BILINEAR
);
146 BufferedImage transformedImage
= new BufferedImage(width
,
147 height
, image
.getType());
148 transformedImage
= affineTransformOp
.filter(image
,
151 image
= transformedImage
;