App

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

AndroidImageFile.java
Go to the documentation of this file.
1 package mhr.appandroid.adapters;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.util.Properties;
12
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import javax.xml.transform.OutputKeys;
17 import javax.xml.transform.Transformer;
18 import javax.xml.transform.TransformerConfigurationException;
19 import javax.xml.transform.TransformerException;
20 import javax.xml.transform.TransformerFactory;
21 import javax.xml.transform.dom.DOMSource;
22 import javax.xml.transform.stream.StreamResult;
23
24 import org.w3c.dom.Document;
25 import org.xml.sax.SAXException;
26
27 import android.R.string;
28 import android.app.Activity;
29 import android.graphics.Bitmap;
30 import android.graphics.Bitmap.CompressFormat;
31 import android.graphics.BitmapFactory;
32 import android.graphics.Canvas;
33 import android.graphics.Paint;
34 import android.graphics.Rect;
35
36 import mhr.appcore.bitmap.BitmapInfo;
37 import mhr.appcore.interfaces.ImageFile;
38 import mhr.appcore.interfaces.PDBitmap;
39 import mhr.appcore.utils.NotImplementedException;
40
44 public class AndroidImageFile implements ImageFile {
45
46 String fname;
47 File iDir;
48 Document doc;
49 protected Activity activity;
50
51 //===== INTERFACES, CLASSES, ENUMS ==========================================================================================================//
52 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
53 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
54
55 //===== FIELDS ==============================================================================================================================//
56 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
57 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
58
59 //===== CONSTRUCTORS, DESTRUCTORS, RELATED METHODS ==========================================================================================//
60 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
61 protected AndroidImageFile() {
62 }
63
64 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
65 public static AndroidImageFile createImageFile(String fname, Activity a) {
66 AndroidImageFile i = new AndroidImageFile();
67 i.activity = a;
68 i.fname = fname;
69 File root = a.getExternalFilesDir(null);
70 i.iDir = new File(root, fname);
71 i.iDir.mkdirs();
72
73 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
74 DocumentBuilder documentBuilder = null;
75 try {
76 documentBuilder = documentBuilderFactory.newDocumentBuilder();
77 } catch (ParserConfigurationException e) {
78 throw new RuntimeException("Unhandled exception " + e.toString());
79 }
80 i.doc = documentBuilder.newDocument();
81 return i;
82 }
83
84 public static AndroidImageFile openImageFile(String fname, Activity a) {
85 AndroidImageFile i = new AndroidImageFile();
86 i.activity = a;
87 i.fname = fname;
88 File root = a.getExternalFilesDir(null);
89 i.iDir = new File(root, fname);
90 if (!i.iDir.exists()) {
91 throw new RuntimeException("File doesnt exists");
92 }
93 InputStream is = null;
94 try {
95 is = new FileInputStream(new File(i.iDir, "image.xml"));
96 } catch (FileNotFoundException e) {
97 throw new RuntimeException("Unhandled exception " + e.toString());
98 }
99
100 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
101 DocumentBuilder builder = null;
102 try {
103 builder = factory.newDocumentBuilder();
104 } catch (ParserConfigurationException e) {
105 try {
106 is.close();
107 } catch (IOException e1) {
108 throw new RuntimeException("Unhandled exception " + e.toString());
109 }
110 throw new RuntimeException("Unhandled exception " + e.toString());
111 }
112 Document dom = null;
113 try {
114 dom = builder.parse(is);
115 } catch (SAXException e) {
116 throw new RuntimeException("Unhandled exception " + e.toString());
117 } catch (IOException e) {
118 throw new RuntimeException("Unhandled exception " + e.toString());
119 } finally {
120 try {
121 is.close();
122 } catch (IOException e) {
123 throw new RuntimeException("Unhandled exception " + e.toString());
124 }
125 }
126 i.doc = dom;
127 return i;
128 }
129
130 //===== METHODS =============================================================================================================================//
131 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
132 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
133 public static Bitmap loadThumb(File root, int width, int height) {
134 // BitmapFactory.Options o = new BitmapFactory.Options();
135 // o.inScaled = false;
136 InputStream is = null;
137 // Bitmap tmp = null;
138 Bitmap retVal = null;
139
140 try {
141 is = new FileInputStream(new File(root, "out.png"));
142 BitmapFactory.Options o = new BitmapFactory.Options();
143 o.inJustDecodeBounds = true; // Pouze precte rozmery obr.
144 BitmapFactory.decodeStream(is, null, o);
145 is.close();
146 is = null;
147
148 int wscale = o.outWidth / width;
149 int hscale = o.outHeight / height;
150 o.inJustDecodeBounds = false;
151 o.inSampleSize = wscale > hscale ? wscale : hscale;
152
153 is = new FileInputStream(new File(root, "out.png"));
154 retVal = BitmapFactory.decodeStream(is, null, o);
155
156 } catch (FileNotFoundException e) {
157 throw new RuntimeException("Unhandled exception " + e.toString());
158 } catch (IOException e) {
159 // Zavírání streamu
160 } finally {
161 if (is != null) {
162 try {
163 is.close();
164 } catch (IOException e) {
165 throw new RuntimeException("Unhandled exception " + e.toString());
166 }
167 }
168 }
169 return retVal;
170 }
171
172 public static void deleteFile(String fname, Activity a) {
173 File root = a.getExternalFilesDir(null);
174 File imageRoot = new File(root, fname);
175 if (!imageRoot.exists()) {
176 return;
177 }
178 File[] files = imageRoot.listFiles();
179 for (File f : files) {
180 f.delete();
181 }
182 imageRoot.delete();
183 }
184
185 //===== CALLBACKS ===========================================================================================================================//
186
187 @Override
188 public Document getDocument() {
189 return doc;
190 }
191
192 @Override
193 public void writeDocument() {
194 TransformerFactory factory = TransformerFactory.newInstance();
195 Transformer transformer = null;
196 try {
197 transformer = factory.newTransformer();
198 } catch (TransformerConfigurationException e) {
199 throw new RuntimeException("Unhandled exception " + e.toString());
200 }
201 Properties outFormat = new Properties();
202 outFormat.setProperty(OutputKeys.INDENT, "yes");
203 outFormat.setProperty(OutputKeys.METHOD, "xml");
204 outFormat.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
205 outFormat.setProperty(OutputKeys.VERSION, "1.0");
206 outFormat.setProperty(OutputKeys.ENCODING, "UTF-8");
207 transformer.setOutputProperties(outFormat);
208 DOMSource domSource = new DOMSource(doc);
209 OutputStream output = null;
210 try {
211 output = new FileOutputStream(new File(iDir, "image.xml"));
212 } catch (FileNotFoundException e) {
213 throw new RuntimeException("Unhandled exception " + e.toString());
214 }
215 StreamResult result = new StreamResult(output);
216 try {
217 transformer.transform(domSource, result);
218 output.flush();
219 } catch (TransformerException e) {
220 throw new RuntimeException("Unhandled exception " + e.toString());
221 } catch (IOException e) {
222 throw new RuntimeException("Unhandled exception " + e.toString());
223 } finally {
224 try {
225 output.close();
226 } catch (IOException e) {
227 throw new RuntimeException("Unhandled exception " + e.toString());
228 }
229 }
230 }
231
232 @Override
233 public PDBitmap createBitmap(BitmapInfo i) {
234 return new APDBitmap(i);
235 }
236
237 @Override
238 public PDBitmap loadBitmap(String fName) {
239 BitmapFactory.Options o = new BitmapFactory.Options();
240 o.inScaled = false;
241 InputStream is = null;
242 APDBitmap retVal = null;
243 try {
244 is = new FileInputStream(new File(iDir, fName + ".png"));
245 retVal = new APDBitmap(BitmapFactory.decodeStream(is));
246 } catch (FileNotFoundException e) {
247 throw new RuntimeException("Unhandled exception " + e.toString());
248 } finally {
249 try {
250 is.close();
251 } catch (IOException e) {
252 throw new RuntimeException("Unhandled exception " + e.toString());
253 }
254 }
255 return retVal;
256 }
257
258 @Override
259 public void writeBitmap(PDBitmap bitmap, String fName) {
260 if (!(bitmap instanceof APDBitmap)) {
261 throw new IllegalArgumentException("Wrong bitmap");
262 }
263 Bitmap b = ((APDBitmap) bitmap).getBitmap();
264 File out = new File(iDir, fName + ".png");
265 OutputStream os = null;
266 try {
267 os = new FileOutputStream(out);
268 b.compress(CompressFormat.PNG, 100, os);
269 os.flush();
270 } catch (IOException e) {
271 throw new RuntimeException("Unhandled exception " + e.toString());
272 } finally {
273 if (os != null) {
274 try {
275 os.close();
276 } catch (IOException e) {
277 throw new RuntimeException("Unhandled exception " + e.toString());
278 }
279 }
280 }
281 }
282
283 @Override
284 public PDBitmap loadMask(String fName) {
285 BitmapFactory.Options o = new BitmapFactory.Options();
286 o.inScaled = false;
287 InputStream is = null;
288 APDBitmap retVal = null;
289 try {
290 is = new FileInputStream(new File(iDir, fName + ".png"));
291 retVal = new APDBitmap(BitmapFactory.decodeStream(is).extractAlpha());
292 } catch (FileNotFoundException e) {
293 throw new RuntimeException("Unhandled exception " + e.toString());
294 } finally {
295 try {
296 is.close();
297 } catch (IOException e) {
298 throw new RuntimeException("Unhandled exception " + e.toString());
299 }
300 }
301 return retVal;
302 }
303
304 @Override
305 public void writeMask(PDBitmap mask, String fName) {
306 if (!(mask instanceof APDBitmap)) {
307 throw new IllegalArgumentException("Wrong bitmap");
308 }
309 Bitmap b = ((APDBitmap) mask).getBitmap();
310
311 Bitmap tmp = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
312 Canvas c = new Canvas(tmp);
313 Paint p = new Paint();
314 p.setColor(0xFF000000);
315 c.drawARGB(0, 0, 0, 0);
316 c.drawBitmap(b, null, new Rect(0, 0, b.getWidth(), b.getHeight()), p);
317
318 writeBitmap(new APDBitmap(tmp), fName);
319
320 tmp.recycle();
321 }
322
323 @Override
324 public PDBitmap loadOutput() {
325 BitmapFactory.Options o = new BitmapFactory.Options();
326 o.inScaled = false;
327 InputStream is = null;
328 APDBitmap retVal = null;
329 try {
330 is = new FileInputStream(new File(iDir, "out.png"));
331 retVal = new APDBitmap(BitmapFactory.decodeStream(is));
332 } catch (FileNotFoundException e) {
333 throw new RuntimeException("Unhandled exception " + e.toString());
334 } finally {
335 try {
336 is.close();
337 } catch (IOException e) {
338 throw new RuntimeException("Unhandled exception " + e.toString());
339 }
340 }
341 return retVal;
342 }
343
344 @Override
345 public void saveOutput(PDBitmap bitmap) {
346 if (!(bitmap instanceof APDBitmap)) {
347 throw new IllegalArgumentException("Wrong bitmap");
348 }
349 Bitmap b = ((APDBitmap) bitmap).getBitmap();
350 File out = new File(iDir, "out.png");
351 OutputStream os = null;
352 try {
353 os = new FileOutputStream(out);
354 b.compress(CompressFormat.PNG, 100, os);
355 os.flush();
356 } catch (IOException e) {
357 throw new RuntimeException("Unhandled exception " + e.toString());
358 } finally {
359 if (os != null) {
360 try {
361 os.close();
362 } catch (IOException e) {
363 throw new RuntimeException("Unhandled exception " + e.toString());
364 }
365 }
366 }
367 }
368
369
370 @Override
371 public void deleteFile() {
372 deleteFile(fname, activity);
373 }
374
375 }