If you think the Android project khandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2012-2014 Ognyan Bankov
*//www.java2s.com
* 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.khandroid.misc;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
publicclass ImageUtils {
publicstatic Bitmap bitmapResizeToMaxXorY(Bitmap srcBitmap, int maxSizeX, int maxSizeY) {
Bitmap ret = null;
if (srcBitmap != null && maxSizeX > 0 && maxSizeY > 0) {
Float origSizeX = Float.valueOf(srcBitmap.getWidth());
Float origSizeY = Float.valueOf(srcBitmap.getHeight());
if (origSizeX > maxSizeX || origSizeY > maxSizeY) {
float origRatio = origSizeX / origSizeY;
float destRatio = maxSizeX / maxSizeY;
float targetSizeX;
float targetSizeY;
if (origRatio >= destRatio) {
if (maxSizeX >= origSizeX) {
targetSizeX = origSizeX;
} else {
targetSizeX = maxSizeX;
}
targetSizeY = targetSizeX / origRatio;
} else {
if (maxSizeY >= origSizeY) {
targetSizeY = origSizeY;
} else {
targetSizeY = maxSizeY;
}
targetSizeX = targetSizeY * origRatio;
}
ret = Bitmap.createScaledBitmap(srcBitmap,
(int) targetSizeX,
(int) targetSizeY,
false);
} else {
// original image is smaller than destination sizes so we are returning original
ret = srcBitmap;
}
}
return ret;
}
publicstatic Bitmap getBitmapFromAsset(Context context, String strName) throws IOException {
AssetManager assetManager = context.getAssets();
InputStream istr;
Bitmap bitmap = null;
istr = assetManager.open(strName);
bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
@Deprecated
publicstatic Bitmap BitmapResizeToMaxXorY(Bitmap srcBitmap, int maxSizeX, int maxSizeY) {
return bitmapResizeToMaxXorY(srcBitmap, maxSizeX, maxSizeY);
}
}