/*
* Copyright 2012 GitHub Inc.
*
* 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.
*/
package com.github.mobile.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.util.Log;
import com.bumptech.glide.Glide;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.ExecutionException;
/**
* Image utilities
*/
public class ImageUtils {
private static final String TAG = "ImageUtils";
/**
* Get a bitmap from the image path
*
* @return bitmap or null if read fails
*/
public static Bitmap getBitmap(final String imagePath, int sampleSize) {
final Options options = new Options();
options.inDither = false;
options.inSampleSize = sampleSize;
RandomAccessFile file = null;
try {
file = new RandomAccessFile(imagePath, "r");
return BitmapFactory.decodeFileDescriptor(file.getFD(), null, options);
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
return null;
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
}
}
}
}
/**
* Get size of image
*
* @return size
*/
public static Point getSize(final String imagePath) {
final Options options = new Options();
options.inJustDecodeBounds = true;
RandomAccessFile file = null;
try {
file = new RandomAccessFile(imagePath, "r");
BitmapFactory.decodeFileDescriptor(file.getFD(), null, options);
return new Point(options.outWidth, options.outHeight);
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
return null;
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
}
}
}
}
/**
* Get bitmap with maximum height or width
*
* @return image
*/
public static Bitmap getBitmap(final String imagePath, int width, int height) {
Point size = getSize(imagePath);
if (size == null) {
return null;
}
int currWidth = size.x;
int currHeight = size.y;
int scale = 1;
while (currWidth >= width || currHeight >= height) {
currWidth /= 2;
currHeight /= 2;
scale *= 2;
}
return getBitmap(imagePath, scale);
}
/**
* Get bitmap with maximum height or width
*
* @return image
*/
public static Bitmap getBitmap(Context context, final String imagePath, int width, int height)
throws ExecutionException, InterruptedException {
return Glide.with(context).load(imagePath).asBitmap().into(width, height).get();
}
/**
* Get bitmap with maximum height or width
*
* @return image
*/
public static Bitmap getBitmap(final File image, int width, int height) {
return getBitmap(image.getAbsolutePath(), width, height);
}
}