Android Socket服務(wù)端與客戶端用字符串的方式互相傳遞圖片的方法
發(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 設(shè)回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此時返回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
// 縮放比。由于是固定比例縮放,只用高或者寬其中一個數(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è)置縮放比例
// 重新讀入圖片,注意此時已經(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ī)了。
- Android將圖片上傳到php服務(wù)器的實(shí)例代碼
- Android讀取服務(wù)器圖片的三種方法
- Android 通過Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例
- Android 通過webservice上傳多張圖片到指定服務(wù)器詳解
- Android選擇圖片或拍照圖片上傳到服務(wù)器
- Android開發(fā)中調(diào)用系統(tǒng)相冊上傳圖片到服務(wù)器OPPO等部分手機(jī)上出現(xiàn)短暫的顯示桌面問題的解決方法
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android異步上傳圖片到PHP服務(wù)器
- Android從服務(wù)器獲取圖片的實(shí)例方法
- android傳送照片到FTP服務(wù)器的實(shí)現(xiàn)代碼
相關(guān)文章
Android中實(shí)現(xiàn)多線程操作的幾種方式
多線程一直是一個老大難的問題,首先因?yàn)樗y以理解,其次在實(shí)際工作中我們需要面對的關(guān)于線程安全問題也并不常見,今天就來總結(jié)一下實(shí)現(xiàn)多線程的幾種方式,感興趣的可以了解一下2021-06-06
Android編程實(shí)現(xiàn)canvas繪制餅狀統(tǒng)計(jì)圖功能示例【自動適應(yīng)條目數(shù)量與大小】
這篇文章主要介紹了Android編程實(shí)現(xiàn)canvas繪制餅狀統(tǒng)計(jì)圖功能,可實(shí)現(xiàn)自動適應(yīng)條目數(shù)量與大小的功能,涉及Android基于canvas的圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
Android巧用XListView實(shí)現(xiàn)萬能下拉刷新控件
這篇文章主要為大家詳細(xì)介紹了Android巧用XListView實(shí)現(xiàn)萬能下拉刷新控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
Android調(diào)用系統(tǒng)照相機(jī)拍照與攝像的方法
這篇文章主要為大家詳細(xì)介紹了Android如何調(diào)用系統(tǒng)現(xiàn)有的照相機(jī)拍照與攝像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
搭建Android上的服務(wù)器 “實(shí)現(xiàn)隔空取物”的方法
本篇文章主要介紹了搭建Android上的服務(wù)器 “實(shí)現(xiàn)隔空取物”的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Android利用ZXing掃描二維碼的實(shí)例代碼解析
這篇文章主要介紹了Android利用ZXing掃描二維碼的實(shí)例解析,代碼簡單易懂,非常不錯,需要的朋友可以參考下2016-12-12
Android開發(fā)中Looper.prepare()和Looper.loop()
Looper用于封裝了android線程中的消息循環(huán),默認(rèn)情況下一個線程是不存在消息循環(huán)(message loop)的,具體調(diào)用方法大家可以通過本文學(xué)習(xí)2016-11-11

