Monday, 5 September 2011

Rotate bitmap to display on ImageView

SetImage function doesn't detect photo orientation. This code is to fix the problem.
At first, detect the orientation then apply this code to draw properly.

int iImageWidth = 320; // original image width
int iImageHeight = 240; // original image height

Bitmap bmOrg = null; 
BufferedInputStream bis;
try {
    bis = new BufferedInputStream(new FileInputStream(mPathImage));
    bmOrg = BitmapFactory.decodeStream(bis); 
    try {
        bis.close();
    } catch (IOException e) {
    e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(iToRotate);
Bitmap resizedBitmap = Bitmap.createBitmap(bmOrg, 0, 0, iImageWidth, iImageHeight, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
mImageView.setImageDrawable(bmd);
mImageView.setScaleType(ScaleType.CENTER_INSIDE);

No comments:

Post a Comment