96编辑器旗下产品
找图
终身
ID:0
到期时间:永久
退出登录
您的位置:首页 > 百科管理 > 详情

bitmap缩小-图片压缩的三种方式

原创:找图网 2023-05-03 05:02:09
  • android中怎么是bitmap缩小

  • /**Bitmap放大的方法*/

    private static Bitmap big(Bitmap bitmap) {

    Matrix matrix = new Matrix();

    (1.5f,1.5f); //长和宽放大缩小的比例

    Bitmap resizeBmp = (bitmap,0,0,(),(),matrix,true);

    return resizeBmp;

    }

    这里放大缩小的方法都有:

  • Bitmap改变大小

  • 用StretchBlt可以放大或缩小bitmap

    The StretchBlt function copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary. The system stretches or compresses the bitmap according to the stretching mode currently set in the destination device context.

    BOOL StretchBlt(

    HDC hdcDest, // handle to destination DC

    int nXOriginDest, // x-coord of destination upper-left corner

    int nYOriginDest, // y-coord of destination upper-left corner

    int nWidthDest, // width of destination rectangle

    int nHeightDest, // height of destination rectangle

    HDC hdcSrc, // handle to source DC

    int nXOriginSrc, // x-coord of source upper-left corner

    int nYOriginSrc, // y-coord of source upper-left corner

    int nWidthSrc, // width of source rectangle

    int nHeightSrc, // height of source rectangle

    DWORD dwRop // raster operation code

    );

  • 图片压缩的三种方式

  • 1、等比压缩,等比压缩是保持原图长宽比例的压缩,只是图片变小,展示的还是原图的所有内容(区别于第二种通过Matrix压缩,可以选取图片的一部分,类似于上传头像时,让你在图上选一块zoom的形式)。等比压缩用的的主要是,通过options缩放比例的设置,来生成缩略图:

    2、通过Matrix进行更加灵活的缩放:这种方式主要是通过构建缩放矩阵和方法来实现灵活缩放,宽和高缩放的比例可以不一致,而且通过方法创建出来的是新的位图,这个位图可以是选取原图的一部分,而不是对原图进行整体缩放!类似于上传头像时,让你在原图上扣下来一块的效果,控制非常灵活。

    3、无损压缩,无损压缩是说图片大小和清晰度看上去和原图没有什么差别,但是确实size缩小了,这里缩小的原理是牺牲了分辨率等其他直观看不到的东西,看起来和原图一样,但是一放大就立马失真了,不会和原图一样放大很多才会逐渐变得不清晰。无损压缩后的图片像素并不会减少,而Bitmap占用内存的定义就是像素点占的内存,所以以Bitmap的方式加载到内存中时,和压缩前占用的内存是同样大的,原来会oom的图片,质量压缩后同样会oom;但是,质量压缩后将流输出到文件中,文件的size会大幅度减小,所以质量压缩特别适合在 Android 端进行图片上传的时候进行图片压缩,既能保持上传后的清晰度,又能减小size。另外质量压缩不是可以无限缩小的,降低到一定程度,就算把quality设置的再小,size也不会再降低了。另外这种方式最好返回保存压缩后的图片保存的文件路径,而不要直接返回Bitmap,示例就懒得改了。

    一张图片处理过程,建议先进行等比压缩或者Matrix压缩后,再进行质量压缩,这样组合使用,不管是生成缩略图还是图片上传,效果都不错

    最新文章 更多
    最新素材 更多
    公司介绍 网站地图 图片资讯
    Copyright © 2010-2022 山东找图网络科技有限公司  鲁ICP备18007836号-3  邮箱:15653358549@163.com