为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼啦一阵风 2019-11-12 11:13:59
我有以下代码://choosed a picturepublic 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图片?谢谢莱斯利
查看完整描述

3 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

如果使用数码相机或智能手机拍摄照片,则旋转通常作为图像文件的一部分存储在照片的Exif数据中。您可以使用Android读取图像的Exif元数据ExifInterface。


首先,创建ExifInterface:


ExifInterface exif = new ExifInterface(uri.getPath());

接下来,找到当前旋转:


int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  

将exif旋转转换为度数:


int rotationInDegrees = exifToDegrees(rotation);

哪里


private static int exifToDegrees(int exifOrientation) {        

    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } 

    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {  return 180; } 

    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  return 270; }            

    return 0;    

 }

然后,以图像的实际旋转为参考点,使用来旋转图像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);


查看完整回答
反对 回复 2019-11-12
  • 3 回答
  • 0 关注
  • 724 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信