Java tutorial
//package com.java2s; /* * Copyright (C) 2016 Jorge Ruesga * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.graphics.Matrix; import android.media.ExifInterface; import java.io.File; import java.io.IOException; public class Main { public static Bitmap decodeBitmap(File file, int dstWidth, int dstHeight) { // First decode with inJustDecodeBounds=true to check dimensions final Options options = new Options(); options.inScaled = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getAbsolutePath(), options); // Decode the bitmap with inSampleSize set options.inSampleSize = calculateBitmapRatio(options, dstWidth, dstHeight); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options); if (bitmap == null) { return null; } // Test if the bitmap has exif format, and decode properly Bitmap out = decodeExifBitmap(file, bitmap); if (!out.equals(bitmap)) { bitmap.recycle(); } return out; } private static int calculateBitmapRatio(Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } private static Bitmap decodeExifBitmap(File file, Bitmap src) { try { // Try to load the bitmap as a bitmap file ExifInterface exif = new ExifInterface(file.getAbsolutePath()); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); if (orientation == ExifInterface.ORIENTATION_UNDEFINED || orientation == ExifInterface.ORIENTATION_NORMAL) { return src; } Matrix matrix = new Matrix(); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { matrix.postRotate(90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { matrix.postRotate(180); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { matrix.postRotate(270); } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) { matrix.setScale(-1, 1); matrix.postTranslate(src.getWidth(), 0); } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) { matrix.setScale(1, -1); matrix.postTranslate(0, src.getHeight()); } // Rotate the bitmap return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); } catch (IOException e) { // Ignore } return src; } }