Back to project page netmbuddy.
The source code is released under:
------- Default ------- Copyright (C) 2012, 2013, 2014 Younghyung Cho. <yhcting77@gmail.com> All rights reserved. This file is part of NetMBuddy This program is licensed under the FreeBSD license R...
If you think the Android project netmbuddy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/****************************************************************************** * Copyright (C) 2012, 2013, 2014/* ww w .j av a2 s . co m*/ * Younghyung Cho. <yhcting77@gmail.com> * All rights reserved. * * This file is part of NetMBuddy * * This program is licensed under the FreeBSD license * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation * are those of the authors and should not be interpreted as representing * official policies, either expressed or implied, of the FreeBSD Project. *****************************************************************************/ package free.yhc.netmbuddy.utils; import static free.yhc.netmbuddy.utils.Utils.eAssert; import java.io.ByteArrayOutputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class ImageUtils { private static final boolean DBG = false; private static final Utils.Logger P = new Utils.Logger(ImageUtils.class); /** * Decode image from file path(String) or raw data (byte[]). * @param image * Two types are supported. * String for file path / byte[] for raw image data. * @param opt * @return */ private static Bitmap decodeImageRaw(Object image, BitmapFactory.Options opt) { if (image instanceof String) { return BitmapFactory.decodeFile((String) image, opt); } else if (image instanceof byte[]) { byte[] data = (byte[]) image; return BitmapFactory.decodeByteArray(data, 0, data.length, opt); } eAssert(false); return null; } /** * Get size(width, height) of given image. * @param image * 'image file path' or 'byte[]' image data * @param out * out[0] : width of image / out[1] : height of image * @return * false if image cannot be decode. true if success */ public static boolean imageSize(Object image, int[] out) { eAssert(null != image); BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inJustDecodeBounds = true; decodeImageRaw(image, opt); if (opt.outWidth <= 0 || opt.outHeight <= 0 || null == opt.outMimeType) { return false; } out[0] = opt.outWidth; out[1] = opt.outHeight; return true; } /** * Calculate rectangle(out[]). This is got by shrinking rectangle(width,height) to * bound rectangle(boundW, boundH) with fixed ratio. * If input rectangle is included in bound, then input rectangle itself will be * returned. (we don't need to adjust) * @param boundW * width of bound rect * @param boundH * height of bound rect * @param width * width of rect to be shrunk * @param height * height of rect to be shrunk * @param out * calculated value [ out[0](width) out[1](height) ] * @return * false(not shrunk) / true(shrunk) */ public static boolean shrinkFixedRatio(int boundW, int boundH, int width, int height, int[] out) { boolean ret; // Check size of picture.. float rw = (float) boundW / (float) width; // width ratio float rh = (float) boundH / (float) height; // height ratio // check whether shrinking is needed or not. if (rw >= 1.0f && rh >= 1.0f) { // we don't need to shrink out[0] = width; out[1] = height; ret = false; } else { // shrinking is essential. float ratio = (rw > rh) ? rh : rw; // choose minimum // integer-type-casting(rounding down) guarantees that value cannot // be greater than bound!! out[0] = (int) (ratio * width); out[1] = (int) (ratio * height); ret = true; } return ret; } /** * Calculate rectangle(out[]). This is got by fitting rectangle(width,height) to * bound rectangle(boundW, boundH) with fixed ratio - preserving width-height-ratio. * If input rectangle is included in bound, then input rectangle itself will be * returned. (we don't need to adjust) * @param boundW * width of bound rect * @param boundH * height of bound rect * @param width * width of rect to be shrunk * @param height * height of rect to be shrunk * @param out * calculated value [ out[0](width) out[1](height) ] * @return * false(not shrunk) / true(shrunk) */ public static void fitFixedRatio(int boundW, int boundH, int width, int height, int[] out) { // Check size of picture.. float rw = (float) boundW / (float) width; // width ratio float rh = (float) boundH / (float) height; // height ratio float ratio = (rw > rh) ? rh : rw; // choose minimum // integer-type-casting(rounding down) guarantees that value cannot // be greater than bound!! out[0] = (int) (ratio * width); out[1] = (int) (ratio * height); } /** * Make fixed-ration-bounded-bitmap with file. * If (0 >= boundW || 0 >= boundH), original-size-bitmap is trying to be created. * @param image * image file path (absolute path) or raw data (byte[]) * @param boundW * bound width * @param boundH * bound height * @return * null if fails */ public static Bitmap decodeImage(Object image, int boundW, int boundH) { eAssert(null != image); BitmapFactory.Options opt = null; if (0 < boundW && 0 < boundH) { int[] imgsz = new int[2]; // image size : [0]=width / [1] = height if (false == imageSize(image, imgsz)) { // This is not proper image data return null; } int[] bsz = new int[2]; // adjusted bitmap size boolean bShrink = shrinkFixedRatio(boundW, boundH, imgsz[0], imgsz[1], bsz); opt = new BitmapFactory.Options(); opt.inDither = false; if (bShrink) { // To save memory we need to control sampling rate. (based on // width!) // for performance reason, we use power of 2. if (0 >= bsz[0]) return null; int sampleSize = 1; while (1 < imgsz[0] / (bsz[0] * sampleSize)) sampleSize *= 2; // shrinking based on width ratio!! // NOTE : width-based-shrinking may make 1-pixel error in height // side! // (This is not Math!! And we are using integer!!! we cannot // make it exactly!!!) opt.inScaled = true; opt.inSampleSize = sampleSize; opt.inDensity = imgsz[0] / sampleSize; opt.inTargetDensity = bsz[0]; } } return decodeImageRaw(image, opt); } /** * Compress give bitmap to JPEG formatted image data. * @param bm * @return */ public static byte[] compressBitmap(Bitmap bm) { long time = System.currentTimeMillis(); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); if (DBG) P.v("TIME: Compress Image : " + (System.currentTimeMillis() - time)); return baos.toByteArray(); } }