Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Socket 跨進程以字符串傳輸圖片的完整實踐

編輯:關於 Android 程式設計

在 Socket 通訊場景中,如果希望讓服務端與客戶端之間直接交換圖片,一個常見且穩定的思路是把圖片先壓縮、再轉換成字符串進行傳輸,接收端拿到字符串後再還原成圖片顯示。本文將沿著「發送端」與「接收端」兩條主線,完整梳理這套流程。

一、發送圖片

第一步,是把待傳輸的圖片從指定路徑讀取出來,並在解碼階段先獲取圖片的尺寸資訊,以便後續進行等比縮放。

 <span >private Bitmap getimage(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 開始讀入圖片,此時把options.inJustDecodeBounds 設回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此時返回bm為空 newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 現在主流手機比較多是800*480分辨率,所以高和寬我們設置為 float hh = 100f;// 這裡設置高度為800f float ww = 100f;// 這裡設置寬度為480f // 縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可 int be = 1;// be=1表示不縮放 if (w > h && w > ww) {// 如果寬度大的話根據寬度固定大小縮放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {// 如果高度高的話根據寬度固定大小縮放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;// 設置縮放比例 // 重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return compressImage(bitmap);// 壓縮好比例大小後再進行質量壓縮 } </span> 

讀取到原始圖片後,還需要對它進行質量壓縮,避免數據量過大導致傳輸緩慢或記憶體溢出。下面的方法透過循環判斷,反覆降低壓縮品質,直到圖片體積滿足要求為止。

 <span >private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, , baos);// 質量壓縮方法,這裡表示不壓縮,把壓縮後的數據存放到baos中 int options = ; while (baos.toByteArray().length / > ) { // 循環判斷如果壓縮後圖片是否大於kb,大於繼續壓縮 baos.reset();// 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這裡壓縮options%,把壓縮後的數據存放到baos中 options -= ;// 每次都減少 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮後的數據baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數據生成圖片 return bitmap; } </span> 

接下來把 Bitmap 物件轉為位元組陣列,為編碼成字符串做好準備。

 <span >public byte[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } </span> 

由於 Socket 適合傳輸文本數據,因此我們再將位元組陣列格式化為十六進位字符串,這樣就能安全地放入通訊流中。

 <span >/** * 格式化byte * * @param b * @return */ public static String bytehex(byte[] b) { char[] Digit = { '', '', '', '', '', '', '', '', '', '', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] out = new char[b.length * ]; for (int i = ; i < b.length; i++) { byte c = b[i]; out[i * ] = Digit[(c >>> ) & XF]; out[i * + ] = Digit[c & XF]; } return new String(out); } </span>

二、接收圖片

接收端首先要做的,是把收到的字符串還原成位元組陣列,也就是執行與發送端相反的「反格式化」操作。

 <span >/** * 反格式化byte * * @param s * @return */ public static byte[] hexbyte(String s) { byte[] src = s.toLowerCase().getBytes(); byte[] ret = new byte[src.length / ]; for (int i = ; i < src.length; i += ) { byte hi = src[i]; byte low = src[i + ]; hi = (byte) ((hi >= 'a' && hi <= 'f') ? xa + (hi - 'a') : hi - ''); low = (byte) ((low >= 'a' && low <= 'f') ? xa + (low - 'a') : low - ''); ret[i / ] = (byte) (hi << | low); } return ret; } </span> 

拿到位元組陣列之後,再將其還原為 Bitmap 物件。

 <span >public Bitmap BytesBimap(byte[] b) { if (b.length != ) { return BitmapFactory.decodeByteArray(b, , b.length); } else { return null; } } </span> 

最後,只需呼叫 Android 提供的 setImageBitmap 方法,就能把還原後的圖片顯示在手機畫面上。整體來看,這套「壓縮 → 位元組 → 字符串 → 傳輸 → 反解 → 顯示」的鏈路,能在純文本通訊環境下穩定地傳遞圖片。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved