android - how to automatically turn the picture in the right form (library cropper )? -
i use library cropper here in project. works cool, if photo made in portrait mode, flips , displayed in landscape mode.
here original photo - original opened program - program
there method in library cropimageview.rotateimage(90); can add button rererses picture when pressed. can make run once loaded in right mode?
imagecrop = (cropimageview) v.findviewbyid(r.id.imagecrop); string picturepath = getarguments().getstring("image"); crudeimage = bitmapfactory.decodefile(picturepath); imagecrop.setimagebitmap(crudeimage);
you need decode original image bitmap in orientation. detect image orientation using exifinterface, use matrix rotate bitmap image's original orientation:
crudeimage = bitmapfactory.decodefile(picturepath); float rotationdegrees = getrotationdegreestooriginal(picturepath); if (rotationdegrees != 0f) { crudeimage = rotateimage(crudeimage, rotationdegrees); } /** * rotate bitmap given degrees * @param bitmap original bitmap * @param degrees rotation degrees * @return rotated bitmap */ public static bitmap rotateimage(bitmap bitmap, float degrees) { matrix matrix = new matrix(); matrix.postrotate(degrees); return bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), bitmap.getheight(), matrix, true); } /** * rotation degrees need applied rotate image original orientation * @param filename full path image * @return rotation degrees, or 0 if file not found */ public static float getrotationdegreestooriginal(string filename) { final exifinterface exif; try { exif = new exifinterface(filename); } catch (ioexception e) { return 0f; } // detect original orientation rotate int orientation = exif.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_normal); switch (orientation) { case exifinterface.orientation_rotate_180: return 180f; case exifinterface.orientation_rotate_90: return 90f; case exifinterface.orientation_rotate_270: return 270f; default: return 0f; } }
Comments
Post a Comment