当前位置: 移动技术网 > IT编程>移动开发>Android > Android部分手机拍照后获取的图片被旋转问题的解决方法

Android部分手机拍照后获取的图片被旋转问题的解决方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

连云港核废料抗议视频,变形金刚2 下载,惠普1536硒鼓型号

调用android系统拍照功能后,三星手机拍摄后的照片被旋转了90度,横着拍给你变成竖的,竖的拍给你变成横的。其它品牌的手机都是正常的,就三星出现这个怪事。

在android适配上,我原来一直以为国内的小米手机够奇葩了,结果还有更奇葩的!你说你没事旋转照片干啥,实在是猜不透其居心何在,纯粹是在给开发者制造麻烦啊!

解决办法是获取到拍照后照片被旋转的角度,再旋转回去就好了。

具体思路:
1、首先在调用拍照方法时,保存拍照后的相片原图,得到原图路径,(photobitmaputils是我自己写的一个工具类)

string filename = ""; 
/** 
 * 启动相机拍照 
 */ 
private void addbitmapshoots() { 
 intent intent = new intent(mediastore.action_image_capture); 
 // 设置图片要保存的 根路径+文件名 
 filename = photobitmaputils.getphotofilename(getcontext()); 
 file file = new file(filename); 
 if (!file.exists()) { 
  try { 
   file.createnewfile(); 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } 
 } 
 intent.putextra(mediastore.extra_output, uri.fromfile(file)); 
 startactivityforresult(intent, open_camera); 
} 

2、在获取相机返回的回调方法onactivityresult()中,修复被旋转的图片并取得修复后的图片路径,有了这个路径后就可以展示出来了

@override 
public void onactivityresult(int requestcode, int resultcode, intent data) { 
 super.onactivityresult(requestcode, resultcode, data); 
 // 获取相机拍照返回 
 if (resultcode == activity.result_ok && requestcode == open_camera) { 
  // 得到修复后的照片路径 
  string filepath = photobitmaputils.amendrotatephoto(filename, getcontext()); 
 } 
} 

photobitmaputils类:

/** 
 * 集合一些图片工具 
 * 
 * created by zhuwentao on 2016-07-22. 
 */ 
public class photobitmaputils { 
 
 /** 
  * 存放拍摄图片的文件夹 
  */ 
 private static final string files_name = "/myphoto"; 
 /** 
  * 获取的时间格式 
  */ 
 public static final string time_style = "yyyymmddhhmmss"; 
 /** 
  * 图片种类 
  */ 
 public static final string image_type = ".png"; 
 
 // 防止实例化 
 private photobitmaputils() { 
 } 
 
 /** 
  * 获取手机可存储路径 
  * 
  * @param context 上下文 
  * @return 手机可存储路径 
  */ 
 private static string getphonerootpath(context context) { 
  // 是否有sd卡 
  if (environment.getexternalstoragestate().equals(environment.media_mounted) 
    || !environment.isexternalstorageremovable()) { 
   // 获取sd卡根目录 
   return context.getexternalcachedir().getpath(); 
  } else { 
   // 获取apk包下的缓存路径 
   return context.getcachedir().getpath(); 
  } 
 } 
 
 /** 
  * 使用当前系统时间作为上传图片的名称 
  * 
  * @return 存储的根路径+图片名称 
  */ 
 public static string getphotofilename(context context) { 
  file file = new file(getphonerootpath(context) + files_name); 
  // 判断文件是否已经存在,不存在则创建 
  if (!file.exists()) { 
   file.mkdirs(); 
  } 
  // 设置图片文件名称 
  simpledateformat format = new simpledateformat(time_style, locale.getdefault()); 
  date date = new date(system.currenttimemillis()); 
  string time = format.format(date); 
  string photoname = "/" + time + image_type; 
  return file + photoname; 
 } 
 
 /** 
  * 保存bitmap图片在sd卡中 
  * 如果没有sd卡则存在手机中 
  * 
  * @param mbitmap 需要保存的bitmap图片 
  * @return 保存成功时返回图片的路径,失败时返回null 
  */ 
 public static string savephototosd(bitmap mbitmap, context context) { 
  fileoutputstream outstream = null; 
  string filename = getphotofilename(context); 
  try { 
   outstream = new fileoutputstream(filename); 
   // 把数据写入文件,100表示不压缩 
   mbitmap.compress(bitmap.compressformat.png, 100, outstream); 
   return filename; 
  } catch (exception e) { 
   e.printstacktrace(); 
   return null; 
  } finally { 
   try { 
    if (outstream != null) { 
     // 记得要关闭流! 
     outstream.close(); 
    } 
    if (mbitmap != null) { 
     mbitmap.recycle(); 
    } 
   } catch (exception e) { 
    e.printstacktrace(); 
   } 
  } 
 } 
 
 /** 
  * 把原图按1/10的比例压缩 
  * 
  * @param path 原图的路径 
  * @return 压缩后的图片 
  */ 
 public static bitmap getcompressphoto(string path) { 
  bitmapfactory.options options = new bitmapfactory.options(); 
  options.injustdecodebounds = false; 
  options.insamplesize = 10; // 图片的大小设置为原来的十分之一 
  bitmap bmp = bitmapfactory.decodefile(path, options); 
  options = null; 
  return bmp; 
 } 
 
 /** 
  * 处理旋转后的图片 
  * @param originpath 原图路径 
  * @param context 上下文 
  * @return 返回修复完毕后的图片路径 
  */ 
 public static string amendrotatephoto(string originpath, context context) { 
 
  // 取得图片旋转角度 
  int angle = readpicturedegree(originpath); 
 
  // 把原图压缩后得到bitmap对象 
  bitmap bmp = getcompressphoto(originpath);; 
 
  // 修复图片被旋转的角度 
  bitmap bitmap = rotaingimageview(angle, bmp); 
 
  // 保存修复后的图片并返回保存后的图片路径 
  return savephototosd(bitmap, context); 
 } 
 
 /** 
  * 读取照片旋转角度 
  * 
  * @param path 照片路径 
  * @return 角度 
  */ 
 public static int readpicturedegree(string path) { 
  int degree = 0; 
  try { 
   exifinterface exifinterface = new exifinterface(path); 
   int orientation = exifinterface.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_normal); 
   switch (orientation) { 
    case exifinterface.orientation_rotate_90: 
     degree = 90; 
     break; 
    case exifinterface.orientation_rotate_180: 
     degree = 180; 
     break; 
    case exifinterface.orientation_rotate_270: 
     degree = 270; 
     break; 
   } 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } 
  return degree; 
 } 
 
 /** 
  * 旋转图片 
  * @param angle 被旋转角度 
  * @param bitmap 图片对象 
  * @return 旋转后的图片 
  */ 
 public static bitmap rotaingimageview(int angle, bitmap bitmap) { 
  bitmap returnbm = null; 
  // 根据旋转角度,生成旋转矩阵 
  matrix matrix = new matrix(); 
  matrix.postrotate(angle); 
  try { 
   // 将原始图片按照旋转矩阵进行旋转,并得到新的图片 
   returnbm = bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), bitmap.getheight(), matrix, true); 
  } catch (outofmemoryerror e) { 
  } 
  if (returnbm == null) { 
   returnbm = bitmap; 
  } 
  if (bitmap != returnbm) { 
   bitmap.recycle(); 
  } 
  return returnbm; 
 } 
} 

在调用修复图片角度方法的时候需要注意,现在的手机像素越来越大,拍完后一张照片有近10m,所以我们需要对图片进行压缩处理。不然在保存图片时会等待挺久的,屏幕会黑一会。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网