Android Open Source - PinterestLikeApp Image Utils






From Project

Back to project page PinterestLikeApp.

License

The source code is released under:

MIT License

If you think the Android project PinterestLikeApp 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

package com.dreamtale.pintrestlike.utils;
/*from  w w  w . ja  va2 s  .com*/
import java.io.IOException;
import java.io.InputStream;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class ImageUtils
{
    private final static String LOG_TAG = "ImageUtils";
    
    public static Bitmap decodeBitmap(Resources res, int id, float reqWidth, float reqHeight)
    {
        BitmapFactory.Options ops = new BitmapFactory.Options();
        ops.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, id, ops);
        
        ops.inSampleSize = calculateSampleSize(ops, reqWidth, reqHeight);
        ops.inJustDecodeBounds = false;
        
        return BitmapFactory.decodeResource(res, id, ops);
    }
    
    public static Bitmap decodeBitmap(InputStream is, float reqWidth)
    {
        BitmapFactory.Options ops = new BitmapFactory.Options();
        ops.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, ops);
        
        try
        {
            is.reset();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        
        ops.inSampleSize = calculateSampleSize(ops, reqWidth, reqWidth / ops.outWidth * ops.outHeight);
        ops.inSampleSize = 2;
        ops.inJustDecodeBounds = false;
        
        return BitmapFactory.decodeStream(is, null, ops);
    }
    
    public static Bitmap decodeBitmap(byte[] array, float reqWidth)
    {
        BitmapFactory.Options ops = new BitmapFactory.Options();
        ops.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(array, 0, array.length, ops);
        
//        try
//        {
//            is.reset();
//        }
//        catch (IOException e)
//        {
//            e.printStackTrace();
//        }
        
        ops.inSampleSize = calculateSampleSize(ops, reqWidth, reqWidth / ops.outWidth * ops.outHeight);
        ops.inSampleSize = 2;
        ops.inJustDecodeBounds = false;
        
        return BitmapFactory.decodeByteArray(array, 0, array.length, ops);
    }
    
    public static int calculateSampleSize(BitmapFactory.Options ops, float reqWidth, float reqHeight)
    {
        float width = ops.outWidth;
        float height = ops.outHeight;
        int sampleSize = 1;
        
        if (width > reqWidth || height > reqHeight)
        {
            int widthRatio = Math.round(width / reqWidth);
            int heightRation = Math.round(height / reqHeight);
            sampleSize = widthRatio < heightRation ? widthRatio : heightRation;
        }
        Log.d(LOG_TAG, "the sample size is " + sampleSize);
        return  sampleSize;
    }
}




Java Source Code List

com.dreamtale.pintrestlike.activity.BluetoothDeviceListActivity.java
com.dreamtale.pintrestlike.activity.DetailActivity.java
com.dreamtale.pintrestlike.activity.MainActivity.java
com.dreamtale.pintrestlike.activity.WelcomeActivity.java
com.dreamtale.pintrestlike.data.ImageAdapter.java
com.dreamtale.pintrestlike.data.ImageInfoProvider.java
com.dreamtale.pintrestlike.data.ImageInfo.java
com.dreamtale.pintrestlike.fragment.ImageDetailFragment.java
com.dreamtale.pintrestlike.parser.ImageParser.java
com.dreamtale.pintrestlike.share.BluetoothService.java
com.dreamtale.pintrestlike.utils.CacheManager.java
com.dreamtale.pintrestlike.utils.ImageDownloader.java
com.dreamtale.pintrestlike.utils.ImageUtils.java
com.dreamtale.pintrestlike.utils.IntentConstant.java
com.dreamtale.pintrestlike.utils.UIConfig.java
com.dreamtale.pintrestlike.widget.BluetoothDeviceListDialog.java
com.dreamtale.pintrestlike.widget.ItemView.java
com.dreamtale.pintrestlike.widget.PinterestScrollView.java
com.dreamtale.pintrestlike.widget.PintrestGridView.java