调整大小后的图像大小大于原始图像

时间:2015-03-14 13:16:37

标签: java android image bitmap

调整图像尺寸后,图像尺寸大于原始尺寸

原始图片细节

  • 尺寸1680 x 1050
  • 尺寸553 KB

调整图片大小

  • 尺寸720 x 1280
  • 尺寸1.83 MB

我使用的方法

 bitmap = Bitmap.createBitmap (bitmap, bitmapGapX, bitmapGapY,
                    deviceWidth, deviceHeight);

有没有办法获得压缩的调整大小的图像。现在iamge尺寸比原始图像大很多

2 个答案:

答案 0 :(得分:0)

请参阅Bitmap.Compress()的文档,尤其是第二个参数(质量):

  

质量提示压缩机,0-100。 0表示压缩小尺寸,100表示​​压缩以获得最高质量。某些格式,如无损的PNG,将忽略质量设置

只需使用质量较低的设置即可获得较小的文件。如果传递给Bitmap.compress()的质量设置很大,并且原始文件以低质量设置保存,则较低分辨率的已调整大小的图像最终可能会比原始文件大一些。

因此,只需尝试使用质量较低的值,例如:

bitmap.compress(Bitmap.CompressFormat.JPEG, 75 /*quality setting*/, outputStream);

尝试不同的质量设置,直到找到质量和文件大小之间的良好折衷。

答案 1 :(得分:0)

我不知道您如何调整图片大小,但您可以尝试使用此方法来缩小尺寸。在后台线程中运行此方法。

    public String compressImage(String imageUri) {

        Log.i(TAG, "qq imageUri = " + imageUri);
        String filePath = getRealPathFromURI(imageUri);
        Log.i(TAG, "qq filePath = " + filePath);

        // getAxiomData( filePath);

        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;
        float maxHeight = 816.0f;
        float maxWidth = 612.0f;
        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 = utils.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) {
            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;
        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) {
                matrix.postRotate(90);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 3) {
                matrix.postRotate(180);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 8) {
                matrix.postRotate(270);
                Log.d("EXIF", "Exif: " + orientation);
            }
            scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                    scaledBitmap.getWidth(), scaledBitmap.getHeight(),
                    matrix, true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileOutputStream fOut = null;
        ByteArrayOutputStream uploadStream = new ByteArrayOutputStream();
        String filename = getFilename();
        try {
            // Store in folder
            fOut = new FileOutputStream(filename);
            scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);

            // Store in variable to upload
            scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
                    uploadStream);
            byte[] byteArray = uploadStream.toByteArray();
            strBase64Img[imgCount] = Base64.encodeToString(byteArray,
                    Base64.DEFAULT);

            // Show in layout
            finalBtmpImg[imgCount] = scaledBitmap;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return filename;
    }