zoukankan      html  css  js  c++  java
  • 正确显示竖屏预览和拍照的照片

    1、预览时正确显示


          主要参考系统相机代码实现getDisplayOritation就可以了
          //在preview之前调用setDisplayOrientation
          int degrees = getDisplayOritation(getDispalyRotation(), cameraId);
          mCamera.setDisplayOrientation(degrees);
          mCamera.startPreview();


          
         getDisplayOritation函数如下:
        private int getDisplayOritation(int degrees, int cameraId) {
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(cameraId, info);
            int result;
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                result = (info.orientation + degrees) % 360;
                result = (360 - result) % 360;
            } else {
                result = (info.orientation - degrees + 360) % 360;
            }
            return result;
        }
        
        private int getDispalyRotation() {
            int i = getWindowManager().getDefaultDisplay().getRotation();
            switch (i) {
            case Surface.ROTATION_0:
                return 0;
            case Surface.ROTATION_90:
                return 90;
            case Surface.ROTATION_180:
                return 180;
            case Surface.ROTATION_270:
                return 270;
            }
            return 0;
        }




    2、显示图片时正确显示


         竖屏拍照的照片,直接使用的话,会旋转90度


         参考系统图库的代码,需要先查询mediascanner的orientation字段,然后应用再把角度旋转过来,这样显示就ok了


         参考代码如下:


               假设c为查询mediaprovider数据库返回的cursor


                int rotation = c.getInt(c.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
                if (rotation != 0) {
                    Bitmap bitmap = BitmapFactory.decodeFile(path);
                    imageBefore.setImageBitmap(bitmap);
                    Matrix m = new Matrix();
                    m.setRotate(rotation);
                    Bitmap transformed = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
                    return transformed;
                }
  • 相关阅读:
    clickhouse 多数据源
    maven-dependency-plugin maven-assembly-plugin
    maven shade plugin
    远程服务器,无法复制粘贴 (通过mstsc复制粘贴失败需要重新启动RDP剪切板监视程序rdpclip.exe)
    Mysql导入大sql文件方法
    MySQL5.7更新json类型字段中的某个key的值 函数json_replace()
    mysq json类型
    增强mybatis-plus的typeHandler,可以支持List<T> 中嵌套对象
    Windows中查看端口占用及关闭对应进程
    Hibernate中继承体现
  • 原文地址:https://www.cnblogs.com/nafio/p/9137285.html
Copyright © 2011-2022 走看看