1 package be
.nikiroo
.utils
.android
;
3 import android
.graphics
.Bitmap
;
4 import android
.graphics
.Bitmap
.Config
;
5 import android
.graphics
.BitmapFactory
;
8 import java
.io
.FileOutputStream
;
9 import java
.io
.IOException
;
10 import java
.io
.InputStream
;
11 import java
.util
.stream
.Stream
;
13 import javax
.imageio
.ImageIO
;
15 import be
.nikiroo
.utils
.Image
;
16 import be
.nikiroo
.utils
.ImageUtils
;
17 import be
.nikiroo
.utils
.StringUtils
;
20 * This class offer some utilities based around images and uses the Android
25 public class ImageUtilsAndroid
extends ImageUtils
{
27 protected boolean check() {
28 // If we can get the class, it means we have access to it
34 public void saveAsImage(Image img
, File target
, String format
)
36 FileOutputStream fos
= new FileOutputStream(target
);
38 Bitmap image
= fromImage(img
);
43 Bitmap
.CompressFormat
.valueOf(format
.toUpperCase()),
45 } catch (Exception e
) {
49 // Some formats are not reliable
51 if (!ok
&& !format
.equals("png")) {
52 ok
= image
.compress(Bitmap
.CompressFormat
.PNG
, 90, fos
);
56 throw new IOException(
57 "Cannot find a writer for this image and format: "
60 } catch (IOException e
) {
61 throw new IOException("Cannot write image to " + target
, e
);
68 * Convert the given {@link Image} into a {@link Bitmap} object.
72 * @return the {@link Image} object
76 static public Bitmap
fromImage(Image img
) throws IOException
{
77 InputStream stream
= img
.newInputStream();
79 Bitmap image
= BitmapFactory
.decodeStream(stream
);
81 String ssize
= StringUtils
.formatNumber(img
.getSize());
82 throw new IOException(
83 "Failed to convert input to image, size was: " + ssize
);