亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android Socket服務(wù)端與客戶端用字符串的方式互相傳遞圖片的方法

 更新時(shí)間:2016年05月03日 14:08:19   作者:奮斗者—cyf  
這篇文章主要介紹了Android Socket服務(wù)端與客戶端用字符串的方式互相傳遞圖片的方法的相關(guān)資料,需要的朋友可以參考下

發(fā)送圖片:

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

<span style="font-family: comic sans ms,sans-serif; font-size: 16px;">private Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此時(shí)返回bm為空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// 現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們設(shè)置為
float hh = 100f;// 這里設(shè)置高度為800f
float ww = 100f;// 這里設(shè)置寬度為480f
// 縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可
int be = 1;// be=1表示不縮放
if (w > h && w > ww) {// 如果寬度大的話根據(jù)寬度固定大小縮放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {// 如果高度高的話根據(jù)寬度固定大小縮放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// 設(shè)置縮放比例
// 重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);// 壓縮好比例大小后再進(jìn)行質(zhì)量壓縮
}
</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);// 質(zhì)量壓縮方法,這里表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
int options = ;
while (baos.toByteArray().length / > ) { // 循環(huán)判斷如果壓縮后圖片是否大于kb,大于繼續(xù)壓縮
baos.reset();// 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
options -= ;// 每次都減少
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數(shù)據(jù)生成圖片
return bitmap;
}
</span> 

將bitmap轉(zhuǎn)化為byte[]數(shù)組

<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轉(zhuǎn)化成byte[]數(shù)組:

<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[]轉(zhuǎn)化成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方法就可以將接收到的圖片顯示到手機(jī)了。

相關(guān)文章

最新評論