1 package mhr.appandroid.adapters;
3 import java.io.ByteArrayOutputStream;
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;
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;
24 import org.w3c.dom.Document;
25 import org.xml.sax.SAXException;
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;
36 import mhr.appcore.bitmap.BitmapInfo;
37 import mhr.appcore.interfaces.ImageFile;
38 import mhr.appcore.interfaces.PDBitmap;
39 import mhr.appcore.utils.NotImplementedException;
69 File root = a.getExternalFilesDir(null);
70 i.iDir =
new File(root, fname);
73 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
74 DocumentBuilder documentBuilder = null;
76 documentBuilder = documentBuilderFactory.newDocumentBuilder();
77 }
catch (ParserConfigurationException e) {
78 throw new RuntimeException(
"Unhandled exception " + e.toString());
80 i.doc = documentBuilder.newDocument();
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");
93 InputStream is = null;
95 is =
new FileInputStream(
new File(i.iDir,
"image.xml"));
96 }
catch (FileNotFoundException e) {
97 throw new RuntimeException(
"Unhandled exception " + e.toString());
100 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
101 DocumentBuilder builder = null;
103 builder = factory.newDocumentBuilder();
104 }
catch (ParserConfigurationException e) {
107 }
catch (IOException e1) {
108 throw new RuntimeException(
"Unhandled exception " + e.toString());
110 throw new RuntimeException(
"Unhandled exception " + e.toString());
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());
122 }
catch (IOException e) {
123 throw new RuntimeException(
"Unhandled exception " + e.toString());
133 public static Bitmap
loadThumb(File root,
int width,
int height) {
136 InputStream is = null;
138 Bitmap retVal = null;
141 is =
new FileInputStream(
new File(root,
"out.png"));
142 BitmapFactory.Options o =
new BitmapFactory.Options();
143 o.inJustDecodeBounds =
true;
144 BitmapFactory.decodeStream(is, null, o);
148 int wscale = o.outWidth / width;
149 int hscale = o.outHeight / height;
150 o.inJustDecodeBounds =
false;
151 o.inSampleSize = wscale > hscale ? wscale : hscale;
153 is =
new FileInputStream(
new File(root,
"out.png"));
154 retVal = BitmapFactory.decodeStream(is, null, o);
156 }
catch (FileNotFoundException e) {
157 throw new RuntimeException(
"Unhandled exception " + e.toString());
158 }
catch (IOException e) {
164 }
catch (IOException e) {
165 throw new RuntimeException(
"Unhandled exception " + e.toString());
173 File root = a.getExternalFilesDir(null);
174 File imageRoot =
new File(root, fname);
175 if (!imageRoot.exists()) {
178 File[] files = imageRoot.listFiles();
179 for (File f : files) {
194 TransformerFactory factory = TransformerFactory.newInstance();
195 Transformer transformer = null;
197 transformer = factory.newTransformer();
198 }
catch (TransformerConfigurationException e) {
199 throw new RuntimeException(
"Unhandled exception " + e.toString());
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;
211 output =
new FileOutputStream(
new File(iDir,
"image.xml"));
212 }
catch (FileNotFoundException e) {
213 throw new RuntimeException(
"Unhandled exception " + e.toString());
215 StreamResult result =
new StreamResult(output);
217 transformer.transform(domSource, result);
219 }
catch (TransformerException e) {
220 throw new RuntimeException(
"Unhandled exception " + e.toString());
221 }
catch (IOException e) {
222 throw new RuntimeException(
"Unhandled exception " + e.toString());
226 }
catch (IOException e) {
227 throw new RuntimeException(
"Unhandled exception " + e.toString());
239 BitmapFactory.Options o =
new BitmapFactory.Options();
241 InputStream is = null;
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());
251 }
catch (IOException e) {
252 throw new RuntimeException(
"Unhandled exception " + e.toString());
261 throw new IllegalArgumentException(
"Wrong bitmap");
263 Bitmap b = ((APDBitmap) bitmap).getBitmap();
264 File out =
new File(iDir, fName +
".png");
265 OutputStream os = null;
267 os =
new FileOutputStream(out);
268 b.compress(CompressFormat.PNG, 100, os);
270 }
catch (IOException e) {
271 throw new RuntimeException(
"Unhandled exception " + e.toString());
276 }
catch (IOException e) {
277 throw new RuntimeException(
"Unhandled exception " + e.toString());
285 BitmapFactory.Options o =
new BitmapFactory.Options();
287 InputStream is = null;
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());
297 }
catch (IOException e) {
298 throw new RuntimeException(
"Unhandled exception " + e.toString());
307 throw new IllegalArgumentException(
"Wrong bitmap");
309 Bitmap b = ((APDBitmap) mask).getBitmap();
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);
325 BitmapFactory.Options o =
new BitmapFactory.Options();
327 InputStream is = null;
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());
337 }
catch (IOException e) {
338 throw new RuntimeException(
"Unhandled exception " + e.toString());
347 throw new IllegalArgumentException(
"Wrong bitmap");
349 Bitmap b = ((APDBitmap) bitmap).getBitmap();
350 File out =
new File(iDir,
"out.png");
351 OutputStream os = null;
353 os =
new FileOutputStream(out);
354 b.compress(CompressFormat.PNG, 100, os);
356 }
catch (IOException e) {
357 throw new RuntimeException(
"Unhandled exception " + e.toString());
362 }
catch (IOException e) {
363 throw new RuntimeException(
"Unhandled exception " + e.toString());