2015-02-20 12 views
5

Ho avuto un sacco di problemi con la rotazione dei bitmap. Il codice seguente funziona, ma è 4-6 volte più lento dell'app Gallery che viene fornito con il telefono Galaxy Note 2.Come ruotare correttamente la bitmap?

Codice:

File DirectoryFile = new File(Image_Path); 
    Bitmap bitmap = BitmapFactory.decodeFile(DirectoryFile.getAbsolutePath()); 



    String imagePATH = FileLocation + "test.jpg"; 
    final File newfile = new File(imagePATH);   

    FileOutputStream mFileOutputStream = null; 
    try {     
      mFileOutputStream = new FileOutputStream(newfile); 
     } catch (FileNotFoundException e1) { } 

     try { 
     Bitmap RotatedBitmap = RotateImage(imagePATH, bitmap); 
     RotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mFileOutputStream); 
     bitmap.recycle(); 
     RotatedBitmap.recycle(); 

     mFileOutputStream.flush(); 
     mFileOutputStream.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

private Bitmap RotateImage(String filePath, Bitmap bitmap) throws IOException { 
    ExifInterface exif = new ExifInterface(filePath); 
    int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
    int rotate = 0; 
    switch (exifOrientation) { 
    case ExifInterface.ORIENTATION_NORMAL: rotate = 0; break;    
    case ExifInterface.ORIENTATION_UNDEFINED: rotate = 90; break;        
    case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; 
    case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; 
    case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; 
    }     

    Matrix matrix = new Matrix(); 
    matrix.postRotate(rotate); 
    //matrix.setRotate(rotate); is no difference ??? 
    return bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); 
} 

Qualcuno può per favore mi guida su questo?

Come posso farlo funzionare come app Galleria?

risposta

0

La libreria Picasso è lo strumento migliore per lavorare con la rotazione di immagini o bitmap. Aggiungere questa dipendenza nella vostra build.gradle 'com.squareup.picasso: Picasso: 2.5.2'

vedere il frammento di seguito per il vostro riferimento

Picasso.with(context) 
           .load(new File(path)) 
           .resize(width, height)        // optional 
           .rotate(getAngleOfRotation(path))     // optional 
           .into(imageview); 


    private float getAngleOfRotation(String path) { 
      ExifInterface exif = null; 
      try { 
       exif = new ExifInterface(path); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       switch (exif 
         .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1)) { 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         return 180; 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         return 90; 
        case ExifInterface.ORIENTATION_ROTATE_270: 
         return 270; 
        case ExifInterface.ORIENTATION_NORMAL: 
         return 0; 
        default: 
         return 0; 
       } 
      } catch (NullPointerException e) { 
       e.printStackTrace(); 
      } 
      return 0; 
     } 
Problemi correlati