当前位置: 移动技术网 > IT编程>移动开发>Android > Android程序开发如何处理图像格式类及图像转换

Android程序开发如何处理图像格式类及图像转换

2019年07月24日  | 移动技术网IT编程  | 我要评论

和平影都官网,法学在职研究生报考条件,约会来

在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(); 
 } 

      以上全部内容就是在android程序开发过程中处理图像格式类及图像转换的方法,需要注意的是,在代码中做了灰度处理,若想得到彩色图,分别对bitmap图像r、g、b三通道赋值。希望大家能够喜欢。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网