Android Socket服務端與用戶端用字串的方式互相傳遞圖片的方法_Android

來源:互聯網
上載者:User

發送圖片:

首先找到具體傳遞的圖片:

<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;// 這裡設定高度為800ffloat 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 / > ) { // 迴圈判斷如果壓縮後圖片是否大於kb,大於繼續壓縮baos.reset();// 重設baos即清空baosimage.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方法就可以將接收到的圖片顯示到手機了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.