如何更改相机意图拍摄的图像方向?

时间:2014-12-18 09:01:49

标签: android

在我的应用程序中我正在从相机意图中拍摄照片,然后在另一个类中创建该图像的缩略图并返回该缩略图,但是如果我以肖像拍摄它将返回为景观。谷歌搜索时,我发现在三星设备中这是一个问题?有没有办法解决这个问题?

这是我创建缩略图的代码:

public class GetImageThumbnail {

private static int getPowerOfTwoForSampleRatio(double ratio) {
int k = Integer.highestOneBit((int) Math.floor(ratio));
if (k == 0)
    return 1;
else
    return k;
}

public Bitmap getThumbnail(Uri uri, Test test)
    throws FileNotFoundException, IOException {
InputStream input = ((Context) test).getContentResolver().openInputStream(uri);

BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither = true;// optional
onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -2)
        || (onlyBoundsOptions.outHeight == -2))
    return null;

int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
        : onlyBoundsOptions.outWidth;

double ratio = (originalSize > 200) ? (originalSize / 175) : 0.5;

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
bitmapOptions.inDither = true;
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
input = ((Context) test).getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
return bitmap;
}
}

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

Activity&当您在onActivityResult中获得位图时,您可以调用此方法

public Bitmap changeOrientation(Uri imageUri, String imagePath, Bitmap source) {
        // TODO Auto-generated constructor stub
        int rotate = 0;
        int orientation = 0;
        try {
            getContentResolver().notifyChange(imageUri, null);
            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            }
            // Log.v(Common.TAG, "Exif orientation: " + orientation);
        } catch (Exception e) {
            e.printStackTrace();
        }

        /****** Image rotation ****/
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);
        Bitmap cropped = Bitmap.createBitmap(source, x, y, width, height, matrix,
                true);
        return cropped;
        /*
         * 
         * 
         * Bitmap android.graphics.Bitmap.createBitmap(Bitmap source, int x, int
         * y, int width, int height, Matrix m, boolean filter) public static
         * Bitmap createBitmap (Bitmap source, int x, int y, int width, int
         * height, Matrix m, boolean filter) Added in API level 1 Returns an
         * immutable bitmap from subset of the source bitmap, transformed by the
         * optional matrix. The new bitmap may be the same object as source, or
         * a copy may have been made. It is initialized with the same density as
         * the original bitmap. If the source bitmap is immutable and the
         * requested subset is the same as the source bitmap itself, then the
         * source bitmap is returned and no new bitmap is created.
         * 
         * Parameters source The bitmap we are subsetting x The x coordinate of
         * the first pixel in source y The y coordinate of the first pixel in
         * source width The number of pixels in each row height The number of
         * rows m Optional matrix to be applied to the pixels filter true if the
         * source should be filtered. Only applies if the matrix contains more
         * than just translation.
         * 
         * Returns A bitmap that represents the specified subset of source
         * Throws IllegalArgumentException if the x, y, width, height values are
         * outside of the dimensions of the source bitmap, or width is <= 0, or
         * height is <= 0
         */
    }

答案 1 :(得分:1)

您可以使用ExifInterface

首先通过以下方法获取图像上的捕获方向

public int getImageOrientation(String imagePath) {
        int rotate = 0;
        try {
            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rotate;
    }

然后使用

Matrix matrix = new Matrix();
                matrix.postRotate(getImageOrientation(path));
                photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(),
                        photo.getHeight(), matrix, true);

重绘位图。

答案 2 :(得分:0)

您可以通过Exif检查方向。如果图片方向错误,您可以旋转图片:Android rotate bitmap around center without resizing