Android Open Source - CommonLibs Image Utils






From Project

Back to project page CommonLibs.

License

The source code is released under:

Apache License

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

/**
 * <p>Title: Photo.java</p>/*  www . j  a va2 s.c  om*/
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2013</p>
 * <p>Company: </p>
 * @author caisenchuan
 * @date 2013-9-8
 * @version 1.0
 */
package com.alex.common.utils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.alex.common.AppConfig;
import com.alex.common.utils.FileUtils.PathType;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;

/**
 * ????????????
 * @author caisenchuan
 *
 */
public class ImageUtils {
    /*--------------------------
     * ?????
     *-------------------------*/
    
    /*--------------------------
     * ???
     *-------------------------*/
    private static final String TAG = ImageUtils.class.getSimpleName();
    
    /**?????????*/
    private static final String PIC_FILE_EXT = ".jpeg";
    
    /*--------------------------
     * ????????
     *-------------------------*/

    /*--------------------------
     * public??
     *-------------------------*/
    /**
     * ?????????
     * @param activity ??????????Result?Activity???????Activity???onActivityResult??
     * @param requestCode ????Activity????requestCode??onActivityResult?????????
     * @return ?????????????????????????null
     * */
    public static String takePhoto(Activity activity, int requestCode) {
        String filename = null;
        
        if(activity != null) {
            Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
            
            filename = FileUtils.getUniqPath(PathType.PHOTO, PIC_FILE_EXT);
            File img = new File(filename);
            Uri imgUri = Uri.fromFile(img);
            KLog.d(TAG, "img uri : " + imgUri);
            i.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
            
            activity.startActivityForResult(i, requestCode);
        }
        
        return filename;
    }
    
    /**
     * ???????
     * @param activity ??????????Result?Activity???????Activity???onActivityResult??
     * @param requestCode ????Activity????requestCode??onActivityResult?????????
     */
    public static void selectPhotoFromAlbum(Activity activity, int requestCode) {
        if(activity != null) {
            Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
            getAlbum.setType("image/*");
            activity.startActivityForResult(getAlbum, requestCode);
        } else {
            KLog.w(TAG, "activity == null!");
        }
    }

    /**
     * ???????SD????
     * @param type ??????????????????????
     * @param bm ??bitmap
     * @return ?????????????????????null
     * */
    public static String savePicToSD(PathType type, Bitmap bm) {
        String filename = null;

        // ????
        if (!FileUtils.isSDMount()) {
            return filename;
        }

        // ???????
        filename = FileUtils.getUniqPath(type, PIC_FILE_EXT);
        File mPhoto = new File(filename);

        // ?????
        try {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(mPhoto));

            bm.compress(Bitmap.CompressFormat.JPEG, AppConfig.JPEG_QUALITY, bos);

            bos.flush();

            bos.close();
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            filename = null;
        } catch (IOException e) {
            e.printStackTrace();
            filename = null;
        }

        return filename;
    }

    /**
     * ???????????????
     * @param filePath ????
     * @param width ????
     * @param height ????
     * @return
     */
    public static Bitmap createNewBitmapAndCompressByFile(String filePath, int width, int height) {
        int offset = 100;
        File file = new File(filePath);
        long fileSize = file.length();
        if (200 * 1024 < fileSize && fileSize <= 1024 * 1024) {
            offset = 90;
        } else if (1024 * 1024 < fileSize) {
            offset = 85;
        }

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; // ?true?????????????????????bitmap?null
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inDither = false;
        /**
         * ??????
         * TODO ???????
         */
        BitmapFactory.decodeFile(filePath, options);

        int bmpheight = options.outHeight;
        int bmpWidth = options.outWidth;
        int inSampleSize = bmpheight / height > bmpWidth / width ? 
                           bmpheight / height : bmpWidth / width;
        //if(bmpheight / wh[1] < bmpWidth / wh[0]) 
        //    inSampleSize = inSampleSize * 2 / 3;
        //TODO ?????????????????????????2/3
        if (inSampleSize > 1) {
            options.inSampleSize = inSampleSize;// ??????
        }
        options.inJustDecodeBounds = false;

        InputStream is = null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            return null;
        }
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(is, null, options);
        } catch (OutOfMemoryError e) {
            System.gc();
            bitmap = null;
        }
        if (offset == 100) {
            return bitmap;// ??????
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, offset, baos);
        byte[] buffer = baos.toByteArray();
        options = null;
        if (buffer.length >= fileSize) {
            return bitmap;
        }
        
        return BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
    }

    /**
     * ???????
     * @param v ??????View
     * @return ??????
     */
    public static String screenShot(View v) {
        String ret = "";
        if(v != null) {
            String fname = FileUtils.getUniqPath(PathType.SHARE, ".png");
            
            View view = v.getRootView();
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            
            Bitmap bitmap = view.getDrawingCache();
            if(bitmap != null) {
                KLog.d(TAG, "bitmap got!");
                try {
                    FileOutputStream out = new FileOutputStream(fname);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                    ret = fname;
                } catch(Exception e) {
                    KLog.e(TAG, "Exception", e);
                }
            } else {
                KLog.d(TAG, "bitmap is NULL!");
            }
        }
        return ret;
    }
    /*--------------------------
     * private??
     *-------------------------*/
}




Java Source Code List

com.alex.common.AppConfig.java
com.alex.common.AppControl.java
com.alex.common.Err.java
com.alex.common.OnHttpRequestReturnListener.java
com.alex.common.activities.BaseActivity.java
com.alex.common.activities.ImageLoadActivity.java
com.alex.common.activities.WebViewActivity.java
com.alex.common.apis.HttpApi.java
com.alex.common.exception.RetErrorException.java
com.alex.common.utils.BackgroundHandler.java
com.alex.common.utils.BaiduMapUtils.java
com.alex.common.utils.DateTimeUtils.java
com.alex.common.utils.DeviceUtils.java
com.alex.common.utils.DialogUtils.java
com.alex.common.utils.FileUtils.java
com.alex.common.utils.ImageUtils.java
com.alex.common.utils.KLog.java
com.alex.common.utils.Misc.java
com.alex.common.utils.NetworkUtils.java
com.alex.common.utils.PrefUtils.java
com.alex.common.utils.ShareUtils.java
com.alex.common.utils.StringUtils.java
com.alex.common.utils.ThreadUtils.java
com.alex.common.utils.ToastUtils.java
com.alex.common.views.ZoomImageView.java