Android Socket服務端與客戶端用字符串的方式互相傳遞圖片的方法
更新時間:2016年05月03日 14:08:19 作者:奮斗者—cyf
這篇文章主要介紹了Android Socket服務端與客戶端用字符串的方式互相傳遞圖片的方法的相關資料,需要的朋友可以參考下
發(fā)送圖片:
首先找到具體傳遞的圖片:
<span style="font-family: comic sans ms,sans-serif; font-size: 16px;">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 style="font-family: comic sans ms,sans-serif; font-size: px;">private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, , baos);// 質量壓縮方法,這里表示不壓縮,把壓縮后的數據存放到baos中 int options = ; while (baos.toByteArray().length / > ) { // 循環(huán)判斷如果壓縮后圖片是否大于kb,大于繼續(xù)壓縮 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轉化為byte[]數組
<span style="font-family: comic sans ms,sans-serif; font-size: 16px;">public byte[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } </span>
格式化byte成字符串
<span style="font-family: comic sans ms,sans-serif; font-size: px;">/** * 格式化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>
接收圖片:
首先將傳遞過來的String轉化成byte[]數組:
<span style="font-family: comic sans ms,sans-serif; font-size: px;">/** * 反格式化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>
將byte[]轉化成bitmap:
<span style="font-family: comic sans ms,sans-serif; font-size: px;">public Bitmap BytesBimap(byte[] b) { if (b.length != ) { return BitmapFactory.decodeByteArray(b, , b.length); } else { return null; } } </span>
使用android中的setImageBitmap方法就可以將接收到的圖片顯示到手機了。
相關文章
Android編程實現canvas繪制餅狀統(tǒng)計圖功能示例【自動適應條目數量與大小】
這篇文章主要介紹了Android編程實現canvas繪制餅狀統(tǒng)計圖功能,可實現自動適應條目數量與大小的功能,涉及Android基于canvas的圖形繪制相關操作技巧,需要的朋友可以參考下2017-01-01Android開發(fā)中Looper.prepare()和Looper.loop()
Looper用于封裝了android線程中的消息循環(huán),默認情況下一個線程是不存在消息循環(huán)(message loop)的,具體調用方法大家可以通過本文學習2016-11-11