【问题标题】:How to change the orientation of image taken by camera intent?如何更改相机意图拍摄的图像的方向?
【发布时间】:2014-12-18 20:02:42
【问题描述】:

在我的应用程序中,我从相机意图拍摄照片,然后在不同的类中创建该图像的缩略图并返回该缩略图,但是如果我以纵向拍摄照片,它会以横向形式返回。当谷歌搜索时,我发现在三星设备中这是一个问题?有没有办法解决这个问题?

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

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

有人可以帮忙吗?

【问题讨论】:

    标签: android


    【解决方案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);
    

    重绘位图。

    【讨论】:

    • 很酷,谢谢,但是(对不起,现在这个愚蠢的问题)我在哪里插入这个代码在我的onActivityResult 中,它从public class GetImageThumbnailpublic class GetImageThumbnail 返回?
    • 在你的 onActivityResult 方法中是的,它将返回一个位图,获取该位图并旋转它并重新创建它。
    【解决方案2】:

    在您的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
             */
        }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        • 2011-07-15
        • 1970-01-01
        相关资源
        最近更新 更多