L版本设置壁纸为什么只支持JPEG格式图片旋转设置

来源:互联网 发布:阿里妈妈淘宝客注册 编辑:程序博客网 时间:2024/05/13 08:52

    在L版本中,图片旋转之后,设置壁纸,只有JPEG格式可以保存为旋转后的图片方向。

这里设置使用的L版本自带的Launcher3中\packages\apps\Launcher3\WallpaperPicker\src\com\android\launcher3\WallpaperCropActivity.java

大概设置壁纸流程:1,首先 通过 Uri /path/Resurce 三种资源都可以获得图片,;

                                    2,其次通过BitmapCropTask extends AsyncTask<Void, Void, Boolean>将资源解析;

                                    3,调用WallpaperManager来讲图片资源实例到系统壁纸中。


阅读这段代码,可以发现设置壁纸有很多方法入口,比如

protected void cropImageAndSetWallpaper(Uri uri,
            OnBitmapCroppedHandler onBitmapCroppedHandler, final boolean finishActivityWhenDone)

或者

 protected void cropImageAndSetWallpaper(
            Resources res, int resId, final boolean finishActivityWhenDone)


但是设计到图片的方向最后都会归于一个方法—————    private static int getRotationFromExifHelper(String path, Resources res, int resId, Context context, Uri uri) 


那么我们来看一下这个方法的具体代码:

private static int getRotationFromExifHelper(            String path, Resources res, int resId, Context context, Uri uri) {        ExifInterface ei = new ExifInterface();//生成ExifInterface对象        InputStream is = null;        BufferedInputStream bis = null;        try {            if (path != null) {                ei.readExif(path);//读取资源图片的Exif头信息            } else if (uri != null) {                is = context.getContentResolver().openInputStream(uri);                bis = new BufferedInputStream(is);                ei.readExif(bis); //<span style="font-family: Arial, Helvetica, sans-serif;">读取资源图片的Exif头信息</span>            } else {                is = res.openRawResource(resId);                  bis = new BufferedInputStream(is);                ei.readExif(bis);      //<span style="font-family: Arial, Helvetica, sans-serif;">读取资源图片的Exif头信息</span>            }            Integer ori = ei.getTagIntValue(ExifInterface.TAG_ORIENTATION);  //直接获取Exif头信息中的方向值            if (ori != null) {                return ExifInterface.getRotationForOrientationValue(ori.shortValue());            }        } catch (IOException e) {            Log.w(LOGTAG, "Getting exif data failed", e);        } catch (NullPointerException e) {            // Sometimes the ExifInterface has an internal NPE if Exif data isn't valid            Log.w(LOGTAG, "Getting exif data failed", e);        } finally {            Utils.closeSilently(bis);            Utils.closeSilently(is);        }        return 0; //方向值一般都是 0,90,180,270 所以当没有获取到资源的方向,默认返回0,即没有旋转的图片    }
这个方法有一个极其重要的类,就是ExifInterface;我们可以主要读取图片方向的代码都是ExifInterface的一些方法,那么这个ExifInterface到底是怎么来的呢。

我们来看一下framework里面对于ExifInterface的描述

/** * This is a class for reading and writing Exif tags in a JPEG file. */public class ExifInterface {    // The Exif tag names    /** Type is int. */    public static final String TAG_ORIENTATION = "Orientation";    /** Type is String. */    public static final String TAG_DATETIME = "DateTime";    /** Type is String. */    public static final String TAG_MAKE = "Make";    /** Type is String. */    public static final String TAG_MODEL = "Model";... ...
This is a class for reading and writing Exif tags in a JPEG file.原来这个类仅仅是为了JPEG图片准备的。

百度百科的描述:

Exif是英文Exchangeable Image File(可交换图像文件)的缩写,最初由日本电子工业发展协会(JEIDA --Japan Electronic Industry Development Association) 制订,目前的最新版本是发表于2002年04月的2.21 版。国际标准化组织(ISO)正在制订的相机文件设计标准(DCF -- Design role for Camera File system)可能以Exif2.1为基础。
Exif 文件实际是JPEG文件的一种,遵从JPEG标准,只是在文件头信息中增加了有关拍摄信息的内容和索引图。所以你可以使用任何支持JPEG格式的图像工具软件观看或修改Exif文件,但,打开时可能看不到Exif信息,一旦修改,Exif信息可能丢失。

   

Exif是英文Exchangeable Image File(可交换图像文件)的缩写,最初由日本电子工业发展协会(JEIDA --Japan Electronic Industry Development Association) 制订,目前的最新版本是发表于2002年04月的2.21 版。国际标准化组织(ISO)正在制订的相机文件设计标准(DCF -- Design role for Camera File system)可能以Exif2.1为基础。
Exif 文件实际是JPEG文件的一种,遵从JPEG标准,只是在文件头信息中增加了有关拍摄信息的内容和索引图。所以你可以使用任何支持JPEG格式的图像工具软件观看或修改Exif文件,但,打开时可能看不到Exif信息,一旦修改,Exif信息可能丢失。
那么我们现在就知道为什么android  L版本仅仅支持对于JPEG格式图片的旋转设置壁纸了。



0 0
原创粉丝点击