2012-04-26 25 views
6

Estoy guardando una imagen de la cámara que estaba en modo horizontal. para que se guarde en modo horizontal y luego aplique una superposición que también está en modo horizontal. Quiero rotar esa imagen y luego guardarla. p.ej. si tengo esteGirar un mapa de bits guardado en Android

enter image description here

Quiero girar hacia la derecha 90 grados una vez que sea esto y guardarlo en la tarjeta sd:

enter image description here

¿Cómo se puede lograr esto?

Respuesta

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

modificar esta función y girar la imagen a continuación, guardarlo ... – MAC

+0

han hecho? – MAC

+0

sí, lo hizo. Muchas gracias. – prometheuspk

0

Use un Matrix.rotate (grados) y dibuje el mapa de bits en su propio lienzo usando esa matriz giratoria. Aunque no sé si tendrías que hacer una copia del mapa de bits antes de dibujar.

Utilice Bitmap.compress (...) para comprimir su mapa de bits en una salida de salida.

1

Puede usar la API Canvas para hacerlo. Tenga en cuenta que debe cambiar el ancho y el alto.

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

Marque esta

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 solución de Singhak funciona bien. En caso de que necesite ajustar el tamaño de mapa de bits resultado (tal vez por ImageView) se puede ampliar el método de la siguiente manera:

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); 
} 
Cuestiones relacionadas