1 package be
.nikiroo
.utils
.android
;
3 import android
.graphics
.Bitmap
;
4 import android
.graphics
.BitmapFactory
;
5 import be
.nikiroo
.utils
.Image
;
6 import be
.nikiroo
.utils
.ImageUtils
;
9 import java
.io
.FileOutputStream
;
10 import java
.io
.IOException
;
13 * This class offer some utilities based around images and uses the Android framework.
17 public class ImageUtilsAndroid
extends ImageUtils
{
19 public void saveAsImage(Image img
, File target
, String format
)
21 FileOutputStream fos
= new FileOutputStream(target
);
23 Bitmap image
= fromImage(img
);
28 Bitmap
.CompressFormat
.valueOf(format
.toUpperCase()),
30 } catch (Exception e
) {
34 // Some formats are not reliable
36 if (!ok
&& !format
.equals("png")) {
37 ok
= image
.compress(Bitmap
.CompressFormat
.PNG
, 90, fos
);
41 throw new IOException(
42 "Cannot find a writer for this image and format: "
45 } catch (IOException e
) {
46 throw new IOException("Cannot write image to " + target
, e
);
53 * Convert the given {@link Image} into a {@link Bitmap} object.
57 * @return the {@link Image} object
61 static public Bitmap
fromImage(Image img
) throws IOException
{
62 Bitmap image
= BitmapFactory
.decodeByteArray(img
.getData(), 0,
63 img
.getData().length
);
65 throw new IOException("Failed to convert input to image");