经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android通过ExifInterface判断Camera图片方向的方法
来源:jb51  时间:2018/12/14 9:11:29  对本文有异议

Android的Camera相关应用开发中,有一个必须搞清楚的知识点,就是Camera的预览方向和拍照方向

图像的Sensor方向:手机Camera的图像数据都是来自于摄像头硬件的图像传感器(Image Sensor),这个Sensor被固定到手机之后是有一个默认的取景方向的,这个方向如下图所示,坐标原点位于手机横放时的左上角:

android应用里使用相机图片时必须要考虑的一个问题就是图片朝向,只有判断对朝向才能调整图片从而更好的展现。本文将介绍一种通过ExifInterface判断图片朝向的方法!上代码:

  1. /**
  2. * 利用给定路径下的图片设置ImageView
  3. * @param imgPath 手机图片文件路径
  4. * @param imgView 需要设置的ImageView
  5. */
  6. public void setImg(String imgPath, ImageView imgView) {
  7. File file = new File(imgPath);
  8. if (file.exists() && file.canRead()) {
  9. // -------1.图片缩放--------
  10. // 手机屏幕信息
  11. DisplayMetrics metric = new DisplayMetrics();
  12. getWindowManager().getDefaultDisplay().getMetrics(metric);
  13. int dw = metric.widthPixels; // 屏幕宽
  14. int dh = metric.heightPixels; // 屏幕高
  15. // 加载图像,只是为了获取尺寸
  16. BitmapFactory.Options options = new BitmapFactory.Options();
  17. options.inJustDecodeBounds = true; // 设置之后可以获取尺寸信息
  18. Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
  19. // 计算水平和垂直缩放系数
  20. int heightRatio = (int) Math.ceil(options.outHeight / (float) dh);
  21. int widthRatio = (int) Math.ceil(options.outWidth / (float) dw);
  22. // 判断哪个大
  23. if (heightRatio > 1 && widthRatio > 1) {
  24. if (heightRatio > widthRatio) {
  25. options.inSampleSize = heightRatio;
  26. } else {
  27. options.inSampleSize = widthRatio;
  28. }
  29. }
  30. // 图片缩放
  31. options.inJustDecodeBounds = false;
  32. bitmap = BitmapFactory.decodeFile(imgPath, options);
  33. // -------2.判断图片朝向--------
  34. try {
  35. ExifInterface exif = new ExifInterface(imgPath);
  36. int degree = 0; // 图片旋转角度
  37. if (exif != null) {
  38. int orientation = exif.getAttributeInt(
  39. ExifInterface.TAG_ORIENTATION, -1);
  40. if (orientation != -1) {
  41. switch (orientation) {
  42. case ExifInterface.ORIENTATION_ROTATE_90:
  43. degree = 90;
  44. break;
  45. case ExifInterface.ORIENTATION_ROTATE_180:
  46. degree = 180;
  47. break;
  48. case ExifInterface.ORIENTATION_ROTATE_270:
  49. degree = 270;
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. }
  56. if (degree != 0) { // 图片需要旋转
  57. int width = bitmap.getWidth();
  58. int height = bitmap.getHeight();
  59. Matrix matrix = new Matrix();
  60. matrix.preRotate(degree);
  61. Bitmap mRotateBitmap = Bitmap.createBitmap(bitmap, 0, 0,
  62. width, height, matrix, true);
  63. imgView.setImageBitmap(mRotateBitmap);
  64. } else {
  65. imgView.setImageBitmap(bitmap);
  66. }
  67. } catch (IOException e) {
  68. }
  69. }
  70. }

本代码包含两大功能:

1. 图片缩放:原始图片一般比较大,经过缩小才能使用;

2. 图片旋转:由于用户拍照时手机角度不同,所得照片可能需要旋转。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对w3xue的支持。如果你想了解更多相关内容请查看下面相关链接

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号