图片压缩策略(外带矫正图片方向)

1,乾言

如果图片太大,上传不仅耗时,而且体验不好。即使加了loading效果,那还是挺耗流量的。so,果断要压缩图片再上传,android客户端,尤其要注意。

2,图片上传

压缩原图后保存在sdcard临时目录,如果压缩后的图片存在,就上传压缩的图片,没有压缩成功(不存在),就直接上原图吧。

// 压缩图片并上传
 private  void uploadFileInThreadByOkHttp(final Activity context, final String actionUrl, final File tempPic) {
    final String pic_path = tempPic.getPath();
    String targetPath = FileUtils.getThumbDir()+"compressPic.jpg";
    //调用压缩图片的方法,返回压缩后的图片path
    final String compressImage = PictureUtil.compressImage(pic_path, targetPath, 30);
    final File compressedPic = new File(compressImage);
    if (compressedPic.exists()) {
      LogUtils.debug(TAG,"图片压缩上传");
      uploadFileByOkHTTP(context, actionUrl, compressedPic);
   }else{//直接上传
        uploadFileByOkHTTP(context, actionUrl, tempPic);
   }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3,压缩图片的关键代码

 public static String compressImage(String filePath, String targetPath, int quality)  {
          Bitmap bm = getSmallBitmap(filePath);//获取一定尺寸的图片
        int degree = readPictureDegree(filePath);//获取相片拍摄角度
        if(degree!=0){//旋转照片角度,防止头像横着显示
            bm=rotateBitmap(bm,degree);
        }
        File outputFile=new File(targetPath);
        try {
            if (!outputFile.exists()) {
                outputFile.getParentFile().mkdirs();
                //outputFile.createNewFile();
            }else{
                outputFile.delete();
            }
            FileOutputStream out = new FileOutputStream(outputFile);
            bm.compress(Bitmap.CompressFormat.JPEG, quality, out);
        }catch (Exception e){}
        return outputFile.getPath();
    }

    /**
     * 根据路径获得图片信息并按比例压缩,返回bitmap
     */
    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;//只解析图片边沿,获取宽高
        BitmapFactory.decodeFile(filePath, options);
        // 计算缩放比
        options.inSampleSize = calculateInSampleSize(options, 480, 800);
        // 完整解析图片返回bitmap
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }


    /**
     * 获取照片角度
     * @param path
     * @return
     */
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    /**
     * 旋转照片
     * @param bitmap
     * @param degress
     * @return
     */
    public static Bitmap rotateBitmap(Bitmap bitmap,int degress) {
        if (bitmap != null) {
            Matrix m = new Matrix();
            m.postRotate(degress);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), m, true);
            return bitmap;
        }
        return bitmap;
    }
public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

4,上传图片

使用okhttp,如果不太熟悉okhttp的话,可以看我的另一博文: 
Android网络请求:OkHttp实战 
本文上传头像,封装uploadFile方法如下:


    /**
     * 上传文件
     * @param url  接口地址
     * @param file 上传的文件
     * @param mediaType  资源mediaType类型:比如 MediaType.parse("image/png");
     * @param responseCallback 回调方法,在子线程,更新UI要post到主线程
     * @return
     */
    public boolean uploadFile(String url, File file, MediaType mediaType, Callback responseCallback) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MediaType.parse("multipart/form-data"));
        if (!file.exists()|| TextUtils.isEmpty(url)){
            return false;
        }
        //addFormDataPart视项目自身的情况而定
               //builder.addFormDataPart("description","2.jpg");
        builder.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file));
        //构建请求体
        RequestBody requestBody = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        enqueue(request,responseCallback);
        return true;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FFmpeg是一个开源的跨平台音视频处理工具,它提供了丰富的功能和命令行选项,可以用于音视频的编解码、转码、剪辑、合并等操作。在FFmpeg中,图片压缩是其中一个常见的功能之一。 要使用FFmpeg进行图片压缩,可以通过以下步骤进行操作: 1. 安装FFmpeg:首先需要在计算机上安装FFmpeg。可以从FFmpeg官方网站(https://ffmpeg.org/)下载适合自己操作系统的版本,并按照安装指南进行安装。 2. 执行压缩命令:打开命令行终端,进入到FFmpeg的安装目录下。然后使用以下命令进行图片压缩: ``` ffmpeg -i input.jpg -vf "scale=w:h" output.jpg ``` 其中,`input.jpg`是待压缩的图片文件名,`output.jpg`是压缩后的图片文件名。`w`和`h`分别表示压缩后的图片宽度和高度,可以根据需要进行调整。 例如,要将图片压缩为宽度为800像素,高度按比例缩放的图片,可以使用以下命令: ``` ffmpeg -i input.jpg -vf "scale=800:-1" output.jpg ``` 这样就会生成一个宽度为800像素,高度按比例缩放的压缩后的图片。 3. 等待压缩完成:执行压缩命令后,FFmpeg会开始处理图片压缩操作。等待命令执行完毕,即可得到压缩后的图片文件。 需要注意的是,FFmpeg支持多种图片格式,可以根据需要选择输出的图片格式。另外,还可以通过其他参数和选项来进一步调整压缩质量、调整图片尺寸等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值