1 package be
.nikiroo
.utils
.android
;
3 import android
.graphics
.Bitmap
;
4 import android
.graphics
.BitmapFactory
;
7 import java
.io
.FileOutputStream
;
8 import java
.io
.IOException
;
10 import be
.nikiroo
.utils
.Image
;
11 import be
.nikiroo
.utils
.ImageUtils
;
14 * This class offer some utilities based around images and uses the Android
19 public class ImageUtilsAndroid
extends ImageUtils
{
21 public void saveAsImage(Image img
, File target
, String format
)
23 FileOutputStream fos
= new FileOutputStream(target
);
25 Bitmap image
= fromImage(img
);
30 Bitmap
.CompressFormat
.valueOf(format
.toUpperCase()),
32 } catch (Exception e
) {
36 // Some formats are not reliable
38 if (!ok
&& !format
.equals("png")) {
39 ok
= image
.compress(Bitmap
.CompressFormat
.PNG
, 90, fos
);
43 throw new IOException(
44 "Cannot find a writer for this image and format: "
47 } catch (IOException e
) {
48 throw new IOException("Cannot write image to " + target
, e
);
55 * Convert the given {@link Image} into a {@link Bitmap} object.
59 * @return the {@link Image} object
63 static public Bitmap
fromImage(Image img
) throws IOException
{
64 Bitmap image
= BitmapFactory
.decodeByteArray(img
.getData(), 0,
65 img
.getData().length
);
67 int size
= img
.getData().length
;
68 String ssize
= size
+ " byte";
70 ssize
= size
+ " bytes";
81 throw new IOException(
82 "Failed to convert input to image, size was: " + ssize
);