拍摄照片发生反转,变量初始null

小编发现像Samsung或MEIZU等部分机子所拍摄的照片存在反转的问题,且拍摄设置上并没有启动“反向存储”的功能。那为何拍摄后取出来的照片被反转了,难道真的是机子本身的感应器产生作用了?如果有知晓的亲请告知。下面为拍摄反转的解决草案。

1.注册权限

<!-- 开启Camera权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

2.拍摄照片

Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String path = Environment.getExternalStorageDirectory() + "/定义存放文件夹名字/";
String name = String.valueOf(System.currentTimeMillis()) + ".png";
File file = new File(path);
if (!file.exists()) file.mkdirs();
String pathStr = path + name;
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(pathStr)));
startActivityForResult(openCameraIntent, "定义请求结果标识");

3.获取照片

Bitmap bitmap = revitionImageSize(path);// 获取并压缩图片
int degree = readImageDegree(path);// 获取原拍摄角度
bitmap = rotaingImageView(degree, bitmap);// 还原图片角度
ImageView控件.setImageBitmap(bitmap);

3.1.照片压缩

public static Bitmap revitionImageSize(String path) throws IOException {

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(in, null, options);
    in.close();

    int i = 0;
    Bitmap bitmap = null;
    while (true) {
        if ((options.outWidth >> i <= 1000)
                && (options.outHeight >> i <= 1000)) {
            in = new BufferedInputStream(
                    new FileInputStream(new File(path)));
            options.inSampleSize = (int) Math.pow(2.0D, i);
            options.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeStream(in, null, options);
            break;
        }
        i += 1;
    }
    return bitmap;
}

3.2.获取原照片角度

public static int readImageDegree(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;
}

3.3.根据角度值反转

public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {

    if(bitmap == null) return null;

    // 旋转图片 动作
    Matrix matrix = new Matrix();;
    matrix.postRotate(angle);
    System.out.println("angle2="+ angle);
    // 创建新的图片
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
            bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    return resizedBitmap;
}

以上为获取拍摄的照片并解决反转的问题,小编参考了其他更多作者的实现。还有一问题就是嵌套使用Fragment的UI,从Camera跳转回来后成员变量被初始Null了,其实是在开启Camera并旋转屏幕的时候将重新执行生命周期,且Activity被系统回收所致,固然得在注册表里为Activity声明禁用横竖屏切换

android:configChanges="orientation|keyboardHidden|screenSize"

一开始小编也不晓得为何要加上“screenSize”,后来看到有作者称如果在android3.2以后的版本中没有添加”screenSize”的话依然会触发横竖屏activity销毁周期(也就是说不能屏蔽activity销毁,从而调用onConfigurationChanged方法,而在3.2之前只要设置orientation|keyboardHidden就可以)。相信很多人加了声明之后仍旧不行,那么还得复写Activity的onConfigurationChanged

public void onConfigurationChanged( android.content.res.Configuration newConfig) {
   // 这里什么都不要做
   super.onConfigurationChanged(newConfig); 
};

这样一来小编既解决了获取拍摄图片角度反转的问题,也解决了变量被初始Null的问题了。相信有其他更高效的解决方案,受教。另外以上内容仅供参考,如有表述不正确的也请为小编论道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值