2015-02-20 47 views
5

Bitmap'leri döndürerek çok fazla sorun yaşıyorum. Aşağıdaki kod çalışır, ancak Galaxy Note 2 telefonla birlikte gelen Galeri uygulamasından 4-6 kat daha yavaştır.Bitmap nasıl düzgün bir şekilde döndürülebilir?

Kodu:

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

birisi bana bu konuda rehberlik lütfen misiniz?

Galeri uygulaması gibi çalışmasını nasıl sağlayabilirim?

cevap

0

Picasso kitaplığı, bitmap veya görüntü döndürme ile çalışmak için en iyi araçtır. senin build.gradle içinde bu bağımlılığı ekle ': picasso: com.squareup.picasso 2.5.2'

referans için aşağıdaki pasajı bakınız

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; 
     } 
İlgili konular