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
;
14 import be
.nikiroo
.utils
.StringUtils
;
17 * This class offer some utilities based around images and uses java.awt.
21 public class ImageUtilsAwt
extends ImageUtils
{
23 protected boolean check() {
24 // Will not work if ImageIO is not available
25 ImageIO
.getCacheDirectory();
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
);
103 if (img
.getSize() <= 2048) {
105 extra
= ", content: "
106 + new String(img
.getData(), "UTF-8");
107 } catch (Exception e
) {
108 extra
= ", content unavailable";
111 String ssize
= StringUtils
.formatNumber(img
.getSize());
112 throw new IOException(
113 "Failed to convert input to image, size was: " + ssize
117 // Note: this code has been found on Internet;
118 // thank you anonymous coder.
119 int width
= image
.getWidth();
120 int height
= image
.getHeight();
121 AffineTransform affineTransform
= new AffineTransform();
123 switch (orientation
) {
125 affineTransform
= null;
128 affineTransform
.scale(-1.0, 1.0);
129 affineTransform
.translate(-width
, 0);
131 case 3: // PI rotation
132 affineTransform
.translate(width
, height
);
133 affineTransform
.rotate(Math
.PI
);
136 affineTransform
.scale(1.0, -1.0);
137 affineTransform
.translate(0, -height
);
139 case 5: // - PI/2 and Flip X
140 affineTransform
.rotate(-Math
.PI
/ 2);
141 affineTransform
.scale(-1.0, 1.0);
143 case 6: // -PI/2 and -width
144 affineTransform
.translate(height
, 0);
145 affineTransform
.rotate(Math
.PI
/ 2);
147 case 7: // PI/2 and Flip
148 affineTransform
.scale(-1.0, 1.0);
149 affineTransform
.translate(-height
, 0);
150 affineTransform
.translate(0, width
);
151 affineTransform
.rotate(3 * Math
.PI
/ 2);
154 affineTransform
.translate(0, width
);
155 affineTransform
.rotate(3 * Math
.PI
/ 2);
158 affineTransform
= null;
162 if (affineTransform
!= null) {
163 AffineTransformOp affineTransformOp
= new AffineTransformOp(
164 affineTransform
, AffineTransformOp
.TYPE_BILINEAR
);
166 BufferedImage transformedImage
= new BufferedImage(width
,
167 height
, image
.getType());
168 transformedImage
= affineTransformOp
.filter(image
,
171 image
= transformedImage
;