我想优化图像文件的大小,像whatsapp和Facebook正在做的.我已经发送了5MB的图片,whatsapp,并且收到的图像大小为80KB.收到的图像与原始图像相似,但分辨率小于原始图像.
我尝试几乎所有的源代码的android图像压缩可用在stackoverflow,但不适用于我.然后我碰到this link来优化图像,这是做好的工作,但仍然没有得到像whatsapp的结果.
如何在不降低图像质量的情况下实现最大图像压缩,就像whatsapp所做的那样?
用源代码回答将是非常有帮助的.
提前致谢.
解决方法
你需要解码图像(位图)
这是代码.
- public Bitmap decodeFile(String path) {
- // Decode image size
- int orientation;
- try {
- if (path == null) {
- return null ;
- }
- BitmapFactory.Options o = new BitmapFactory.Options();
- o.inJustDecodeBounds = true;
- // Find the correct scale value. It should be the power of 2.
- final int required_SIZE = 70;
- int width_tmp = o.outWidth,height_tmp = o.outHeight;
- int scale = 0;
- while (true) {
- if (width_tmp / 2 < required_SIZE
- || height_tmp / 2 < required_SIZE)
- break;
- width_tmp /= 2;
- height_tmp /= 2;
- scale++;
- }
- BitmapFactory.Options o2 = new BitmapFactory.Options();
- o2.inSampleSize = scale;
- Bitmap bm = BitmapFactory.decodeFile(path,o2);
- Bitmap bitmap = bm;
- ExifInterface exif = new ExifInterface(path);
- orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,1);
- Log.e("orientation","" + orientation);
- bitmap = Bitmap.createBitmap(bm,bm.getWidth(),bm.getHeight(),null,true);
- ImageViewChooseImage.setImageBitmap(bitmap);
- bitmapfinal = bitmap;
- return bitmap ;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }