Android图像格式类及图像转换方法

  

  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类的图像格式对象。如

1 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相对应的格式进行代替。由此可知,两种图像格式之间存在相通之处。用法举例,让窗口实现渐变的效果,如

1 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类图像格式对象,如:

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

  

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

  1、YUV转JPG

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

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

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

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

  2、PNG转Bitmap

 1 byte[] data = null;
 2 File pngImage = null;
 3 BufferedOutputStream stream = null;
 4 try {
 5   pngImage = new File(outputFile); //outputFile为png图像名称
 6   FileOutputStream fstream = new FileOutputStream(pngImage);
 7   stream = new BufferedOutputStream(fstream);
 8   stream.write(data);
 9 } catch (Exception e) {
10   e.printStackTrace();
11 } finally {
12   if (stream != null) {
13     try {
14     stream.close();
15     } catch (IOException e) {
16       e.printStackTrace();
17     }
18   }
19 }
20 Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);

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

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

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

  3、ARGB转Bitmap

 1 Bitmap bitmapOrg = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
 2 Bitmap bitmapNew = bitmapOrg.copy(Config.ARGB_8888, true);
 3 if(bitmapNew == null)
 4   return;
 5 for(int i = 0;i<bitmapNew.getWidth();i++)
 6 {
 7   for(int j =0;j<bitmapNew.getHeight();j++)
 8   {
 9     int col = bitmapNew.getPixel(i, j);
10     int alpha = col&0xFF000000;
11     int red = (col&0x00FF0000)>>16;
12     int green = (col&0x0000FF00)>>8;
13     int blue = (col&0x000000FF);
14     int gray = (int)((float)red*0.3+(float)green*0.59+(float)blue*0.11);
15     int newColor = alpha|(gray<<16)|(gray<<8)|gray;
16   }
17 }
18 sendMsg(bitmapNew);
19 File file = new File(Environment.getExternalStorageDirectory()+File.separator+"gray"+number+".jpg");
20 OutputStream out;
21 try {
22   out = new FileOutputStream(file);
23   if(bitmapNew.compress(Bitmap.CompressFormat.JPEG, 100, out))
24   out.close();
25 } catch (FileNotFoundException e) {
26   e.printStackTrace();
27 } catch (IOException e) {
28   e.printStackTrace();
29 } 

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

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

时间: 2024-08-09 02:18:28

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

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

在Android中,对图像进行颜色方面的处理,如黑白老照片.泛黄旧照片.高对比度.低饱和度等效果,都可以通过使用颜色矩阵(ColorMatrix)来实现. 1.颜色矩阵(ColorMatrix)介绍 颜色矩阵M是一个5*4的矩阵,如图1所示.在Android中,颜色矩阵M是以一维数组m=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t]的方式进行存储的. 图1 颜色矩阵M 在一张图片中,图像的RGBA(红色.绿色.蓝色.透明度)值决定了该图片所呈现出来的颜色效果.

Android Matrix类以及ColorMatri

引自:http://www.chinabaike.com/t/37396/2014/0624/2556217.html Android Matrix类以及ColorMatrix类详解 最近在系统学习了android的图像处理(在网上搜集了一些资料并自己编写了测试程序,做了整理),现在这里做一总结: 一.ColorMatrix类 ColorMatrix是一个5x4阶的矩阵 在下面表示为A,第一行表示R红色分量,第二行表示G绿色分量,第三行表示B蓝色分量,第四行表示透明度: 用一维数组的存储方式如下

android 小说类源码制作教程源码下载

自己闲着没事制作了个小说软件用来自己看全本/连载小说, 翻页,字体大小,目录,自动更新 具体效果如下:奉献给大家下载查看... 下载APK效果查看地址: http://yun.baidu.com/s/1gdknYyJ 源码下载地址: http://download.csdn.net/detail/ainibaifenbai/7575817 android 小说类源码制作教程源码下载,布布扣,bubuko.com

Android Activity类讲解(一)

--by CY[[email protected]] 1.protected void onCreate(Bundle savedInstanceState) { throw new RuntimeException("Stub!"); } 当创建一个Activity时,系统会自动调用onCreate方法来完成创建工作.该创建工作包括布局,监听器的绑定等. 首先说一下Bundle 这个类,Bundle是一个键值对,跟Map类似,两个Activity之间的通信可以用Bundle类来实现.

android异步类AsyncTask的简单使用

Android为了降低这个开发难度,提供了AsyncTask.AsyncTask就是一个封装过的后台任务类,顾名思义就是异步任务,更通俗地说就是一个执行后台任务的线程 而且他还会自动通知主线程更新UI 优点: 结构清晰,容易理解. 缺点 代码量稍大 下面直接看代码 1 private class AsyncLogin extends AsyncTask<Void,Integer,Boolean>{ 2 private EditText passwordEdit; 3 private EditT

Android基类设计方法详解

1 为什么要设计基类 为什么要给程序设计基类呢?主要是出于2个原因,一是方便代码编写,减少重复代码和冗余逻辑,优化代码:二是优化程序架构,降低耦合度,方便拓展.修改. ok,编写代码是程序员的第一步,那么第二步就是要编写高质量的代码,代码能实现功能是一方面,写的优美则是另一方面,这也是我们所有攻城狮们应该追求的境界. 2 设计基类的基本思路 那么,哪些东西我们需要抽象到基类中呢? 2.1 重复的代码:如果一个逻辑是大多数子类都需要使用的 2.2 臭而长的代码:典型的findviewbyid.To

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

c#图像处理入门 -bitmap类和图像像素值获取方法 一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetPixel方法和SetPixel方法:获取和设置一个图像的指定像素的颜色. 2. PixelFormat属性:返回图像的像素格式. 3. Palette属性:获取和设置图像所使用的颜色调色板. 4. Height Width属性:返回图像的高度和

Android Environment类的接口详解

Android应用开发中,常使用Environment类去获取外部存储目录,在访问外部存储之前一定要先判断外部存储是否已经是可使用(已挂载&可使用)状态, 并且需要在AndroidManifest.xml文件中添加外部存储读和写的权限. Environment类中提供了几个静态常量用于标识外部存储的状态,这些状态都是String类型 MEDIA_BAD_REMOVAL 在没有挂载前存储媒体已经被移除. MEDIA_CHECKING 正在检查存储媒体. MEDIA_MOUNTED 存储媒体已经挂载

《全民填宝石》Android替换类宝石游戏,豌豆荚首发,快来尝鲜吧!

亲爱的朋友,你是否需要一款用心做出来的又极富创意性的宝石游戏呢,很高兴我担任了这个工作,我很荣幸,这是由我本人开发的一款Android休闲类替换宝石游戏[全民填宝石],你需要点击下方带框的宝石并拖动到想替换的色框上,颜色相同就得分,看你能否在120秒内全部替换并过关呢?不妨试试吧! 游戏下载地址: http://apps.wandoujia.com/apps/com.lxc.quanmingtianbaoshi