2015-12-09 21 views
-4

我尝试过设置BitmapFactory.OptionsinSampleSize,但它不会将图像缩小到500 KB以下。需要Java代码才能在Android中压缩低于500 KB的图像

图片可能是任何大小,必须压缩,因为我将它存储在数据库中,图片低于500 KB是我的主要要求。以下是我的代码:

     int inSampleSize = 1; 

        Bitmap bitmapImage;//= BitmapFactory.decodeFile(imageUri, options); 
        do { 
         BitmapFactory.Options options = new BitmapFactory.Options(); 
         options.inSampleSize = inSampleSize; 
         options.inScaled = true; 
         bitmapImage = BitmapFactory.decodeFile(imageUri, options); 
         inSampleSize++; 
        } while (bitmapImage != null && bitmapImage.getByteCount() > 500000); 

请帮我解决这个问题。

+3

1)请提供您尝试过的一些代码。 2)请做一些研究 3)你永远不能保证你会压缩那么多 – apmartin1991

+0

在数据库上保存图像不是最好的解决方案。您可以将图像保存在内部存储器上,并将图像的路径保存到数据库中作为字符串。 –

+0

是的,这是一个不错的选择。但我需要将图像字节发送到服务器,并且服务器接受大小小于500 KB的图像。 –

回答

1

试试这个

public Bitmap compressImage(String imageUri) { 

     String filePath = getRealPathFromURI(imageUri); 
     Bitmap scaledBitmap = null; 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true;      
     Bitmap bmp = BitmapFactory.decodeFile(filePath,options); 

     int actualHeight = options.outHeight; 
     int actualWidth = options.outWidth; 


     ExifInterface exi; 
     int orien=0; 
     try { 
      exi = new ExifInterface(filePath); 
      orien = exi.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 



     float maxHeight = ScreenHeight; 
     float maxWidth = ScreenWidth; 


     float imgRatio = actualWidth/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(filePath,options); 
     } 
     catch(OutOfMemoryError exception){ 
     } 
     try{ 
      scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888); 
     } 
     catch(OutOfMemoryError exception){ 
     } 

     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)); 



     boolean isPorait=true; 
     if(((Activity)context).getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){ 
      isPorait=false; 
     } 




     ExifInterface exif; 
     try { 
      exif = new ExifInterface(filePath); 

      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); 
      Log.d("EXIF", "Exif: " + orientation); 
      Matrix matrix = new Matrix(); 
      if (orientation == 6) { 
       if(isPorait) 
       matrix.postRotate(90); 
       Log.d("EXIF", "Exif: " + orientation); 
      } else if (orientation == 3) { 
       if(!isPorait) 
       matrix.postRotate(180); 
       else 
       matrix.postRotate(-90); 
       Log.d("EXIF", "Exif: " + orientation); 
      } else if (orientation == 8) { 
       if(isPorait) 
       matrix.postRotate(270); 
       else 
        matrix.postRotate(180); 
       Log.d("EXIF", "Exif: " + orientation); 
      } 


      scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); 
     } catch (IOException e) { 
     } 


     bmp.recycle(); 

     return scaledBitmap; 

    } 
+0

我已经试过上面的代码,但它没有压缩图像的大小。我需要减小文件大小以及高度和宽度 –

+0

我正在使用它。它将图像大小缩小到100 kb以下。 –

+0

你是如何计算ScreenHeight和ScreenWidth的,因为图像可以是肖像或风景。 –