發送圖片:
首先找到具體傳遞的圖片:
<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方法就可以將接收到的圖片顯示到手機了。