服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android程序开发如何处理图像格式类及图像转换

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

2021-03-28 17:57路上的脚印 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类的图像格式对象。如

 

复制代码 代码如下:

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
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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三通道赋值。希望大家能够喜欢。

延伸 · 阅读

精彩推荐
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7442021-03-11
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4632021-03-09
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4662021-01-03
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27