当前位置: 首页 > 工具软件 > ImageResizer > 使用案例 >

imageresizer(压缩图片优化版及普通)

裴展
2023-12-01
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import com.example.imageresizer.utils.ImageResizer;
import java.io.InputStream;
import java.net.HttpURLConnection;

import java.net.URL;

//主页面

public class MainActivity extends AppCompatActivity {

    private ImageView ivIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        ivIcon = (ImageView) findViewById(R.id.ivIcon);

    }
//监听
    public void btnResizerImage(View view){

        loadImage("http://i3.s2.dpfile.com/2010-12-20/6201691_b.jpg(249x249)/thumb.jpg");

    }

//创建方法
    private void loadImage(String path){

        new AsyncTask<String,Void,Bitmap>(){

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);


                if(bitmap != null)
                    ivIcon.setImageBitmap(bitmap);
                else
                    ivIcon.setImageResource(R.mipmap.ic_launcher);


            }

            @Override
            protected Bitmap doInBackground(String... params) {

                try {
                    String path = params[0];
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);

                    int code = connection.getResponseCode();

                    if(code == 200){
                        //得到图片数据

                       InputStream is = connection.getInputStream();

//这是不优化的

                      /*   //BitmapFactory.decodeStream(is);//ARGB_888 一个像素占 4个字节  ARGB_565 一个像素占 2 个字节  Alpha Red、Green、Blue(颜色)
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        //在图片真正解析之前,不要解析图片数据,只需要告诉一下转换器,只要获取图片的像素(宽高)
                        options.inJustDecodeBounds = true;//告诉BitmapFactory转换工厂不去真正解析图片只需要拿到图片宽高就行
                        //假解析 API 有bug
                        BitmapFactory.decodeStream(is,null,options);
                        //压缩,根据图片采样率进行二次采样   100 * 100 是你所期望的具体图片尺寸
                        //options.inSampleSize = 2; //采样用来计算图片的宽高(1920 * 1080 -> 1920 / 2 1080 / 2)

                        *//*int inSampleSize = ImageResizer.caculateSampleSize(options,50,50);
                        System.out.println("采样率 : "+inSampleSize);*//*
                        options.inSampleSize = ImageResizer.caculateSampleSize(options,50,50);
                        options.inJustDecodeBounds = false;
                        //关闭之前的流
                        is.close();
                        //重新再去得到当前这张图片的字节流数据
                        is= url.openStream();
                        //开始解析图片
                        Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);
                        //关闭流
                        is.close();*/

                      // Bitmap bitmap = BitmapFactory.decodeStream(is);
                        //Bitmap bitmap = ImageResizer.decodeStreamFromNetWork(url,is,50,50);
                        return ImageResizer.decodeStreamFromNetWork(url,is,50,50);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;
            }
        }.execute(path);

    }




import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.InputStream;
import java.net.URL;


//这是优化抽出一个类

public class ImageResizer {

    /**
     *  计算图片的采样率
     * @param options
     * @param reqWidth  你所期望压缩的宽度
     * @param reqHeight  你所期望压缩的高度
     */
    public static int caculateSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){

        //得到这个图片的原始宽高
        int width = options.outWidth;
        int height = options.outHeight;

        System.out.println("宽:"+width+"高 : "+height);

        int inSampleSize = 1;

        if(width > reqWidth || height > reqHeight){

            int halfWidth = width / 2;
            int halfHeight = height / 2;

            while ((halfWidth / inSampleSize) >= reqWidth && (halfHeight / inSampleSize) >= reqHeight){

                inSampleSize *= 2;

            }

        }
        return inSampleSize;
    }


    /**
     * 图片压缩彻底封装
     * @param url
     * @param is
     * @param reqWidth
     * @param reqHeight
     * @return
     * @throws Exception
     */
    public static Bitmap decodeStreamFromNetWork(URL url, InputStream is, int reqWidth, int reqHeight) throws Exception{

        BitmapFactory.Options options = new BitmapFactory.Options();
        //假解析
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is,null,options);

        //计算采样率
        options.inSampleSize = caculateSampleSize(options,reqWidth,reqHeight);
        //开始真正解析图片
        options.inJustDecodeBounds = false;
        //关闭流,避免decodeStream(1,2,3);bug
        is.close();
        InputStream inputStream = url.openStream();
        return BitmapFactory.decodeStream(inputStream,null,options);
    }



 类似资料: