Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Bitmap decodeStrem(File fil, Uri selectedImage,
- Context mContext) {
- Bitmap bitmap = null;
- try {
- bitmap = BitmapFactory.decodeStream(mContext.getContentResolver()
- .openInputStream(selectedImage));
- final int THUMBNAIL_SIZE = getThumbSize(bitmap);
- bitmap = Bitmap.createScaledBitmap(bitmap, THUMBNAIL_SIZE,
- THUMBNAIL_SIZE, false);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(baos
- .toByteArray()));
- return bitmap = rotateImage(bitmap, fil.getAbsolutePath());
- } catch (Exception e) {
- e.printStackTrace();
- }
- return bitmap;
- }
- public static Bitmap rotateImages(Bitmap bmp, String imageUrl) {
- if (bmp != null) {
- ExifInterface ei;
- int orientation = 0;
- try {
- ei = new ExifInterface(imageUrl);
- orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
- ExifInterface.ORIENTATION_NORMAL);
- } catch (IOException e) {
- e.printStackTrace();
- }
- int bmpWidth = bmp.getWidth();
- int bmpHeight = bmp.getHeight();
- Matrix matrix = new Matrix();
- switch (orientation) {
- case ExifInterface.ORIENTATION_UNDEFINED:
- matrix.postRotate(90);
- break;
- case ExifInterface.ORIENTATION_ROTATE_90:
- matrix.postRotate(90);
- break;
- case ExifInterface.ORIENTATION_ROTATE_180:
- matrix.postRotate(180);
- break;
- case ExifInterface.ORIENTATION_ROTATE_270:
- matrix.postRotate(270);
- break;
- default:
- break;
- }
- Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
- bmpHeight, matrix, true);
- return resizedBitmap;
- } else {
- return bmp;
- }
- }
- public static Bitmap decodeFile(File f, int sampling) {
- try {
- BitmapFactory.Options o2 = new BitmapFactory.Options();
- o2.inJustDecodeBounds = true;
- BitmapFactory.decodeStream(
- new FileInputStream(f.getAbsolutePath()), null, o2);
- o2.inSampleSize = sampling;
- o2.inTempStorage = new byte[48 * 1024];
- o2.inJustDecodeBounds = false;
- Bitmap bitmap = BitmapFactory.decodeStream(
- new FileInputStream(f.getAbsolutePath()), null, o2);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- bitmap = rotateImage(bitmap, f.getAbsolutePath());
- return bitmap;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (OutOfMemoryError e) {
- e.printStackTrace();
- }
- return null;
- }
- public static Bitmap rotateImage(Bitmap bmp, String imageUrl) {
- if (bmp != null) {
- ExifInterface ei;
- int orientation = 0;
- try {
- ei = new ExifInterface(imageUrl);
- orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
- ExifInterface.ORIENTATION_NORMAL);
- } catch (IOException e) {
- e.printStackTrace();
- }
- int bmpWidth = bmp.getWidth();
- int bmpHeight = bmp.getHeight();
- Matrix matrix = new Matrix();
- switch (orientation) {
- case ExifInterface.ORIENTATION_UNDEFINED:
- matrix.postRotate(90);
- break;
- case ExifInterface.ORIENTATION_ROTATE_90:
- matrix.postRotate(90);
- break;
- case ExifInterface.ORIENTATION_ROTATE_180:
- matrix.postRotate(180);
- break;
- case ExifInterface.ORIENTATION_ROTATE_270:
- matrix.postRotate(270);
- break;
- default:
- break;
- }
- Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
- bmpHeight, matrix, true);
- return resizedBitmap;
- } else {
- return bmp;
- }
- }
- public static int getThumbSize(Bitmap bitmap) {
- int THUMBNAIL_SIZE = 250;
- if (bitmap.getWidth() < 300) {
- THUMBNAIL_SIZE = 250;
- } else if (bitmap.getWidth() < 600) {
- THUMBNAIL_SIZE = 500;
- } else if (bitmap.getWidth() < 1000) {
- THUMBNAIL_SIZE = 750;
- } else if (bitmap.getWidth() < 2000) {
- THUMBNAIL_SIZE = 1500;
- } else if (bitmap.getWidth() < 4000) {
- THUMBNAIL_SIZE = 2000;
- } else if (bitmap.getWidth() > 4000) {
- THUMBNAIL_SIZE = 2000;
- }
- return THUMBNAIL_SIZE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement