androidは、写真urlによってbitmapまたはdrawableを取得し、圧縮処理を行う.

12743 ワード

よく次のようなことがあります.
1.写真urlを通じてBitmapオブジェクトとDrawableオブジェクトを生成します.
2.ネットワークで取得した画像を圧縮処理する.
次はツール類を提供します.これから直接持ってきて使ってもいいです.
本文は第一コード(diyidaima.com)のオリジナルです.転載には出典を明記する必要があります.http://www.diyidaima.com/article/detail/Z8GiPpv4
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 *        
 */
public class ImageUtils {
    private int picType;//0    png  ;1  jpg  jpeg
    public static ImageUtils getIntance(){
        return new ImageUtils();
    }

    public void setPicType(int picType) {
        this.picType = picType;
    }

    /**
     *     
     */
    public Bitmap compressImage(Bitmap image) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap.CompressFormat Type=picType==0?Bitmap.CompressFormat.PNG:Bitmap.CompressFormat.JPEG;
        //image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//      ,  100     ,          baos 
        image.compress(Type, 100, baos);//      ,  100     ,          baos 
        int options = 100;
        while (baos.toByteArray().length / 1024 > 100) {  //               100kb,      
            baos.reset();//  baos   baos
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//    options%,          baos 
            options -= 10;//     10
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//       baos   ByteArrayInputStream 
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// ByteArrayInputStream      
        return bitmap;
    }

    /**
     *            (           )
     */
    public Bitmap getimage(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //      ,   options.inJustDecodeBounds   true 
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//    bm  

        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        //          800*480   ,          
        float hh = 800f;//       800f
        float ww = 480f;//       480f
        //   。         ,                  
        int be = 1;//be=1     
        if (w > h && w > ww) {//                 
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {//                 
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
        newOpts.inSampleSize = be;//      
        //      ,       options.inJustDecodeBounds   false 
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        return compressImage(bitmap);//               
    }

    /**
     *            (  Bitmap    )
     */
    public Bitmap comp(Bitmap image) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        if (baos.toByteArray().length / 1024 > 1024) {//        1M,           (BitmapFactory.decodeStream)   
            baos.reset();//  baos   baos
            Bitmap.CompressFormat Type=picType==0?Bitmap.CompressFormat.PNG:Bitmap.CompressFormat.JPEG;
            //image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//    50%,          baos 
            image.compress(Type, 50, baos);//    50%,          baos 
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //      ,   options.inJustDecodeBounds   true 
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        //          800*480   ,          
        float hh = 800f;//       800f
        float ww = 480f;//       480f
        //   。         ,                  
        int be = 1;//be=1     
        if (w > h && w > ww) {//                 
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {//                 
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
        newOpts.inSampleSize = be;//      
        //      ,       options.inJustDecodeBounds   false 
        isBm = new ByteArrayInputStream(baos.toByteArray());
        bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        return compressImage(bitmap);//               
    }

    /**      */
    public void getPicTypeByUrl(String url){
        if(url==null){
            return;
        }
        if(url.equals("")){
            return;
        }
        String[] picArray=url.split("/");
        String picStr="";
        if(picArray.length>0){
            picStr=picArray[picArray.length-1];
        }else{
            picStr=picArray[0];
        }
        if(picStr.toLowerCase().contains(".png")){
            picType=0;
        }else if(picStr.toLowerCase().contains(".jpg")||picStr.toLowerCase().contains(".jpeg")){
            picType=1;
        }
    }

    /**    url  Bitmap  
     * @param urlpath
     * @return Bitmap
     *     url      
     */
    public static Bitmap getBitMBitmap(String urlpath) {
        Bitmap map = null;
        try {
            URL url = new URL(urlpath);
            URLConnection conn = url.openConnection();
            conn.connect();
            InputStream in;
            in = conn.getInputStream();
            map = BitmapFactory.decodeStream(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }
    /**    url  Drawable  
     * @param urlpath
     * @return Bitmap
     *   url         
     */
    public static Drawable getDrawable(String urlpath){
        Drawable drawable = null;
        try {
            URL url = new URL(urlpath);
            URLConnection conn = url.openConnection();
            conn.connect();
            InputStream in;
            in = conn.getInputStream();
            drawable = Drawable.createFromStream(in, "background.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return drawable;
    }
}