zoukankan      html  css  js  c++  java
  • 竖屏拍照,但是sd卡中却是横屏解决方法

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
      
            switch (resultCode) {  
            case 1:  
                if (data != null) {  
                    // 取得返回的Uri,基本上选择照片的时候返回的是以Uri形式,但是在拍照中有得机子呢Uri是空的,所以要特别注意  
                    Uri mImageCaptureUri = data.getData();  
                    // 返回的Uri不为空时,那么图片信息数据都会在Uri中获得。如果为空,那么我们就进行下面的方式获取  
                    if (mImageCaptureUri != null) {  
                        setImage(mImageCaptureUri);// 根据Uri处理并显示图片  
                    }  
                }  
                break;  
            default:  
                break;  
      
            }  
        }  

    setImage方法里面对图片进行旋转

    private void setImage(Uri mImageCaptureUri) {  
      
        // 不管是拍照还是选择图片每张图片都有在数据中存储也存储有对应旋转角度orientation值  
        // 所以我们在取出图片是把角度值取出以便能正确的显示图片,没有旋转时的效果观看  
      
        ContentResolver cr = this.getContentResolver();  
        Cursor cursor = cr.query(mImageCaptureUri, null, null, null, null);// 根据Uri从数据库中找  
        if (cursor != null) {  
            cursor.moveToFirst();// 把游标移动到首位,因为这里的Uri是包含ID的所以是唯一的不需要循环找指向第一个就是了  
            String filePath = cursor.getString(cursor.getColumnIndex("_data"));// 获取图片路  
            String orientation = cursor.getString(cursor  
                    .getColumnIndex("orientation"));// 获取旋转的角度  
            cursor.close();  
            if (filePath != null) {  
                Bitmap bitmap = BitmapFactory.decodeFile(filePath);//根据Path读取资源图片  
                int angle = 0;  
                if (orientation != null && !"".equals(orientation)) {  
                    angle = Integer.parseInt(orientation);  
                }  
                if (angle != 0) {  
                    // 下面的方法主要作用是把图片转一个角度,也可以放大缩小等  
                    Matrix m = new Matrix();  
                    int width = bitmap.getWidth();  
                    int height = bitmap.getHeight();  
                    m.setRotate(angle); // 旋转angle度  
                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,  
                            m, true);// 从新生成图片  
                      
                }  
                photo.setImageBitmap(bitmap);  
            }  
        }  
    }  

    如果获取不到uri,可以尝试直接将bitmap传到方法里进行操作

  • 相关阅读:
    设计模式一 Simple Factory, Factory Method, Abstract Factory以及Builder模式简述
    SQL Server中对XML操作
    开发常用小工具介绍
    强制休息程序 EyeGuardian 眼睛守护者 Beta测试版
    定时计划任务方案比较以及通过脚本创建计划任务(SchTasks命令)
    在Myeclipse中配置Maven
    Jena的环境配置
    0x01_go代码简单示例
    0x00_go语言安装
    信息收集工具
  • 原文地址:https://www.cnblogs.com/robben/p/4772749.html
Copyright © 2011-2022 走看看