2012-07-31 20 views
10

Quiero combinar dos mapas de bits, aquí está mi códigoCombinar dos mapas de bits en Android

// Camera arg conversion to Bitmap 
        Bitmap cameraBitmap = BitmapFactory.decodeByteArray(arg0, 0, 
          arg0.length); 
        Bitmap back = Bitmap.createBitmap(cameraBitmap.getWidth(), 
         cameraBitmap.getHeight(), Bitmap.Config.ARGB_8888); 
        Canvas cam = new Canvas(back); 
        cam.drawBitmap(cameraBitmap, matrix, null); 


        // FrameLayout to Bitmap 
        FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame); 
        Bitmap foreground = Bitmap.createBitmap(mainLayout.getWidth(), 
          mainLayout.getHeight(), Bitmap.Config.ARGB_8888); 
        Canvas c = new Canvas(foreground); 
        mainLayout.draw(c); 

        Bitmap cs = null; 
        cs = Bitmap.createBitmap(foreground.getWidth(), cameraBitmap.getHeight(), Bitmap.Config.ARGB_8888); 

        Canvas comboImage = new Canvas(cs); 
        comboImage.drawBitmap(cameraBitmap, 0f, 0f, null); 
        comboImage.drawBitmap(foreground, 0f, cameraBitmap.getHeight(), null); 

        FileOutputStream fos = null; 
        try { 
         fos = new FileOutputStream(file); 
         if (fos != null) { 
          cs.compress(Bitmap.CompressFormat.PNG, 90, fos); 
          fos.close(); 
         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

La imagen de la cámara se convierta en el fondo, y el primer plano como la imagen de arriba. Lo intenté desde Combining 2 Images in Android using Canvas pero no me ayudó. Alguna idea.? Gracias

Respuesta

29

de su ejemplo, que se olvidó de agregar las siguientes líneas:

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

En el ejemplo anterior no se dibuja su imagen en el lienzo, y ese es el problema. Puedes pensar que tu lienzo es tu cuaderno de bocetos. Por ahora no pintaste nada, y te preguntas a ti mismo, así no puedo ver ningún color.

Por lo tanto, mi consejo, crear en primer lugar los dos mapas de bits y, a continuación, haga lo siguiente que:

c.drawBitmap(cameraBitmap, top point, left point, null); 
c.drawBitmap(foreground, top point, left point, null); 

También puede hacer esto en primer lugar crear los objetos dibujable de los mapas de bits, como en el siguiente código :

Drawable cameraBitmap = BitmapDrawable(cameraBitmap); 
Drawable foreground= BitmapDrawable(foreground); 

Entonces, cuando usted tiene los objetos dibujable, puede establecer arreglan sus límites, y de esa manera se establece en hacer que desea mostrar esa imagen.

cameraBitmap.setBounds(left, top, right, bottom); 
foreground.setBounds(left, top, right, bottom); 

y, finalmente, llegar a esa en el lienzo:

cameraBitmap.draw(canvas); 
foreground.draw(canvas); 

EDIT:

Este es un ejemplo, usar esto para entender su aplicación:

Bitmap bitmap = null; 
    try { 

     bitmap = Bitmap.createBitmap(500, 500, Config.ARGB_8888); 
     Canvas c = new Canvas(bitmap); 
     Resources res = getResources(); 


     Bitmap bitmap1 = BitmapFactory.decodeResource(res, R.drawable.test1); //blue 

     Bitmap bitmap2 = BitmapFactory.decodeResource(res, R.drawable.test2); //green 
     Drawable drawable1 = new BitmapDrawable(bitmap1); 
     Drawable drawable2 = new BitmapDrawable(bitmap2); 


     drawable1.setBounds(100, 100, 400, 400); 
     drawable2.setBounds(150, 150, 350, 350); 
     drawable1.draw(c); 
     drawable2.draw(c); 


    } catch (Exception e) { 
    } 
    return bitmap; 

Este es lo que obtengo del código de arriba:

enter image description here

+0

he intentado su ans, revise mi pregunta editada. Pero no se puede ver ninguna imagen de fusión, ¿me puede ayudar más? – Numair

+0

@Numair ¿Aún necesita ayuda? –

+0

Sí @Ofir A. Todavía estoy teniendo el problema :( – Numair

0

Tenga en cuenta que el BitmapDrawable (mapa de bits) ya no se utiliza. Kinldy cheque this para la alternativa.

BitmapDrawable(Bitmap bitmap) Este constructor estaba en desuso en el nivel de API 4. Use BitmapDrawable(Resources, Bitmap) para asegurarse de que el dibujo haya establecido correctamente su densidad objetivo.

0
imagen de agua

Redimensionar mismo tamaño que la imagen original,

Uri bmpUri1 = getLocalBitmapUri(ivImage); 
Uri bmpUri2 = getLocalBitmapUri(watermark_imageview); 

try { 
    bm1 = BitmapFactory.decodeStream(
      getContentResolver().openInputStream(bmpUri1)); 
    bm2 = BitmapFactory.decodeStream(
      getContentResolver().openInputStream(bmpUri2)); 

    Bitmap bmOverlay = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), bm1.getConfig()); 
    bm2 = Bitmap.createScaledBitmap(bm2, bm1.getWidth(), bm1.getHeight(), 
      true); 

    Canvas canvas = new Canvas(bmOverlay); 
    canvas.drawBitmap(bm1, 0,0, null); 
    canvas.drawBitmap(bm2, 0,0, null); 
    watermarkimage.setVisibility(View.GONE); 
    im =new ImageView(ImageClick.this); 
    im.setImageBitmap(bmOverlay); 
    bmpUri = getLocalBitmapUri(im); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

private Uri getLocalBitmapUri(ImageView imageView) { 
    Drawable drawable = imageView.getDrawable(); 
    Bitmap bmp = null; 
    if (drawable instanceof BitmapDrawable){ 
     bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
    } else { 
     return null; 
    } 
    // Store image to default external storage directory 
    Uri bmpUri = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
     FileOutputStream out = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
     out.close(); 
     bmpUri = Uri.fromFile(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return bmpUri; 
} 
0

la fusión de dos de mapa de bits verticalmente cuando uno es grande y la segunda es pequeño
seguir este método

public Bitmap finalcombieimage(Bitmap c, Bitmap s) { 
    Bitmap cs = null; 
    DisplayMetrics metrics = getBaseContext().getResources().getDisplayMetrics(); 
    int width = metrics.widthPixels; 
    int height = metrics.heightPixels; 
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    Canvas comboImage = new Canvas(cs); 
    Rect dest1 = new Rect(0, 0, width, height); // left,top,right,bottom 
    comboImage.drawBitmap(c, null, dest1, null); 
    Rect dest2 = new Rect(0, height-400/2, width, height); 
    comboImage.drawBitmap(s, null, dest2, null); 
    return cs; 
} 

enter image description here