Traitement des images sous Android

Tutoriels Android

Bards of the Earth

Nouvel album de métal symphonique en ligne ! A écouter ici !

Traitement des images sous Android


Note


Le code ci-dessous permet le traitement des images sous Android, c'est-à-dire :

  • Convertir un Drawable en Bitmap (convert Drawable to Bitmap)
  • Convertir un Bitmap en Drawable (convert Bitmap to Drawable)
  • Convertir un ByteArray en Drawable (convert ByteArray to Drawable)
  • Convertir un Drawable en ByteArray (convert drawable to ByteArray)
  • Convertir un Bitmap en ByteArray (PNG) (convert Bitmap to ByteArray)
  • Convertir un ByteArray en Bitmap (convert ByteArray to Bitmap)
Vous pouvez récupérer le code, l'intégrer directement en tant que classe dans votre projet et faire appel aux fonctions présentées ci-dessus.

Code source


package packagename;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

/**
 * Traitement des images sous Android.
 * @author François http://www.francoiscolin.fr/
 *
 */
public class ImageProcessing {
    
    /**
     * Conversion ByteArray to Drawable
     */
     public static Drawable byteArrayToDrawable(Context context, byte[] bytes) {
         return bitmapToDrawable(context, byteArrayToBitmap(bytes));
     }
     
     /**
      * Conversion Drawable to ByteArray
      */
     public static byte[] drawableToByteArray(Drawable drawable) {
         return bitmapToByteArray(drawableToBitmap(drawable));
     }

     /**
      * Conversion Bitmap to ByteArray (Pour du format PNG, idem pour JPG, etc.)
      */
     public static byte[] bitmapToByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, outputStream);       
        return outputStream.toByteArray();
     }
     
     /**
      * Conversion ByteArray to Bitmap
      */
     public static Bitmap byteArrayToBitmap(byte[] bytes) {
         InputStream is = new ByteArrayInputStream(bytes);
         return BitmapFactory.decodeStream(is);
     }
     
     /**
      * Conversion Drawable to Bitmap
      */
     public static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable)drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap); 
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
     }
     
     /**
      * Conversion Bitmap to Drawable
      */
     public static Drawable bitmapToDrawable(Context context,Bitmap bitmap) {
         return new BitmapDrawable(context.getResources(), bitmap);
     }

}

Cela a été utile?