多语言展示
当前在线:1228今日阅读:23今日分享:25

图片压缩大小清晰度测试

现在手机app中对图片运用随处可见,然而我们宝贵的流量是那么贵,查看图片的时候,怎么进行压缩大小,成了需要解决的问题,下面工具可将一张2M图片压缩成几十K。
方法/步骤
1

需求: 昨天客户发来需求说app里面图片不清晰,需要修改图片清晰度。

2

原因: 之前的代码由于app的图片要上传到图片服务器,为了节省流量和减少上传时的耗时,所以图片应该尽量小,但却导致了图片不清晰。

3

解决思路:好吧,问题来了,如何找一个让客户满意,图片大小和清晰度合适的参数呢?         我想了下,写了个测试工具给客户,让客户自己选择,希望这个工具对大家都有用。         图片压缩,可以看做是分辨率和压缩率的结合;

4

具体操作:先设置分辨率大小,压缩率大小,建议压缩率30 不失真,拍照获取图片,可以通过具体设置的路径,查看原始图片和压缩过的图片大小和清晰度。看核心代码: bt_pic.setOnClickListener(new OnClickListener() {              @Override              public void onClick(View v) {                      captureImage(MainActivity.this, IMAGE_CAPTURE);//拍照              }          });

5

调用系统拍照: public void captureImage(Activity activity, int requestCode) {          Intent intent = new Intent(                  android.provider.MediaStore.ACTION_IMAGE_CAPTURE);          File photo = new File(getTempDirectoryPath(activity), TMP_IMAGE);//临时存放的原始图片,按现在手机的分辨率,一般都有1M以上大小              intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,                      Uri.fromFile(photo));          activity.startActivityForResult(intent, requestCode);      }

6

获取临时存放文件夹路径 :protected static String getTempDirectoryPath(Context ctx) {          File cache = null;          // 存放在外部存储(此目录查看原始图片)          if (Environment.getExternalStorageState().equals(                  Environment.MEDIA_MOUNTED)) {              cache = new File(Environment.getExternalStorageDirectory()                      .getAbsolutePath()                      + '/Android/data/'                      + ctx.getPackageName() + '/cache1/');          }          // 存放在内部存储          else {              cache = ctx.getCacheDir();          }          // 创建文件夹          if (!cache.exists()) {              cache.mkdirs();          }          return cache.getAbsolutePath();      }

8

获取图片并打水印 :public boolean handlePictureAndWatermarkForIntent(Activity activity,              Intent data, String file, int width, int height, String mark) {          boolean result = false;          Bitmap bitmap = handlePictureForIntent(activity, data, width, height);          if (bitmap != null) {              bitmap = watermark(bitmap, mark);              try {                  saveBitmap(file, bitmap,compression_ratio.getText().toString().trim());                  result = true;              } catch (IOException e) {                  e.printStackTrace();              } finally {                  if (bitmap != null) {                      bitmap.recycle();                      bitmap = null;                  }              }          }          return result;      }

9

获取系统拍照返回的图片: public static Bitmap handlePictureForIntent(Activity activity, Intent data,   int width, int height) {          Bitmap bitmap = null;        // 从文件中获取bitmap,临时的原始图片(几M)              String tmpFile = getTempDirectoryPath(activity) + File.separator                      + TMP_IMAGE;              bitmap = handlePictureForFile(tmpFile, width, height);                  return bitmap;      }

10

处理文件图片 :public static Bitmap handlePictureForFile(String file, int width, int height) {          Bitmap bitmap = null;          final BitmapFactory.Options options = new BitmapFactory.Options();          // 只获取图片大小          options.inJustDecodeBounds = true;          BitmapFactory.decodeFile(file, options);            // 获取bitmap          options.inJustDecodeBounds = false;          options.inSampleSize = Math.max(options.outWidth / width,                  options.outHeight / height);          bitmap = BitmapFactory.decodeFile(file, options);          if (bitmap != null && bitmap.getWidth() > bitmap.getHeight()) { // 旋转图片              bitmap = rotateBitmap(bitmap, 90);          }          return zoomBitmap(bitmap, width, height);      }

11

保存压缩图片:public static void saveBitmap(String file, Bitmap bitmap,String compression_ratio)              throws IOException {          // 压缩图片并保存至文件          File photo = new File(file);          String dir = photo.getParent();          if (dir != null) {              new File(dir).mkdirs();          }          if (!photo.exists()) {              photo.createNewFile();          }          FileOutputStream outStream = new FileOutputStream(photo);          bitmap.compress(CompressFormat.JPEG,Integer.valueOf(compression_ratio), outStream);          outStream.flush();          outStream.close();      }

12

通过以上方法可以查看:Environment.getExternalStorageDirectory()                      .getAbsolutePath()                      + '/Android/data/'                      + ctx.getPackageName() + '/cache1/'  路径下的原始图片大小清晰度。和查看Environment.getExternalStorageDirectory()+                  getResources().getString(R.string.kdumpda_filepath)                  + '/' + getDateFormatForFile()                  + '/'  压缩过后的图片大小和清晰度。demo路径:https://pan.baidu.com/s/1o8AZtV0?errno=0&errmsg=Auth%20Login%20Sucess&&bduss=&ssnerror=0

推荐信息