2012-04-26 21 views
6

Sto salvando un'immagine dalla fotocamera che era in modalità orizzontale. quindi viene salvato in modalità orizzontale e quindi applico una sovrapposizione anche in modalità orizzontale. Voglio ruotare l'immagine e quindi salvare. per esempio. se ho questoRuota una bitmap salvata in Android

enter image description here

voglio ruotare in senso orario di 90 gradi una volta e renderlo presente e salvarlo in sdcard:

enter image description here

come è questo per essere realizzato?

risposta

12
void rotate(float x) 
    { 
     Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.tedd); 

     int width = bitmapOrg.getWidth(); 

     int height = bitmapOrg.getHeight(); 


     int newWidth = 200; 

     int newHeight = 200; 

     // calculate the scale - in this case = 0.4f 

     float scaleWidth = ((float) newWidth)/width; 

     float scaleHeight = ((float) newHeight)/height; 

     Matrix matrix = new Matrix(); 

     matrix.postScale(scaleWidth, scaleHeight); 
     matrix.postRotate(x); 

     Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true); 

     iv.setScaleType(ScaleType.CENTER); 
     iv.setImageBitmap(resizedBitmap); 
    } 
+0

modificare questa funzione e ruotare l'immagine quindi salvarlo ... – MAC

+0

hai fatto? – MAC

+0

sì, lo ha fatto. Grazie mille. – prometheuspk

0

Utilizzare un Matrix.rotate (gradi) e disegnare la bitmap sulla propria tela utilizzando quella matrice rotante. Non so però se potresti dover fare una copia della bitmap prima di disegnare.

Utilizzare Bitmap.compress (...) per comprimere la bitmap su un outputstream.

1

È possibile utilizzare l'API Canvas per farlo. Si noti che è necessario cambiare larghezza e altezza.

final int width = landscapeBitmap.getWidth(); 
    final int height = landscapeBitmap.getHeight(); 
    Bitmap portraitBitmap = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(portraitBitmap); 
    c.rotate(90, height/2, width/2); 
    c.drawBitmap(landscapeBitmap, 0,0,null); 
    portraitBitmap.compress(CompressFormat.JPEG, 100, stream); 
2

Scegli questa

public static Bitmap rotateImage(Bitmap src, float degree) 
{ 
     // create new matrix 
     Matrix matrix = new Matrix(); 
     // setup rotation degree 
     matrix.postRotate(degree); 
     Bitmap bmp = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); 
     return bmp; 
} 
0

La soluzione di Singhak funziona bene. Nel caso in cui è necessario adattarsi alle dimensioni del risultato bitmap (forse per ImageView) è possibile espandere il metodo come segue:

public static Bitmap rotateBitmapZoom(Bitmap bmOrg, float degree, float zoom){ 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(degree); 

    float newHeight = bmOrg.getHeight() * zoom; 
    float newWidth = bmOrg.getWidth()/100 * (100.0f/bmOrg.getHeight() * newHeight); 

    return Bitmap.createBitmap(bmOrg, 0, 0, (int)newWidth, (int)newHeight, matrix, true); 
} 
Problemi correlati