最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android程序開發(fā)如何處理圖像格式類及圖像轉(zhuǎn)換

 更新時間:2015年07月28日 09:06:20   作者:路上的腳印  
這篇文章主要介紹了Android程序開發(fā)如何處理圖像格式類及圖像轉(zhuǎn)換,需要的朋友可以參考下

在Android程序開發(fā)過程中,明確哪些圖像格式類(ImageFormat、PixelFormat及BitmapConfig等)及圖像(JPG、PNG及BMP等)的轉(zhuǎn)換方式非常重要,在以后的程序開發(fā)過程中會起到非常重要的作用。在一個項目開發(fā)過程中一款軟件的開發(fā)和圖像處理有著密切的關(guān)系,特別是在移動應(yīng)用程序,在視覺效果等方面起到至關(guān)重要的作用,因為這關(guān)系到用戶體驗度。下面通過代碼實例給大家分享下:

  關(guān)于圖像格式類,介紹以下三個:ImageFormat、PixelFormat及BitmapConfig。

  1、ImageFormat(android.graphics.ImageFormat),格式參數(shù)有以下幾種:

    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

  解釋總是英文的最通俗易懂,這里就不獻丑翻譯了。用法舉例,在構(gòu)建ImageReader類的對象時,會用到ImageFormat類的圖像格式對象。如

復制代碼 代碼如下:

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

  imageReader對象表示其緩存中最多存在寬高分別為width和height、RGB_565格式的圖像流兩幀。

在需求中用哪一種圖像格式,要視實際情況而定,后面的類似。

2、PixelFormat(android.graphics.PixelFormat),格式參數(shù)有以下幾種:

    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相對應(yīng)的格式進行代替。由此可知,兩種圖像格式之間存在相通之處。用法舉例,讓窗口實現(xiàn)漸變的效果,如

復制代碼 代碼如下:

getWindow().setFormat(PixelFormat.RGBA_8888);

  補充說明:RGBA_8888為android的一種32位顏色格式,R、G、B、A分別用八位表示,Android默認的圖像格式是PixelFormat.OPAQUE,其是不帶Alpha值的。

  3、Bitmap.Config(Android.graphics.Bitmap內(nèi)部類)

  

復制代碼 代碼如下:

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。(

官網(wǎng)介紹,大致意思是說:影響一個圖片色彩色度顯示質(zhì)量主要看位圖配置,顯示圖片時透明還是半透明)。

    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個字節(jié),每個顏色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。(這個應(yīng)該很容易理解了)。

  用法舉例,構(gòu)建Bitmap對象時,會用到BitmapConfig類圖像格式對象,如:

復制代碼 代碼如下:

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

  下面來看各種類型圖像之間的轉(zhuǎn)換都有哪些方法、差異及共同點。

  1、YUV轉(zhuǎn)JPG

  查閱到的資料大部分是把Yuv圖像數(shù)據(jù)通過數(shù)學運算得到每個像素點的RGB編碼,存入Bitmap對象,再調(diào)用Bitmap類自帶的壓縮方法生成JPG圖片。這種方法效率極低,一張480x320分辨率的圖片有20萬個字節(jié),因此運算需要經(jīng)過20萬次循環(huán)。其實android.graphics包下面有一個YuvImage類,可以將數(shù)據(jù)直接導入:

YuvImage image = new YuvImage(data, ImageFormat.NV21, IMG_WIDTH, IMG_HEIGHT, null);
  前面兩個參數(shù)決定了數(shù)據(jù)源與圖像格式,后面單個參數(shù)就不解釋了。

  而YuvImage類正好有一個compressToJPEG(Rect rect, int i, OutputStream)方法,可以直接將數(shù)據(jù)保存在JPG文件的輸出流中。

  2、PNG轉(zhuǎn)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);

  雖然沒有華麗的算法,但效果不錯哦,就是想改變圖像屬性時得另外實現(xiàn)。

  3、ARGB轉(zhuǎn)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(); 
 } 

      以上全部內(nèi)容就是在Android程序開發(fā)過程中處理圖像格式類及圖像轉(zhuǎn)換的方法,需要注意的是,在代碼中做了灰度處理,若想得到彩色圖,分別對Bitmap圖像R、G、B三通道賦值。希望大家能夠喜歡。

相關(guān)文章

  • Android原生嵌入React Native詳解

    Android原生嵌入React Native詳解

    這篇文章主要為大家詳細介紹了Android原生嵌入React Native的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android編程實現(xiàn)Gallery中每次滑動只顯示一頁的方法

    Android編程實現(xiàn)Gallery中每次滑動只顯示一頁的方法

    這篇文章主要介紹了Android編程實現(xiàn)Gallery中每次滑動只顯示一頁的方法,涉及Android擴展Gallery控件實現(xiàn)翻頁效果控制的功能,涉及Android事件響應(yīng)及屬性控制的相關(guān)技巧,需要的朋友可以參考下
    2015-11-11
  • Android 中LayoutInflater.inflate()方法的介紹

    Android 中LayoutInflater.inflate()方法的介紹

    這篇文章主要介紹了Android 中LayoutInflater.inflate()方法的介紹的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • android TabLayout的指示器寬度問題

    android TabLayout的指示器寬度問題

    這篇文章主要介紹了android TabLayout的指示器寬度問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Android Rxjava3 使用場景詳解

    Android Rxjava3 使用場景詳解

    本文主要介紹了Android Rxjava3 使用場景詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • Android組件實現(xiàn)列表選擇框功能

    Android組件實現(xiàn)列表選擇框功能

    android提供的列表選擇框(Spinner)相當于web端用戶注冊時的選擇下拉框,比如注冊候選擇省份城市等。這篇文章主要介紹了Android組件實現(xiàn)列表選擇框功能,需要的朋友可以參考下
    2017-02-02
  • xUtils3.0框架學習筆記分享

    xUtils3.0框架學習筆記分享

    這篇文章主要為大家分享了xUtils3.0框架學習筆記,xUtils開源框架的功能介紹,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android FFmpeg音視頻解碼播放示例詳解

    Android FFmpeg音視頻解碼播放示例詳解

    這篇文章主要為大家介紹了Android FFmpeg音視頻解碼播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Android一行代碼實現(xiàn)圓形頭像

    Android一行代碼實現(xiàn)圓形頭像

    本篇文章主要介紹了Android一行代碼實現(xiàn)圓形頭像的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-05-05
  • 揭秘在ListView等AdapterView上動態(tài)添加刪除項的陷阱

    揭秘在ListView等AdapterView上動態(tài)添加刪除項的陷阱

    今天遇到這么個需求,需要在運行時動態(tài)添加ListView的item,看起來很簡單,實際操作過程中卻遇到了麻煩,下面揭秘在ListView等AdapterView上動態(tài)添加刪除項的陷阱
    2016-04-04

最新評論

余庆县| 榆社县| 额敏县| 永川市| 车致| 印江| 新昌县| 杭州市| 上林县| 乳山市| 如东县| 宽城| 涞水县| 吕梁市| 贺兰县| 宜章县| 扬中市| 黄大仙区| 盈江县| 新兴县| 锡林浩特市| 紫金县| 河北省| 子洲县| 宿州市| 东乡| 赫章县| 普兰店市| 巴林右旗| 花莲市| 宁都县| 泾源县| 永修县| 潮州市| 新营市| 阿巴嘎旗| 乐山市| 商都县| 兴文县| 乌苏市| 司法|