Combiner deux images dans android java


J'ai donc deux images stockées localement sur une carte SD dans Android et je veux les combiner en une seule image. C'est difficile à expliquer, donc je vais créer un lien vers une image pour un meilleur exemple de la façon dont je veux prendre les deux premières images et les combiner dans la dernière.

http://img850.imageshack.us/i/combinedh.jpg/

Author: Kling Klang, 2011-03-31

4 answers

Créez votre cible Bitmap, créez un Canvas pour elle, utilisez Canvas.drawBitmap pour blit chaque bitmap source dans votre bitmap cible.

 11
Author: EboMike, 2011-03-31 01:32:56

J'utilise généralement la fonction suivante de Jon Simon {[3] } pour combiner deux Bitmap passés en argument et obtenir un Bitmap combiné en sortie,

    public Bitmap combineImages(Bitmap c, Bitmap s) 
{ 
    Bitmap cs = null; 

    int width, height = 0; 

    if(c.getWidth() > s.getWidth()) { 
      width = c.getWidth() + s.getWidth(); 
      height = c.getHeight(); 
    } else { 
      width = s.getWidth() + s.getWidth(); 
      height = c.getHeight(); 
    } 

    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

    Canvas comboImage = new Canvas(cs); 

    comboImage.drawBitmap(c, 0f, 0f, null); 
    comboImage.drawBitmap(s, c.getWidth(), 0f, null); 

    return cs; 
}  
 11
Author: Hitesh Patel, 2013-11-22 05:33:09

La façon la plus simple de procéder serait probablement d'utiliser deux ImageViews dans un RelativeLayout. Vous pouvez positionner l'ImageViews les uns sur les autres dans la mise en page.

 2
Author: Manish Burman, 2011-03-31 03:28:16

Similaire à la réponse de Hitesh , mais avec des paramètres pour spécifier la position de l'image de premier plan:

public static Bitmap mergeBitmaps(Bitmap bitmapBg, Bitmap bitmapFg, float fgLeftPos, float fgTopPos) {

    // Calculate the size of the merged Bitmap
    int mergedImageWidth = Math.max(bitmapBg.getWidth(), bitmapFg.getWidth());
    int mergedImageHeight = Math.max(bitmapBg.getHeight(), bitmapFg.getHeight());

    // Create the return Bitmap (and Canvas to draw on)
    Bitmap mergedBitmap = Bitmap.createBitmap(mergedImageWidth, mergedImageHeight, bitmapBg.getConfig());
    Canvas mergedBitmapCanvas = new Canvas(mergedBitmap);

    // Draw the background image
    mergedBitmapCanvas.drawBitmap(bitmapBg, 0f, 0f, null);

    //Draw the foreground image
    mergedBitmapCanvas.drawBitmap(bitmapFg, fgLeftPos, fgTopPos, null);

    return mergedBitmap;

}
 0
Author: ban-geoengineering, 2018-09-08 12:06:03