如何使位图压缩而不改变位图大小?

我使用这种方法来压缩图像

if(bitmapObject.compress(Bitmap.CompressFormat.PNG, 100, fOut)) { ... } 

但是我得到的图像在压缩动作之前是很小的(在维度上)。

我的应用程序需要通过networking发送压缩图像 – 所以我想发送尽可能less的数据,但我必须保持图像的原始大小。

有一些其他的方法来保持一些压缩的原始位图维度?

你确定它比较小?

 Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg")); ByteArrayOutputStream out = new ByteArrayOutputStream(); original.compress(Bitmap.CompressFormat.PNG, 100, out); Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())); Log.e("Original dimensions", original.getWidth()+" "+original.getHeight()); Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight()); 

 12-07 17:43:36.333: E/Original dimensions(278): 1024 768 12-07 17:43:36.333: E/Compressed dimensions(278): 1024 768 

也许你从一个资源得到你的位图,在这种情况下,位图维度将取决于手机的屏幕密度

 Bitmap bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.img_1024x768)).getBitmap(); Log.e("Dimensions", bitmap.getWidth()+" "+bitmap.getHeight()); 12-07 17:43:38.733: E/Dimensions(278): 768 576 

如果您使用的是PNG格式,那么它将不会压缩图像,因为PNG是一种无损格式。 使用JPEG压缩您的图像,并使用0而不是100质量。

质量接受0 – 100

0 =最大压缩(最适合小图像的质量)

100 =最小压缩(最适合大图像的质量)

我这样做了:

从Singleton类获取压缩的位图

 ImageView imageView = (ImageView)findViewById(R.id.imageView); Bitmap bitmap = ImageUtils.getInstant().getCompressedBitmap("Your_Image_Path_Here"); imageView.setImageBitmap(bitmap); 

ImageUtils.java

 public class ImageUtils { public static ImageUtils mInstant; public static ImageUtils getInstant(){ if(mInstant==null){ mInstant = new ImageUtils(); } return mInstant; } public Bitmap getCompressedBitmap(String imagePath) { float maxHeight = 1920.0f; float maxWidth = 1080.0f; Bitmap scaledBitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeFile(imagePath, options); int actualHeight = options.outHeight; int actualWidth = options.outWidth; float imgRatio = (float) actualWidth / (float) actualHeight; float maxRatio = maxWidth / maxHeight; if (actualHeight > maxHeight || actualWidth > maxWidth) { if (imgRatio < maxRatio) { imgRatio = maxHeight / actualHeight; actualWidth = (int) (imgRatio * actualWidth); actualHeight = (int) maxHeight; } else if (imgRatio > maxRatio) { imgRatio = maxWidth / actualWidth; actualHeight = (int) (imgRatio * actualHeight); actualWidth = (int) maxWidth; } else { actualHeight = (int) maxHeight; actualWidth = (int) maxWidth; } } options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight); options.inJustDecodeBounds = false; options.inDither = false; options.inPurgeable = true; options.inInputShareable = true; options.inTempStorage = new byte[16 * 1024]; try { bmp = BitmapFactory.decodeFile(imagePath, options); } catch (OutOfMemoryError exception) { exception.printStackTrace(); } try { scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888); } catch (OutOfMemoryError exception) { exception.printStackTrace(); } float ratioX = actualWidth / (float) options.outWidth; float ratioY = actualHeight / (float) options.outHeight; float middleX = actualWidth / 2.0f; float middleY = actualHeight / 2.0f; Matrix scaleMatrix = new Matrix(); scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); Canvas canvas = new Canvas(scaledBitmap); canvas.setMatrix(scaleMatrix); canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG)); ExifInterface exif = null; try { exif = new ExifInterface(imagePath); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); Matrix matrix = new Matrix(); if (orientation == 6) { matrix.postRotate(90); } else if (orientation == 3) { matrix.postRotate(180); } else if (orientation == 8) { matrix.postRotate(270); } scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); } catch (IOException e) { e.printStackTrace(); } ByteArrayOutputStream out = new ByteArrayOutputStream(); scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, out); byte[] byteArray = out.toByteArray(); Bitmap updatedBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); return updatedBitmap; } private 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; } final float totalPixels = width * height; final float totalReqPixelsCap = reqWidth * reqHeight * 2; while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) { inSampleSize++; } return inSampleSize; } } 

压缩位图后尺寸相同

我怎么检查?

 Bitmap beforeBitmap = BitmapFactory.decodeFile("Your_Image_Path_Here"); Log.i("Before Compress Dimension", beforeBitmap.getWidth()+"-"+beforeBitmap.getHeight()); Bitmap afterBitmap = ImageUtils.getInstant().getCompressedBitmap("Your_Image_Path_Here"); Log.i("After Compress Dimension", afterBitmap.getWidth() + "-" + afterBitmap.getHeight()); 

输出:

 Before Compress : Dimension: 1080-1452 After Compress : Dimension: 1080-1452 

希望这会帮助你。

我想你使用这种方法来压缩位图

 BitmapFactory.Option imageOpts = new BitmapFactory.Options (); imageOpts.inSampleSize = 2; // for 1/2 the image to be loaded Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath, imageOpts), 96, 96, false);