Android 图片压缩/剪裁

2017-11-10  本文已影响0人  XINHAO_HAN

我这里说的是Android原生的图片压缩剪裁,如果你自己写的SO库,或者用C开源库,那我T.T,你还是用你自己的W~~~

介绍这个类

  Bitmap //我是个"变态地图"~~`
  BitmapFactory//我是个变态地图的爸爸我是它的工厂 T.T~~~

一下介绍该方法~~

   Bitmap bitmap = Bitmap.createBitmap(bit, x, y, width, height);//我是剪裁滴~

该方法的思路与之前的View视图的思路有点BUG,
View视图思路:x OR Y是在VIEW的左上角,width,height来表示它的宽高
而这个有点相似,所以这个车不好开,稍不小心就开沟里去了....

java.lang.IllegalArgumentException: x + width  must be <= bitmap.width()

看到这个错误,你迷茫了,你说MMP,我...
别急~我来给你画张图吧

这个思路是正确的,对,YOU,READ!!!你对了!


无标题.png

我们再来看看错误的 ERROR!!!

无标题.png

ERROR!!!


无标题.png

对的!!!!


无标题.png

明白了木有啊~~~

我们再来看一下压缩的技术含量

//我们来压缩一下下

//操作图像的Matrix类
  Matrix matrix = new Matrix();
//设置缩放,
matrix.postScale(scaleWidth, scaleHeight);
//开始生成新的BitMap
 Bitmap.createBitmap(orgBitmap,x, y, width, height, matrix, true);

如果又不懂Matrix的,可以看一下解释

感谢### [斌哥A1001](http://blog.csdn.net/u011057161)
他的博客:http://blog.csdn.net/u011057161/article/details/29208713
//      canvas.translate(240.0f, 400.0f);//画布移动  
//      canvas.rotate(50);//画布旋转  
//      canvas.scale(2, 2, 240.0f, 400.0f);  
          
//      matrix.postRotate(30);//绕原点旋转30度  
//      matrix.postRotate(30, width/2, heigth/2);//绕某个点旋转30度,这里选择的原点是图片的中心点  
//      matrix.postScale(2, 1);//两个参数为缩放比例。按比例缩放,宽为原来的2倍,1为正常所以高不变,但参考点事坐标原点  
//      matrix.postTranslate(240-width/2, 400-heigth/2);//参考点为坐标原点(0,0)移动到(240-width/2,400-heigth/2)  
//      matrix.postScale(2, 2, 240-width/2,400-heigth/2);//以 (240-width/2,400-heigth/2)为缩放中心  
//      matrix.postSkew(0.2f, 0.2f);//参数为x,y轴倾斜角度  
          
//      matrix.postScale(2.0f, 2.0f);  
        matrix.postTranslate(100, 100);  
//      matrix.postTranslate(100, 100);

如果是预读可以使用这个方法

 BitmapFactory.Options options = new BitmapFactory.Options();

BitmapFactory.Options//中有这个属性
options.inJustDecodeBounds = true;//代表只预读图片

 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = true;//代表只预读图片
 BitmapFactory.decodeStream(new FileInputStream(File文件),null,options );//读入图片
int x = options.outWidth//图案片的宽
int y = options.outHeight//图片的高
int scale=1;   

//图片的宽高默认100个像素
   final int IMAGE_SIZE=100;   
          //先循环一排排处理,,注意这个是耗时操作!!!1
            while(true){  
            //如果 宽除以2小于预设的值,就退出
                if(x/2<REQUIRED_SIZE || y/2<REQUIRED_SIZE)   
                    break;
                //除以一半像素   
                x/=2;   
                 //除以一半像素  
                y/=2;   
                //如果除以2,就代表倍数是2 所以就是N * 2
                scale*=2;   
            } 

           BitmapFactory.Options options2= new BitmapFactory.Options();
            //设置缩放值
            options2.inSampleSize=scale;  
            //然后读入就OK啦
           BitmapFactory.decodeStream(new FileInputStream(f), null, options2);
            //写出图片--附加的
           bitmap.compress(Bitmap.CompressFormat.PNG, 50, fileOutputStream);

再来一个例子详细解释一下Options

//创建Options 
BitmapFactory.Options options = new Options();  
//预加载图片
options.inJustDecodeBounds = true;  
//获取图片
BitmapFactory.decodeResource(getResources(), R.drawable.a2,options);  
//设置质量
/*

ALPHA_8:每个像素占用1byte内存
ARGB_4444:每个像素占用2byte内存
ARGB_8888:每个像素占用4byte内存
RGB_565:每个像素占用2byte内存

*/
options.inPreferredConfig = Bitmap.Config.ARGB_4444;  

options.inSampleSize = calculateInSampleSize(options, 200, 200);  
//关闭预加载
options.inJustDecodeBounds = false;  
//读入图片
Bitmap bitmap = BitmapFactory.decodeResource(rs, R.drawable.a2,options);  
                iv.setImageBitmap(bitmap); 



//---------------------------------计算比例方法Android文档提供,我来给你哒哒注释~~
public int calculateInSampleSize(BitmapFactory.Options op, int reqWidth,  
            int reqheight) {  
          //读入宽
        int originalWidth = op.outWidth;  
        //读入高
        int originalHeight = op.outHeight;  
        //设置默认缩放比例
        int inSampleSize = 1;  
          //如果高和宽,大于你要缩放的像素,那么就继续计算缩放值
        if (originalWidth > reqWidth || originalHeight > reqheight) {  
            //这一行来计算倍数的
            int halfWidth = originalWidth / 2;  
            //这一行来计算倍数的
            int halfHeight = originalHeight / 2;  
            //如果当前缩放的像素/计算的倍数大于要预设的像素,就继续计算缩放值
            while ((halfWidth / inSampleSize > reqWidth)  
                    &&(halfHeight / inSampleSize > reqheight)) {  
                inSampleSize *= 2;  
  
            }  
        }  
        return inSampleSize;  
    } 

希望以上文档对你了解Android的Bitmap,与BitmapFactory和 BitmapFactory.Options更近一步

上一篇 下一篇

猜你喜欢

热点阅读