From 10b6023d2b40921bfc55abf414a988ca19e5e142 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 15 May 2019 21:39:59 +0200 Subject: [PATCH] add checks on ImageUtils instances --- src/be/nikiroo/utils/ImageUtils.java | 13 ++++++++++++- src/be/nikiroo/utils/android/ImageUtilsAndroid.java | 11 +++++++++++ src/be/nikiroo/utils/ui/ImageUtilsAwt.java | 8 ++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/be/nikiroo/utils/ImageUtils.java b/src/be/nikiroo/utils/ImageUtils.java index cacff8d5..e95f73e4 100644 --- a/src/be/nikiroo/utils/ImageUtils.java +++ b/src/be/nikiroo/utils/ImageUtils.java @@ -190,6 +190,14 @@ public abstract class ImageUtils { return set_flag; } + /** + * Check that the class can operate (for instance, that all the required + * libraries or frameworks are present). + * + * @return TRUE if it works + */ + abstract protected boolean check(); + /** * Create a new {@link ImageUtils}. * @@ -199,7 +207,10 @@ public abstract class ImageUtils { for (String clazz : new String[] { "be.nikiroo.utils.ui.ImageUtilsAwt", "be.nikiroo.utils.android.ImageUtilsAndroid" }) { try { - return (ImageUtils) SerialUtils.createObject(clazz); + ImageUtils obj = (ImageUtils) SerialUtils.createObject(clazz); + if (obj.check()) { + return obj; + } } catch (Exception e) { } } diff --git a/src/be/nikiroo/utils/android/ImageUtilsAndroid.java b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java index f1988624..4b3c1ea6 100644 --- a/src/be/nikiroo/utils/android/ImageUtilsAndroid.java +++ b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java @@ -7,6 +7,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import javax.imageio.ImageIO; + import be.nikiroo.utils.Image; import be.nikiroo.utils.ImageUtils; import be.nikiroo.utils.StringUtils; @@ -18,6 +20,14 @@ import be.nikiroo.utils.StringUtils; * @author niki */ public class ImageUtilsAndroid extends ImageUtils { + @Override + protected boolean check() { + // If we can get the class, it means we have access to it + @SuppressWarnings("unused") + Object test = Bitmap.class; + return true; + } + @Override public void saveAsImage(Image img, File target, String format) throws IOException { @@ -64,6 +74,7 @@ public class ImageUtilsAndroid extends ImageUtils { static public Bitmap fromImage(Image img) throws IOException { byte[] array = img.getData(); int size = array.length; + // TODO: check if we can use a stream, too Bitmap image = BitmapFactory.decodeByteArray(array, 0, size); if (image == null) { String ssize = StringUtils.formatNumber(size); diff --git a/src/be/nikiroo/utils/ui/ImageUtilsAwt.java b/src/be/nikiroo/utils/ui/ImageUtilsAwt.java index 6e9160f5..367e757a 100644 --- a/src/be/nikiroo/utils/ui/ImageUtilsAwt.java +++ b/src/be/nikiroo/utils/ui/ImageUtilsAwt.java @@ -18,6 +18,14 @@ import be.nikiroo.utils.ImageUtils; * @author niki */ public class ImageUtilsAwt extends ImageUtils { + @Override + protected boolean check() { + // If we can get the class, it means we have access to it + @SuppressWarnings("unused") + Object test = ImageIO.class; + return true; + } + @Override public void saveAsImage(Image img, File target, String format) throws IOException { -- 2.27.0