asysbang

 找回密码
 立即注册
查看: 17267|回复: 1
打印 上一主题 下一主题

正确显示竖屏预览和拍照的照片

[复制链接]

513

主题

2

好友

6404

积分

管理员

Rank: 80Rank: 80Rank: 80Rank: 80Rank: 80

最佳新人 活跃会员 热心会员 推广达人 宣传达人 灌水之王 突出贡献 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-5-27 19:08:06 |只看该作者 |倒序浏览
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;
            }


回复

使用道具 举报

无效楼层,该帖已经被删除
您需要登录后才可以回帖 登录 | 立即注册

Archiver|手机版|aSys-帮 ( 京ICP备13033689号 )

GMT+8, 2024-10-5 15:27 , Processed in 0.070395 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部