Android获取相机位图的方向?并向后旋转-90度

我有以下代码:


//choosed a picture

public void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (resultCode == RESULT_OK) {

        if (requestCode == ImageHelper.SELECT_PICTURE) {


            String picture           = "";


            Uri selectedImageUri     = data.getData();

            //OI FILE Manager

            String filemanagerstring = selectedImageUri.getPath();

            //MEDIA GALLERY

            String selectedImagePath = ImageHelper.getPath(mycontext, selectedImageUri);


            picture=(selectedImagePath!=null)?selectedImagePath:filemanagerstring;

...


这只是图库中的图片选择器。这很好,但是当我在imageview上打开该图片时,使用相机在“肖像模式”下拍摄的图像看起来不错,但是使用相机在“景观模式”下拍摄的图像以-90度打开。


如何旋转这些图片?


    Bitmap output       = Bitmap.createBitmap(newwidth, newheight, Config.ARGB_8888);

    Canvas canvas       = new Canvas(output);

我尝试了这个:


Log.e("w h", bitmap.getWidth()+" "+bitmap.getHeight());

if (bitmap.getWidth()<bitmap.getHeight()) canvas.rotate(-90);

但这不起作用,所有图像尺寸均为:* 2560 1920像素(全部为肖像和横向模式)


我该怎么做才能旋转回LANDSCAPE图片?


谢谢莱斯利


呼啦一阵风
浏览 732回答 3
3回答

互换的青春

如果使用数码相机或智能手机拍摄照片,则旋转通常作为图像文件的一部分存储在照片的Exif数据中。您可以使用Android读取图像的Exif元数据ExifInterface。首先,创建ExifInterface:ExifInterface exif = new ExifInterface(uri.getPath());接下来,找到当前旋转:int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);&nbsp;&nbsp;将exif旋转转换为度数:int rotationInDegrees = exifToDegrees(rotation);哪里private static int exifToDegrees(int exifOrientation) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }&nbsp;&nbsp; &nbsp; else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {&nbsp; return 180; }&nbsp;&nbsp; &nbsp; else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {&nbsp; return 270; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return 0;&nbsp; &nbsp;&nbsp;&nbsp;}然后,以图像的实际旋转为参考点,使用来旋转图像Matrix。Matrix matrix = new Matrix();if (rotation != 0) {matrix.preRotate(rotationInDegrees);}使用将Bitmap.createBitmapa Matrix作为参数的方法来创建新的旋转图像:Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)其中Matrix m拥有新的轮换:Bitmap adjustedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);
打开App,查看更多内容
随时随地看视频慕课网APP