如何改变由照相机意图拍摄图像的方向? [英] How to change the orientation of image taken by camera intent?

查看:163
本文介绍了如何改变由照相机意图拍摄图像的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我正在拍摄照片从相机的意图,然后在不同的类,它创建图像的缩略图,并返回一个缩略图,但是如果我拍照人像返回景观。而谷歌搜索这个时候,我发现,在三星的设备,这是一个问题吗?有没有解决这个方式?

in my app i am taking a picture from the camera intent, and then in a different class it creates a thumbnail of that image and returns that thumbnail, However if i take the picture in portrait it returns as landscape. And when googling this, i found that in samsung devices this is a problem? is there a way of resolving this?

这是我的code创建缩略图:

here is my code for creating the thumbnail:

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;
}
}

能否有人请帮助?

Could some one please help?

推荐答案

活动使用此方法&安培;当你得到了在的onActivityResult 位图,你可以调用此方法

use this method in your Activity & when you got the bitmap in onActivityResult you can call this method

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
         */
    }

这篇关于如何改变由照相机意图拍摄图像的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆