Android矩阵postrotae在某些设备中不起作用

时间:2019-02-20 05:25:56

标签: android matrix exif

在我的新应用中,我需要从图库中获取图像并将其上传到服务器中。 我需要根据图像的方向旋转一些图像,为此,我使用了波纹管代码。一切都很好,并且在许多设备上都运行良好,但是在某些设备(例如Samsung g610)中,matrix.postRotate(90.0f)不适用于方向6的图像(垂直图像); 有什么问题吗?

 public static Bitmap loadBitmapforUpload(String path, float maxWidth,
                                         float maxHeight, boolean useMaxScale) {
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bmOptions);
    float photoW = bmOptions.outWidth;
    float photoH = bmOptions.outHeight;

    float scaleFactor = useMaxScale ? Math.max(photoW / maxWidth, photoH / maxHeight) :
            Math.min(photoW / maxWidth, photoH / maxHeight);
    if (scaleFactor < 1) {
        scaleFactor = 1;
    }
    bmOptions.inJustDecodeBounds = false;

    bmOptions.inSampleSize = (int) scaleFactor;
    if (bmOptions.inSampleSize % 2 != 0) {
        int sample = 1;
        while (sample * 2 < bmOptions.inSampleSize) {
            sample *= 2;
        }
        bmOptions.inSampleSize = sample;
    }

    bmOptions.inPurgeable = Build.VERSION.SDK_INT < 21;
    Matrix matrix = null;
    try {
        ExifInterface exif;
        exif = new ExifInterface(path);

        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.postRotate(90.0f);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.postRotate(180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.postRotate(270);
                break;
        }
    } catch (Throwable e) {
        FileLog.e(e);
    }
    Bitmap b = null;
    try {
        b = BitmapFactory.decodeFile(path, bmOptions);
        if (b != null) {
            Bitmap newBitmap = Bitmaps.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
            if (newBitmap != b) {
                b.recycle();
                b = newBitmap;
            }
        }
    } catch (Throwable e) {
        FileLog.e(e);
    }
    return b;
}

0 个答案:

没有答案