android – 如何优化像Facebook和WhatApp这样的图像?

前端之家收集整理的这篇文章主要介绍了android – 如何优化像Facebook和WhatApp这样的图像?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想优化图像文件的大小,像whatsapp和Facebook正在做的.我已经发送了5MB的图片,whatsapp,并且收到的图像大小为80KB.收到的图像与原始图像相似,但分辨率小于原始图像.

我尝试几乎所有的源代码android图像压缩可用在stackoverflow,但不适用于我.然后我碰到this link来优化图像,这是做好的工作,但仍然没有得到像whatsapp的结果.

如何在不降低图像质量的情况下实现最大图像压缩,就像whatsapp所做的那样?

用源代码回答将是非常有帮助的.

提前致谢.

解决方法

你需要解码图像(位图)

这是代码.

  1. public Bitmap decodeFile(String path) {
  2. // Decode image size
  3. int orientation;
  4. try {
  5. if (path == null) {
  6. return null ;
  7. }
  8. BitmapFactory.Options o = new BitmapFactory.Options();
  9. o.inJustDecodeBounds = true;
  10. // Find the correct scale value. It should be the power of 2.
  11. final int required_SIZE = 70;
  12. int width_tmp = o.outWidth,height_tmp = o.outHeight;
  13. int scale = 0;
  14. while (true) {
  15. if (width_tmp / 2 < required_SIZE
  16. || height_tmp / 2 < required_SIZE)
  17. break;
  18. width_tmp /= 2;
  19. height_tmp /= 2;
  20. scale++;
  21. }
  22. BitmapFactory.Options o2 = new BitmapFactory.Options();
  23. o2.inSampleSize = scale;
  24. Bitmap bm = BitmapFactory.decodeFile(path,o2);
  25. Bitmap bitmap = bm;
  26. ExifInterface exif = new ExifInterface(path);
  27. orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,1);
  28.  
  29. Log.e("orientation","" + orientation);
  30. bitmap = Bitmap.createBitmap(bm,bm.getWidth(),bm.getHeight(),null,true);
  31. ImageViewChooseImage.setImageBitmap(bitmap);
  32. bitmapfinal = bitmap;
  33. return bitmap ;
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. return null;
  37. }
  38.  
  39. }

猜你在找的Android相关文章