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?