在安卓手机上,使用相机拍摄的照片面临方向问题

5

我正在使用exifInterface解决照片旋转问题,以下是代码:

  • 从文件中创建一个位图
Bitmap b = BitmapFactory.decodeFile(imagePath);
  • 通过缩放位图到适当的级别来调整大小
int width = b.getWidth();
int height = b.getHeight();
int newWidth = 150;
int newHeight = 150;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, width, height, matrix, true);
// resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
  • 处理图像的方向
 ExifInterface exif = new ExifInterface(imagePath);
    String orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    if (orientation.equals(ExifInterface.ORIENTATION_NORMAL)) {
            // Do nothing. The original image is fine.
    } else if (orientation.equals(ExifInterface.ORIENTATION_ROTATE_90+"")) {
            matrix.postRotate(90);
    } else if (orientation.equals(ExifInterface.ORIENTATION_ROTATE_180+"")) {
            matrix.postRotate(180);
    } else if (orientation.equals(ExifInterface.ORIENTATION_ROTATE_270+"")) {
            matrix.postRotate(270);
}
  • 保存新的位图
 out = new FileOutputStream(new File("some output file path"));
    Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, width, height, matrix, true);
    resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);

这段代码不能解决旋转问题,请给我指导。 在 LG 设备上,ExifInterface 总是返回 0 方向,在三星设备上返回 6 和 1。

如何解决所有设备(如 htc、Motorola、Samsung、Sony 和 LG)的问题。

非常感谢各位,求助!

1个回答

7
您可以使用此函数来完成您需要的相同操作。将此函数放在您的活动或任何实用程序类中,并调用它以从文件路径获取位图。
我一直在我的应用程序中使用这个函数,我的主要测试设备是LG。
public static Bitmap decodeFile(String path) {
    int orientation;
    try {
        if (path == null) {
            return null;
        }
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 8;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale++;
        }
        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bm = BitmapFactory.decodeFile(path, o2);
        Bitmap bitmap = bm;

        ExifInterface exif = new ExifInterface(path);
        orientation = exif
                .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        Log.e("orientation", "" + orientation);
        Matrix m = new Matrix();

        if ((orientation == 3)) {
            m.postRotate(180);
            m.postScale((float) bm.getWidth(), (float) bm.getHeight());
            // if(m.preRotate(90)){
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                    bm.getHeight(), m, true);
            return bitmap;
        } else if (orientation == 6) {
            m.postRotate(90);
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                    bm.getHeight(), m, true);
            return bitmap;
        }
        else if (orientation == 8) {
            m.postRotate(270);
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                    bm.getHeight(), m, true);
            return bitmap;
        }
        return bitmap;
    } catch (Exception e) {
    }
    return null;
}

是的,我也用 HTC Explorer 进行了测试,它的运行情况和 LG Optimus Black 一样良好。@user991429 - MKJParekh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接