压缩位图不会压缩图像的高度

时间:2020-03-19 10:19:02

标签: java android

private Bitmap getBitmapScaled(String dirname, String filename) {
    Bitmap compressedImage = null;
    Bitmap compressedImage1 = null;
    try {
        Matrix matrix = new Matrix();
        File file = getMeterFilePath(dirname, filename);

        ExifInterface exif = new ExifInterface(
                file.getAbsolutePath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        Log.d("1111111111111",""+orientation);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.postRotate(270);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.postRotate(180);

                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.postRotate(0);
                break;

            case ExifInterface.ORIENTATION_UNDEFINED:
                matrix.postRotate(90);

                break;
        }
        compressedImage = new Compressor.Builder(mContext)
                .setMaxWidth(AppConstants.IMAGE_WIDTH)
                .setMaxHeight(AppConstants.IMAGE_HEIGHT)
                .setQuality(1)
                .setCompressFormat(Bitmap.CompressFormat.JPEG)
                .build()
                .compressToBitmap(file);

        compressedImage1 = Bitmap.createBitmap(compressedImage, 0, 0, compressedImage.getWidth(),
                compressedImage.getHeight(), matrix, true);
        if (compressedImage1 != null)
            compressedImage1 = CommonUtils.addWaterMarkDate(compressedImage1, CommonUtils.getCurrentDateTime());
    } catch (Exception e) {
    }

    return compressedImage1;
}

我一直在使用此代码创建位图。使用此功能,不会压缩is图像的高度。第二次压缩用于旋转图像,我觉得第二个功能有问题。早些时候,我仅使用第一个函数来创建位图,该位图过去可以正常工作,但不能用于旋转图像,第二个功能不能正常工作。

0 个答案:

没有答案