2013-02-22 5 views
2
public Bitmap RotationPic(Bitmap tmpBitmap){ 
     int x = tmpBitmap.getWidth(); 
     int y = tmpBitmap.getHeight(); 
     if(x > y){ 

      Matrix matrix = new Matrix(); 
      matrix.reset(); 
      matrix.setRotate(90); 
      return Bitmap.createBitmap(tmpBitmap,0,0, x, y,matrix, true); 
     } 
     return tmpBitmap; 
    } 

문장 Bitmap.createBitmap (tmpBitmap, 0,0, x, y, matrix, true); 오류가 있습니다.회전 그림 오류 : java.lang.OutOfMemoryError

나는 네트워크 그림을 읽고 그림 크기가 약 100k ~ 300k로 큽니다.

이 문제를 해결하는 방법은 무엇입니까? 그림을 회전시키는 다른 방법이 있습니까?

답변

2

이 시도하십시오 하지만 이것을 사용하려면 pic 경로가 필요합니다. 당신은 해상도 증가 REQUIRED_SIZE 값

public static Bitmap DecodeFileToBitmap (String path) 
{ 
    try 
    {  
      File f = new File(path); 

      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

      final int REQUIRED_SIZE=500; 

      int scale=1; 
      while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE) 
       scale*=2; 

      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize=scale; 
      Bitmap bm = BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
      Bitmap bitmap = bm; 

      ExifInterface exif = null; 
      try 
      { 
       exif = new ExifInterface(path); 

      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
      Matrix m = new Matrix(); 

      if ((orientation == 3)) 
      { 
       m.postRotate(180); 
       m.postScale((float) bm.getWidth(), (float) bm.getHeight()); 
       bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true); 
       return bitmap; 
      } 
      else if (orientation == 6) 
      { 
       m.postRotate(90); 
       bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true); 
       return bitmap; 
      } 
      else if (orientation == 8) 
      { 
       m.postRotate(270); 
       bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true); 
       return bitmap; 
      } 

      return bitmap; 
      } 

      catch(Exception ex) {} 
    } 

    catch (FileNotFoundException e) {} 
    return null; 
} 
1

대체 Bitmap.createBitmap (tmpBitmap, 0,0, X, Y, 매트릭스, true)를 좋아하지 않는 경우; Bitmap.createBitmap (tmpBitmap, 0,0, y, x, matrix, true); 90도 너비 = 높이 및 높이 = 너비로 이미지를 회전 할 때