二次采样

来源:互联网 发布:mac 照片图库 删除 编辑:程序博客网 时间:2024/04/29 22:08

         

            Bitmap bitmap=compassPic(picPath,320,320);
            img2.setImageBitmap(bitmap);

        //采样的方法                                      //第一个参数图片路径,第二个参数宽,第三个参数高

public  Bitmap compassPic(String picPath,int withpx,int heightpx){

        //1.创建options对旬
        BitmapFactory.Options options=new BitmapFactory.Options();
        //2.
        options.inJustDecodeBounds=true;
        Bitmap b=BitmapFactory.decodeFile(picPath,options);
        if(b==null){
            Log.d("zzz"," is null");
        }

        //得到原始图片的宽度与高度
        int realHeight=options.outHeight;
        int realWidth=options.outWidth;
        Log.d("zzz","原始高度:"+realHeight+" 原始宽度:"+realWidth);
        //////////////二次:计算一个压缩比例///////////////////////////////////

        int sampleSize=1;
        if(realHeight>heightpx || realWidth>withpx){

            int heighRate=realHeight/heightpx;
            int widthRate=realWidth/withpx;

            Log.d("zzz","heighRate:"+heighRate+" widthRate:"+widthRate);

            //得到压缩比例
            sampleSize=heighRate>widthRate?heighRate:widthRate;

        }

        options.inJustDecodeBounds=false;
        options.inSampleSize=sampleSize;//指定压缩比例 5
        options.inPreferredConfig= Bitmap.Config.RGB_565;//1px 占2个字节


        return BitmapFactory.decodeFile(picPath,options);
    }
原创粉丝点击