需要Java代码来压缩Android中500 KB以下的图像

时间:2015-12-09 10:40:38

标签: java android image-processing bitmap bitmapimage

我尝试过设置BitmapFactory.Options inSampleSize,但不是缩小图像低于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);

请帮我解决这个问题。

1 个答案:

答案 0 :(得分: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;

    }