2012-04-26 16 views
6

風景モードのカメラから画像を保存しています。そのため、ランドスケープモードで保存され、次にランドスケープがランドスケープモードにも適用されます。その画像を回転して保存したいと思います。例えば私はこのアンドロイドで保存したビットマップを回転する

enter image description here

を持っている場合、私は一度時計回りに90度回転して、この作成し、SDカードに保存したい:

これを達成する方法

enter image description here

答えて

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

この機能を変更し、それを保存して画像を回転させる...あなたは – MAC

+0

を行っていますか? – MAC

+0

はい、しました。どうもありがとうございます。 – prometheuspk

0

Matrix.rotate(degrees)を使用して、その回転行列を使用して独自のCanvasにビットマップを描画します。描画する前にビットマップのコピーを作成しなければならないかどうかはわかりません。

ビットマップを出力ストリームに圧縮するには、Bitmap.compress(...)を使用します。

1

キャンバスAPIを使用してこれを行うことができます。幅と高さを切り替える必要があることに注意してください。

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

チェックこの

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

Singhakの解決が正常に動作します。 あなたは次のようにメソッドを拡張することができます(おそらく、ImageViewのために)結果のビットマップのサイズに合わせて必要な場合:

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