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 javax
.imageio
.ImageIO
;
12 import be
.nikiroo
.utils
.Image
;
13 import be
.nikiroo
.utils
.ImageUtils
;
14 import be
.nikiroo
.utils
.StringUtils
;
17 * This class offer some utilities based around images and uses the Android
22 public class ImageUtilsAndroid
extends ImageUtils
{
24 protected boolean check() {
25 // If we can get the class, it means we have access to it
26 @SuppressWarnings("unused")
27 Object test
= Bitmap
.class;
32 public void saveAsImage(Image img
, File target
, String format
)
34 FileOutputStream fos
= new FileOutputStream(target
);
36 Bitmap image
= fromImage(img
);
41 Bitmap
.CompressFormat
.valueOf(format
.toUpperCase()),
43 } catch (Exception e
) {
47 // Some formats are not reliable
49 if (!ok
&& !format
.equals("png")) {
50 ok
= image
.compress(Bitmap
.CompressFormat
.PNG
, 90, fos
);
54 throw new IOException(
55 "Cannot find a writer for this image and format: "
58 } catch (IOException e
) {
59 throw new IOException("Cannot write image to " + target
, e
);
66 * Convert the given {@link Image} into a {@link Bitmap} object.
70 * @return the {@link Image} object
74 static public Bitmap
fromImage(Image img
) throws IOException
{
75 byte[] array
= img
.getData();
76 int size
= array
.length
;
77 // TODO: check if we can use a stream, too
78 Bitmap image
= BitmapFactory
.decodeByteArray(array
, 0, size
);
80 String ssize
= StringUtils
.formatNumber(size
);
81 throw new IOException(
82 "Failed to convert input to image, size was: " + ssize
);