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 be
.nikiroo
.utils
.Image
;
14 import be
.nikiroo
.utils
.ImageUtils
;
15 import be
.nikiroo
.utils
.StringUtils
;
18 * This class offer some utilities based around images and uses the Android
23 public class ImageUtilsAndroid
extends ImageUtils
{
25 protected boolean check() {
26 // If we can get the class, it means we have access to it
27 Config c
= Config
.ALPHA_8
;
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 InputStream stream
= img
.newInputStream();
77 Bitmap image
= BitmapFactory
.decodeStream(stream
);
80 if (img
.getSize() <= 2048) {
83 + new String(img
.getData(), "UTF-8");
84 } catch (Exception e
) {
85 extra
= ", content unavailable";
88 String ssize
= StringUtils
.formatNumber(img
.getSize());
89 throw new IOException(
90 "Failed to convert input to image, size was: " + ssize