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
;
12 import be
.nikiroo
.utils
.StringUtils
;
15 * This class offer some utilities based around images and uses the Android
20 public class ImageUtilsAndroid
extends ImageUtils
{
22 public void saveAsImage(Image img
, File target
, String format
)
24 FileOutputStream fos
= new FileOutputStream(target
);
26 Bitmap image
= fromImage(img
);
31 Bitmap
.CompressFormat
.valueOf(format
.toUpperCase()),
33 } catch (Exception e
) {
37 // Some formats are not reliable
39 if (!ok
&& !format
.equals("png")) {
40 ok
= image
.compress(Bitmap
.CompressFormat
.PNG
, 90, fos
);
44 throw new IOException(
45 "Cannot find a writer for this image and format: "
48 } catch (IOException e
) {
49 throw new IOException("Cannot write image to " + target
, e
);
56 * Convert the given {@link Image} into a {@link Bitmap} object.
60 * @return the {@link Image} object
64 static public Bitmap
fromImage(Image img
) throws IOException
{
65 byte[] array
= img
.getData();
66 int size
= array
.length
;
67 Bitmap image
= BitmapFactory
.decodeByteArray(array
, 0, size
);
69 String ssize
= StringUtils
.formatNumber(size
);
70 throw new IOException(
71 "Failed to convert input to image, size was: " + ssize
);