Android Open Source - mclib Image Helper






From Project

Back to project page mclib.

License

The source code is released under:

Apache License, Version 2.0 FoundationProjectsPeopleGet InvolvedDownloadSupport ApacheHome ? Licenses Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITION...

If you think the Android project mclib 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 2012 Mikhail Chabanov//w ww  . ja v  a2  s .c om

   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 mc.lib.image;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import mc.lib.files.FileHelper;
import mc.lib.log.Log;
import mc.lib.screen.ScreenHelper;
import mc.lib.stream.StreamHelper;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class ImageHelper
{
    private static final String LOGTAG = ImageHelper.class.getSimpleName();

    /**
     * Read bitmap from InputStream. This method is the most inefficient, use other methods if possible.
     * 
     * @param context
     * @param is
     *            image source
     * @return Bitmap or null if fail
     */
    public static Bitmap getBitmap(Context context, InputStream is)
    {
        int[] wh = ScreenHelper.getWidthHeight(context);
        return getBitmap(context, is, wh[0], wh[1]);
    }

    /**
     * Read bitmap from InputStream. This method is the most inefficient, use other methods if possible.
     * 
     * @param context
     * @param is
     *            image source
     * @param width
     *            required image width
     * @param height
     *            required image height
     * @return Bitmap or null if fail
     */
    private static Bitmap getBitmap(Context context, InputStream is, int width, int height)
    {
        File f = null;
        FileOutputStream fos = null;
        try
        {
            f = FileHelper.createTemporaryFile(context);
            fos = new FileOutputStream(f);
            StreamHelper.copyStream(is, fos);
            StreamHelper.close(fos);
            return getBitmap(f, width, height);
        }
        catch(FileNotFoundException e)
        {
            Log.e(LOGTAG, "Cannot get bitmap", e);
        }
        finally
        {
            StreamHelper.close(fos);
            if(f != null)
            {
                f.delete();
            }
        }
        return null;
    }

    public static Bitmap getBitmapFromAssets(Context context, String filePath)
    {
        int[] wh = ScreenHelper.getWidthHeight(context);
        return getBitmapFromAssets(context, filePath, wh[0], wh[1]);
    }

    public static Bitmap getBitmapFromAssets(Context context, String filePath, int width, int height)
    {
        InputStream is = null;
        try
        {
            is = context.getAssets().open(filePath);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, options);
            StreamHelper.close(is);
            is = context.getAssets().open(filePath);
            options.inSampleSize = calculateSize(options, width, height);
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeStream(is, null, options);
        }
        catch(IOException e)
        {
            Log.e(LOGTAG, "Cannot get bitmap", e);
        }
        finally
        {
            StreamHelper.close(is);
        }
        return null;
    }

    public static Bitmap getBitmap(Context context, int resId)
    {
        int[] wh = ScreenHelper.getWidthHeight(context);
        return getBitmap(context, resId, wh[0], wh[1]);
    }

    public static Bitmap getBitmap(Context context, int resId, int width, int height)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(context.getResources(), resId, options);
        options.inSampleSize = calculateSize(options, width, height);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(context.getResources(), resId, options);
    }

    public static Bitmap getBitmap(Context context, File file)
    {
        return getBitmap(context, file.getAbsolutePath());
    }

    public static Bitmap getBitmap(File file, int width, int height)
    {
        return getBitmap(file.getAbsolutePath(), width, height);
    }

    public static Bitmap getBitmap(Context context, String filePath)
    {
        int[] wh = ScreenHelper.getWidthHeight(context);
        return getBitmap(filePath, wh[0], wh[1]);
    }

    public static Bitmap getBitmap(String filePath, int width, int height)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        options.inSampleSize = calculateSize(options, width, height);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    public static Drawable bitmapToDrawable(Bitmap src)
    {
        if(src == null)
        {
            return null;
        }

        return new BitmapDrawable(src);
    }

    private static int calculateSize(BitmapFactory.Options options, int width, int height)
    {
        final int h = options.outHeight;
        final int w = options.outWidth;
        int size = 1;

        if(h > height || w > width)
        {
            if(w > h)
            {
                size = Math.round((float)h / (float)height);
            }
            else
            {
                size = Math.round((float)w / (float)width);
            }
        }
        return size;
    }
}




Java Source Code List

mc.lib.archives.ArchivesHelper.java
mc.lib.assets.AssetsHelper.java
mc.lib.audio.AudioSource.java
mc.lib.audio.IAudioPlayer.java
mc.lib.audio.MediaAudioPlayer.java
mc.lib.files.FileHelper.java
mc.lib.files.FileReadingHelper.java
mc.lib.files.PermissionHelper.java
mc.lib.identity.IdentityHelper.java
mc.lib.image.BitmapTools.java
mc.lib.image.ImageHelper.java
mc.lib.image.InputStreamFullSkip.java
mc.lib.interfaces.OnCompleteListener.java
mc.lib.interfaces.OnProgressListener.java
mc.lib.interfaces.OnStartListener.java
mc.lib.io.KeyboardHelper.java
mc.lib.io.ToastHelper.java
mc.lib.location.LocationHelper.java
mc.lib.location.OnLocationChangeListener.java
mc.lib.log.Log.java
mc.lib.network.AsyncFileDownloader.java
mc.lib.network.AsyncNetworkHelper.java
mc.lib.network.EmailHelper.java
mc.lib.network.NetworkHelper.java
mc.lib.regexp.PopularRegexp.java
mc.lib.regexp.Validator.java
mc.lib.screen.ScreenHelper.java
mc.lib.stream.AdvancedStreamHelper.java
mc.lib.stream.StreamHelper.java
mc.lib.video.BasicVideoPlayer.java
mc.lib.video.IVideoPlayer.java
mc.lib.video.VideoViewPlayer.java