Android图像格式类及图像转换方法介绍  一款软件的开发和图像密切相关,特别是移动应用程序,在视觉效果等方面是至关重要的,因为这直接关系到用户的体验效果。在Android程序开发的过程中,了解存在哪些图像格式类(ImageFormat、PixelFormat及BitmapConfig等)及图像(JPG、PNG及BMP等)的转换方法,对以后的开发多多少少会有些帮助。

  关于图像格式类,介绍以下三个:ImageFormat、PixelFormat及BitmapConfig。

  1、ImageFormat(android.graphics.ImageFormat),格式参数有以下几种:

    int JPEG ,Encoded formats,常量值: 256 (0x00000100)

    int NV16,YCbCr format, used for video,16 (0x00000010)

    int NV21,YCrCb format used for images, which uses the NV21 encoding format,常量值: 17 (0x00000011)

    int RGB_565,RGB format used for pictures encoded as RGB_565,常量值: 4 (0x00000004)

    int UNKNOWN, 常量值:0 (0x00000000)

    int YUY2,YCbCr format used for images,which uses YUYV (YUY2) encoding format,20 (0x00000014)

    int YV12,Android YUV format,This format is exposed to software decoders and applications

    YV12 is a 4:2:0 YCrCb planar format comprised of a WxH Y plane followed by (W/2) x (H/2) Cr and Cb planes

  解释总是英文的最通俗易懂,这里就不献丑翻译了。用法举例,在构建ImageReader类的对象时,会用到ImageFormat类的图像格式对象。如

 ImageReader imageReader = ImageReader.newInstance(width, height, ImageFormat.RGB_565, 2);

  imageReader对象表示其缓存中最多存在宽高分别为width和height、RGB_565格式的图像流两帧。

在需求中用哪一种图像格式,要视实际情况而定,后面的类似。

2、PixelFormat(android.graphics.PixelFormat),格式参数有以下几种:

    int A_8,常量值:8 (0x00000008)

    int JPEG,常量值:256 (0x00000100),constant,已声明不赞成使用,use ImageFormat.JPEG instead.

    int LA_88,常量值:10 (0x0000000a)

    int L_8, 常量值:9 (0x00000009)

    int OPAQUE,常量值: -1 (0xffffffff),System chooses an opaque format (no alpha bits required)

    int RGBA_4444,常量值:7 (0x00000007)

    int RGBA_5551,常量值:6 (0x00000006)

    int RGBA_8888,常量值:1 (0x00000001)

    int RGBX_8888,常量值:2 (0x00000002)

    int RGB_332,常量值:11 (0x0000000b)

    int RGB_565,常量值:4 (0x00000004)

    int RGB_888,常量值:3 (0x00000003)

    int TRANSLUCENT,常量值: -3 (0xfffffffd),System chooses a format that supports translucency (many alpha bits)

    int TRANSPARENT,常量值:-2 (0xfffffffe),System chooses a format that supports transparency (at least 1 alpha bit)

    int UNKNOWN,常量值: 0 (0x00000000)

    int YCbCr_420_SP,常量值:17 (0x00000011),constant 已声明不赞成使用 use ImageFormat.NV21 instead

    int YCbCr_422_I,常量值: 20 (0x00000014),constant 已声明不赞成使用 use ImageFormat.YUY2 instead

    int YCbCr_422_SP,常量值:16 (0x00000010),constant 已声明不赞成使用 use ImageFormat.NV16 instead

  注意,有四种图像格式已被声明不赞成使用,可以用ImaggFormat相对应的格式进行代替。由此可知,两种图像格式之间存在相通之处。用法举例,让窗口实现渐变的效果,如

 getWindow().setFormat(PixelFormat.RGBA_8888);

  补充说明:RGBA_8888为android的一种32位颜色格式,R、G、B、A分别用八位表示,Android默认的图像格式是PixelFormat.OPAQUE,其是不带Alpha值的。

  3、Bitmap.Config(Android.graphics.Bitmap内部类)

  Possible bitmap configurations。A bitmap configuration describes how pixels are stored。This affects the quality (color depth) as well as the ability to display transparent/translucent colors。(官网介绍,大致意思是说:影响一个图片色彩色度显示质量主要看位图配置,显示图片时透明还是半透明)。

    ALPHA_8:Each pixel is stored as a single translucency (alpha) channel。(原图的每一个像素以半透明显示)

    ARGB_4444:This field was deprecated in API level 13。Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead。(在API13以后就被弃用了,建议使用8888)。

    ARGB_8888 :Each pixel is stored on 4 bytes。 Each channel (RGB and alpha for translucency) is stored with 8 bits of precision (256 possible values) 。This configuration is very flexible and offers the best quality。 It should be used whenever possible。(每个像素占4个字节,每个颜色8位元,反正很清晰,看着很舒服)。

    RGB_565:Each pixel is stored on 2 bytes and only the RGB channels are encoded:red is stored with 5 bits of precision (32 possible values),green is stored with 6 bits of precision (64 possible values) and blue is stored with 5 bits of precision。(这个应该很容易理解了)。

  用法举例,构建Bitmap对象时,会用到BitmapConfig类图像格式对象,如:

 Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.RGB_565)

  下面来看各种类型图像之间的转换都有哪些方法、差异及共同点。

  1、YUV转JPG

  查阅到的资料大部分是把Yuv图像数据通过数学运算得到每个像素点的RGB编码,存入Bitmap对象,再调用Bitmap类自带的压缩方法生成JPG图片。这种方法效率极低,一张480x320分辨率的图片有20万个字节,因此运算需要经过20万次循环。其实android.graphics包下面有一个YuvImage类,可以将数据直接导入:

 YuvImage image = new YuvImage(data, ImageFormat.NV21, IMG_WIDTH, IMG_HEIGHT, null);

  前面两个参数决定了数据源与图像格式,后面单个参数就不解释了。

  而YuvImage类正好有一个compressToJPEG(Rect rect, int i, OutputStream)方法,可以直接将数据保存在JPG文件的输出流中。

  2、PNG转Bitmap

 byte[] data = null;
File pngImage = null;
BufferedOutputStream stream = null;
try {
  pngImage = new File(outputFile); //outputFile为png图像名称
  FileOutputStream fstream = new FileOutputStream(pngImage);
  stream = new BufferedOutputStream(fstream);
  stream.write(data);
} catch (Exception e) {
  e.printStackTrace();
} finally {
  if (stream != null) {
    try {
    stream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);

  如果通过资源(drawable)的形式,那就方便地多,只需要一句话。

 Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

  虽然没有华丽的算法,但效果不错哦,就是想改变图像属性时得另外实现。

  3、ARGB转Bitmap

 Bitmap bitmapOrg = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
Bitmap bitmapNew = bitmapOrg.copy(Config.ARGB_8888, true);
if(bitmapNew == null)
  return;
for(int i = 0;i<bitmapNew.getWidth();i++)
{
  for(int j =0;j<bitmapNew.getHeight();j++)
  {
    int col = bitmapNew.getPixel(i, j);
    int alpha = col&0xFF000000;
    int red = (col&0x00FF0000)>>16;
    int green = (col&0x0000FF00)>>8;
    int blue = (col&0x000000FF);
    int gray = (int)((float)red*0.3+(float)green*0.59+(float)blue*0.11);
    int newColor = alpha|(gray<<16)|(gray<<8)|gray;
  }
}
sendMsg(bitmapNew);
File file = new File(Environment.getExternalStorageDirectory()+File.separator+"gray"+number+".jpg");
OutputStream out;
try {
  out = new FileOutputStream(file);
  if(bitmapNew.compress(Bitmap.CompressFormat.JPEG, 100, out))
  out.close();
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

  注意,代码中做了灰度处理,若想得到彩色图,分别对Bitmap图像R、G、B三通道进行赋值即可。

  今天就先介绍到这,接下来随着学习的深入,图像转换有新的实现再进行补充。

Android图像格式类及图像转换方法的更多相关文章

  1. Android ColorMatrix类图像颜色处理-黑白老照片、泛黄旧照片、高对比度等效果

    在Android中,对图像进行颜色方面的处理,如黑白老照片.泛黄旧照片.高对比度.低饱和度等效果,都可以通过使用颜色矩阵(ColorMatrix)来实现. 1.颜色矩阵(ColorMatrix)介绍 ...

  2. Android Matrix类以及ColorMatri

    引自:http://www.chinabaike.com/t/37396/2014/0624/2556217.html Android Matrix类以及ColorMatrix类详解 最近在系统学习了 ...

  3. OpenCV中IplImage图像格式与BYTE图像数据的转换

    最近在将Karlsruhe Institute of Technology的Andreas Geiger发表在ACCV2010上的Efficent Large-Scale Stereo Matchin ...

  4. android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

    android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...

  5. Android 服务类Service 的详细学习

    http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建   目录(?)[+] 什么是服务 服务有 ...

  6. 项目源码--Android答题类游戏源码

    下载源码 技术要点: 1. 精致的答题UI 2. Android的Http通信技术 3. Android数据库QLITE与其他数据存储技术 4. Android在线音乐背景技术 5. Android答 ...

  7. android application类的用法

    android application类的用法 Application是android系统Framework提供的一个组件,它是单例模式(singleton),即每个应用只有一个实例,用来存储系统的一 ...

  8. c#图像处理入门(-bitmap类和图像像素值获取方法)

    c#图像处理入门 -bitmap类和图像像素值获取方法 一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义 ...

  9. android资讯类软件框架《IT蓝豹》

    android资讯类软件框架 android资讯类软件框架,支持侧滑,并且首页viewpager切换tab,tab滑动到最右侧的时候提示滑动结束, 滑动到最左侧的时候切换滑动侧滑menu.左滑和侧滑处 ...

随机推荐

  1. JS高级程序设计2nd部分知识要点1

    保存浮点数值需要的内存空间是保存整数值的两倍,因此ECMAScript会不失时机的将浮点数值转换为整数值 浮点数值的最高精度是17位小数 parseInt 字符串转换为数值,可传基数(8,16) pa ...

  2. Ubuntu16.04安装ROS-kinetic

    参考链接:http://www.voidcn.com/blog/wishchin/article/p-5972036.html 第一步: 软件源配置1. 增加下载源(增加ubuntu版的ros数据仓库 ...

  3. 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点

    平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...

  4. three Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  5. win10下安装Wampservice过程中遇到的问题及解决办法

    今天在电脑上装Wampserver的时候遇到了几个问题,启动Wampserver无法成功,一直显示橙色.若启动成功Wampserver的图标会显示绿色. 下面的是解决方法 安装 在浏览器中搜索Wamp ...

  6. git版本控制工具(二)----本地版本库的常用操作

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  7. Loadrunner:场景运行较长时间后报错:Message id [-17999] was not saved - Auto Log cache is too small to contain the message.

    loadrunner运行时间较长后,跑数据过程老是失败,有如下error: Message id [-17999] was not saved - Auto Log cache is too smal ...

  8. 创建服务factory和service方法的区别

    factory方法返回的是对象,json或数组,也可以返回字符串类型的数据,但service方法只能返回数据或对象 创建服务有3种方法 $provide.provider('服务名',function ...

  9. C# Thread.Join()用法的理解 转

    指在一线程里面调用另一线程join方法时,表示将本线程阻塞直至另一线程终止时再执行 比如 1using System; 2 3namespace TestThreadJoin 4{ 5 class P ...

  10. SQL 数据结构操作语句

    修改字段 exec sp_rename '表名.[字段名]','新字段名','column' alter table tab_info rename column createname to this ...