Android Open Source - photo-share-android Image Utils






From Project

Back to project page photo-share-android.

License

The source code is released under:

Apache License

If you think the Android project photo-share-android 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.project.photoshare.utils;
// w w  w.j  a  v  a  2  s  .  c om
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;

import java.io.ByteArrayOutputStream;

/**
 * Util class for manipulating images.
 *
 * @author <a href="mailto:">TakuyaKodama</a> (kodama-t)
 * @version 1.00 14/03/28 kodama-t
 */
public class ImageUtils {

    private static final String TAG = ImageUtils.class.getSimpleName();

    // ??????????????????????
    // Because Android devices are required to support this texture size to use OpenGL.
    // ref: http://androidadvent.blogspot.com/2011/12/2.html
    public static final int MAX_GL_TEXTURE_HEIGHT = 2048;
    public static final int MAX_GL_TEXTURE_WIDTH = 2048;
    public static final float MAX_BITMAP_WIDTH = 500;
    public static final float MAX_BITMAP_HEIGHT = 500;

    public static int calcInSampleSize(int imageHeight, int imageWidth) {
        int inSampleSize = 1;
        if(imageHeight > MAX_GL_TEXTURE_HEIGHT || imageWidth > MAX_GL_TEXTURE_WIDTH) {
            if(imageHeight > imageWidth) {
                inSampleSize = (int) Math.ceil((float) imageHeight / (float) MAX_GL_TEXTURE_HEIGHT);
            } else {
                inSampleSize = (int) Math.ceil((float) imageWidth / (float) MAX_GL_TEXTURE_WIDTH);
            }
        }
        Log.i(TAG, "inSampleSize: " + inSampleSize);
        return inSampleSize;
    }

    public static byte[] getResizedByteArray(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }

        Bitmap resizedBitmap = resize(bitmap);
        Log.i(TAG, "resizedWidth: " + resizedBitmap.getWidth() + ", resizedHeight: " + resizedBitmap.getHeight());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        return baos.toByteArray();
    }

    public static Bitmap resize(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float scale = Math.min( MAX_BITMAP_WIDTH / width, MAX_BITMAP_HEIGHT / height);

        Log.i(TAG, "width: " + width + ", height: " + height);
        Log.i(TAG, "scale: " + scale);

        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);

        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }
}




Java Source Code List

com.project.photoshare.DefaultActivity.java
com.project.photoshare.DownloadPageFragment.java
com.project.photoshare.HistoryPageFragment.java
com.project.photoshare.InputPassword.java
com.project.photoshare.MainActivity.java
com.project.photoshare.MyPageFragment.java
com.project.photoshare.PasswordConfirmation.java
com.project.photoshare.SelectIsAddLater.java
com.project.photoshare.SplashActivity.java
com.project.photoshare.UploadCompleted.java
com.project.photoshare.UploadPageFragment.java
com.project.photoshare.api.ApiRequestService.java
com.project.photoshare.api.InputStreamRequest.java
com.project.photoshare.api.MultipartHurlStack.java
com.project.photoshare.api.MultipartJsonRequest.java
com.project.photoshare.listener.PostImageErrorListener.java
com.project.photoshare.listener.PostImageSuccessListener.java
com.project.photoshare.utils.BitmapDecodeLoader.java
com.project.photoshare.utils.ImageUtils.java
com.project.photoshare.utils.LogHelper.java
com.project.photoshare.utils.Utils.java
com.project.photoshare.utils.volley.LruImageCache.java
com.project.photoshare.utils.volley.VolleyHelper.java
com.project.photoshare.widget.CustomEditText.java