图片的二次采样

本文介绍了一种图片处理方法,包括获取期望尺寸、计算最佳采样率及二次采样等步骤,有效解决图片尺寸过大导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class BitmapUtil {


    /**
     * 灵活的方法:获取期望的宽和高.
     * 图片实际的宽与高,根据默认最大大小值,得到图片实际的缩放比例
     * @param maxPrimary
     * @param maxSecondary
     * @param actualPrimary
     * @param actualSecondary
     * @return
     */
    private static int getResizedDimension(int maxPrimary, int maxSecondary,
                                           int actualPrimary, int actualSecondary) {
        // If no dominant value at all, just return the actual.
        if (maxPrimary == 0 && maxSecondary == 0) {
            return actualPrimary;
        }

        // If primary is unspecified, scale primary to match secondary's scaling
        // ratio.
        if (maxPrimary == 0) {
            double ratio = (double) maxSecondary / (double) actualSecondary;
            return (int) (actualPrimary * ratio);
        }

        if (maxSecondary == 0) {
            return maxPrimary;
        }

        double ratio = (double) actualSecondary / (double) actualPrimary;
        int resized = maxPrimary;
        if (resized * ratio > maxSecondary) {
            resized = (int) (maxSecondary / ratio);
        }
        return resized;
    }

    //计算最佳采样率的方法.actualWidth=120,actualHeight=120,desiredWidth=100,desiredHeight=100
    public static int findBestSampleSize(int actualWidth, int actualHeight,
                                  int desiredWidth, int desiredHeight) {
        //1.73
        double wr = (double) actualWidth / desiredWidth;
        //1.73
        double hr = (double) actualHeight / desiredHeight;
        //1.73
        double ratio = Math.min(wr, hr);
        float n = 1.0f;
        while ((n * 2) <= ratio) {
            n *= 2;
        }

        return (int) n;
    }

    /**
     * 图片二次采样的方法   图片的像素压缩 修改图片的宽度/高度  图片按照比例大小进行压缩
     * @param maxWidth
     * @param maxHeight
     * @param data
     * @param config
     * @return
     */
    public static Bitmap ParserBitmap(int maxWidth,int maxHeight,
                                      byte[] data,Bitmap.Config config){
        Bitmap bm=null;
        //第一次采样
        BitmapFactory.Options options=new BitmapFactory.Options();
        if(maxWidth==0 && maxHeight==0){
            //设置图片的位数  rgb_565 rgb_4444 rgb_8888
            options.inPreferredConfig=config;
            bm=BitmapFactory.decodeByteArray(data,0,data.length,options);
        }else{
            //该属性设置为true表示只加载图片的边框 不会加载图片的具体像素点  测量边界
            options.inJustDecodeBounds=true;
            //第一次加载图片 只加载图片的边框 并不加载像素点
            BitmapFactory.decodeByteArray(data,0,data.length,options);
            //图片的真实的宽度和高度 原图
            int trueWidth=options.outWidth;
            int trueHeight=options.outHeight;

            Log.i("tag","原图的宽度:"+trueWidth+",原图的高度:"+trueHeight);

            //期望的宽度和高度
            int desWidth=getResizedDimension(maxWidth,maxHeight,trueWidth,trueHeight);
            int desHeight=getResizedDimension(maxHeight,maxWidth,trueHeight,trueWidth);

//            int desWidth=300;
//            int desHeight=500;

            Log.i("tag","期望的宽度:"+desWidth+",期望的高度:"+desHeight);

            //至此第一次采样结束   开始第二次采样
            //第二次采样不仅只是加载图片的边缘
            options.inJustDecodeBounds=false;
            //inSampleSize 设置图片的采样率  获取压缩比例
            options.inSampleSize=findBestSampleSize(trueWidth,trueHeight,desWidth,desHeight);
            Log.i("tag","----options.inSampleSize-----"+ options.inSampleSize);
            //生成临时的bitmap对象
            Bitmap tempBitmap=BitmapFactory.decodeByteArray(data,0,data.length,options);
            if(tempBitmap!=null || tempBitmap.getWidth()>desWidth
                    || tempBitmap.getHeight()>desHeight){
                //创建压缩后的bitmap对象
                bm=tempBitmap.createScaledBitmap(tempBitmap,desWidth,desHeight,true);
                tempBitmap.recycle();
            }else{
                bm=tempBitmap;
            }
        }
        return bm;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值