1 package be
.nikiroo
.utils
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
7 import be
.nikiroo
.utils
.serial
.SerialUtils
;
10 * This class offer some utilities based around images.
14 public abstract class ImageUtils
{
15 private static ImageUtils instance
= newObject();
18 * Get a (unique) instance of an {@link ImageUtils} compatible with your
21 * @return an {@link ImageUtils}
23 public static ImageUtils
getInstance() {
28 * Save the given resource as an image on disk using the given image format
29 * for content, or with "png" format if it fails.
36 * the file format ("png", "jpeg", "bmp"...)
39 * in case of I/O error
41 public abstract void saveAsImage(Image img
, File target
, String format
)
45 * Return the EXIF transformation flag of this image if any.
48 * Note: this code has been found on internet; thank you anonymous coder.
52 * the data {@link InputStream}
54 * @return the transformation flag if any
59 protected static int getExifTransorm(InputStream in
) throws IOException
{
60 int[] exif_data
= new int[100];
64 /* Read File head, check for JPEG SOI + Exif APP1 */
65 for (int i
= 0; i
< 4; i
++)
66 exif_data
[i
] = in
.read();
68 if (exif_data
[0] != 0xFF || exif_data
[1] != 0xD8
69 || exif_data
[2] != 0xFF || exif_data
[3] != 0xE1)
72 /* Get the marker parameter length count */
73 int length
= (in
.read() << 8 | in
.read());
75 /* Length includes itself, so must be at least 2 */
76 /* Following Exif data length must be at least 6 */
80 /* Read Exif head, check for "Exif" */
81 for (int i
= 0; i
< 6; i
++)
82 exif_data
[i
] = in
.read();
84 if (exif_data
[0] != 0x45 || exif_data
[1] != 0x78
85 || exif_data
[2] != 0x69 || exif_data
[3] != 0x66
86 || exif_data
[4] != 0 || exif_data
[5] != 0)
90 length
= length
> exif_data
.length ? exif_data
.length
: length
;
91 for (int i
= 0; i
< length
; i
++)
92 exif_data
[i
] = in
.read();
95 return -1; /* Length of an IFD entry */
97 /* Discover byte order */
98 if (exif_data
[0] == 0x49 && exif_data
[1] == 0x49)
100 else if (exif_data
[0] == 0x4D && exif_data
[1] == 0x4D)
106 if (is_motorola
== 1) {
107 if (exif_data
[2] != 0)
109 if (exif_data
[3] != 0x2A)
112 if (exif_data
[3] != 0)
114 if (exif_data
[2] != 0x2A)
118 /* Get first IFD offset (offset to IFD0) */
120 if (is_motorola
== 1) {
121 if (exif_data
[4] != 0)
123 if (exif_data
[5] != 0)
125 offset
= exif_data
[6];
127 offset
+= exif_data
[7];
129 if (exif_data
[7] != 0)
131 if (exif_data
[6] != 0)
133 offset
= exif_data
[5];
135 offset
+= exif_data
[4];
137 if (offset
> length
- 2)
138 return -1; /* check end of data segment */
140 /* Get the number of directory entries contained in this IFD */
142 if (is_motorola
== 1) {
143 number_of_tags
= exif_data
[offset
];
144 number_of_tags
<<= 8;
145 number_of_tags
+= exif_data
[offset
+ 1];
147 number_of_tags
= exif_data
[offset
+ 1];
148 number_of_tags
<<= 8;
149 number_of_tags
+= exif_data
[offset
];
151 if (number_of_tags
== 0)
155 /* Search for Orientation Tag in IFD0 */
157 if (offset
> length
- 12)
158 return -1; /* check end of data segment */
161 if (is_motorola
== 1) {
162 tagnum
= exif_data
[offset
];
164 tagnum
+= exif_data
[offset
+ 1];
166 tagnum
= exif_data
[offset
+ 1];
168 tagnum
+= exif_data
[offset
];
170 if (tagnum
== 0x0112)
171 break; /* found Orientation Tag */
172 if (--number_of_tags
== 0)
177 /* Get the Orientation value */
178 if (is_motorola
== 1) {
179 if (exif_data
[offset
+ 8] != 0)
181 set_flag
= exif_data
[offset
+ 9];
183 if (exif_data
[offset
+ 9] != 0)
185 set_flag
= exif_data
[offset
+ 8];
194 * Check that the class can operate (for instance, that all the required
195 * libraries or frameworks are present).
197 * @return TRUE if it works
199 abstract protected boolean check();
202 * Create a new {@link ImageUtils}.
204 * @return the {@link ImageUtils}
206 private static ImageUtils
newObject() {
207 for (String clazz
: new String
[] { "be.nikiroo.utils.ui.ImageUtilsAwt",
208 "be.nikiroo.utils.android.ImageUtilsAndroid" }) {
210 ImageUtils obj
= (ImageUtils
) SerialUtils
.createObject(clazz
);
214 } catch (Throwable e
) {