zoukankan      html  css  js  c++  java
  • 图片压缩不失真的问题

    最近项目遇到一个问题,就是图片压缩发送后,失真度太大,对方看不清图片。

    当然,凡是压缩就会产生一定的失真度,只是度的大小问题而已,下面介绍一种,图片变小而尽量减少失真度的较好的方法(网上找的。实践之后觉得还行)。

    /**
     * 根据路径获得图片并压缩返回bitmap用于显示
     * 
     * @param imagesrc
     * @return
     */
    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
    
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize2(options, 480, 800);
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
    
        return BitmapFactory.decodeFile(filePath, options);
    }
    private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth,
                                                 int reqHeight) {
    // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
    
            if (height > reqHeight || width > reqWidth) {
    
    // Calculate ratios of height and width to requested height and
    // width
                final int heightRatio = Math.round((float) height / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);
    
    // Choose the smallest ratio as inSampleSize value, this will
    // guarantee
    // a final image with both dimensions larger than or equal to the
    // requested height and width.
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }
    
            return inSampleSize;
        }
    
    
    
    public static String save(String mCurrentPhotoPath) {
    
            String path = null ;
            File f = new File(mCurrentPhotoPath);
            try {
    
                Bitmap bm = getSmallBitmap(mCurrentPhotoPath);
    
                // 获取保存图片的目录
                File dir = new File(
                        Environment
                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                        "jiankewang");
                // 获取保存 隐患检查的图片文件夹名称
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                
                FileOutputStream fos = new FileOutputStream(new File(
                        dir, "small_" + f.getName()));
    
                path = dir + "/small_" + f.getName();
                bm.compress(Bitmap.CompressFormat.JPEG, 90, fos);
    
            } catch (Exception e) {
                
            }
    
        return  path ;
    }
  • 相关阅读:
    【华为云技术分享】区块链与数据库如何结合?
    【华为云技术分享】跟繁琐的命令行说拜拜!Gerapy分布式爬虫管理框架来袭!
    gin casbin xorm vue-admin权限认证。
    golang优秀库及介绍
    网上的element-ui-admin运行
    golang时区处理
    Let's Encrypt apache的配置
    wireshark分析自己向自己请求服务
    XORM的几个常用数据处理
    golang处理json
  • 原文地址:https://www.cnblogs.com/Jackie-zhang/p/5178301.html
Copyright © 2011-2022 走看看