X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fandroid%2FImageUtilsAndroid.java;fp=src%2Fbe%2Fnikiroo%2Futils%2Fandroid%2FImageUtilsAndroid.java;h=483c44ff144ec8f705412e85c2c9b428e973999f;hb=e8aa5bf9227a0d6a6d0bb6a8bc0cc04d0f4d601a;hp=0000000000000000000000000000000000000000;hpb=805005449dacb1e7b825db63836bf100e472ddd0;p=fanfix.git diff --git a/src/be/nikiroo/utils/android/ImageUtilsAndroid.java b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java new file mode 100644 index 0000000..483c44f --- /dev/null +++ b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java @@ -0,0 +1,70 @@ +package be.nikiroo.utils.android; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import be.nikiroo.utils.Image; +import be.nikiroo.utils.ImageUtils; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * This class offer some utilities based around images and uses the Android framework. + * + * @author niki + */ +public class ImageUtilsAndroid extends ImageUtils { + @Override + public void saveAsImage(Image img, File target, String format) + throws IOException { + FileOutputStream fos = new FileOutputStream(target); + try { + Bitmap image = fromImage(img); + + boolean ok = false; + try { + ok = image.compress( + Bitmap.CompressFormat.valueOf(format.toUpperCase()), + 90, fos); + } catch (Exception e) { + ok = false; + } + + // Some formats are not reliable + // Second change: PNG + if (!ok && !format.equals("png")) { + ok = image.compress(Bitmap.CompressFormat.PNG, 90, fos); + } + + if (!ok) { + throw new IOException( + "Cannot find a writer for this image and format: " + + format); + } + } catch (IOException e) { + throw new IOException("Cannot write image to " + target, e); + } finally { + fos.close(); + } + } + + /** + * Convert the given {@link Image} into a {@link Bitmap} object. + * + * @param img + * the {@link Image} + * @return the {@link Image} object + * @throws IOException + * in case of IO error + */ + static public Bitmap fromImage(Image img) throws IOException { + Bitmap image = BitmapFactory.decodeByteArray(img.getData(), 0, + img.getData().length); + if (image == null) { + throw new IOException("Failed to convert input to image"); + } + + return image; + } +}