当前位置: 移动技术网 > IT编程>移动开发>Android > Android Socket服务端与客户端用字符串的方式互相传递图片的方法

Android Socket服务端与客户端用字符串的方式互相传递图片的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

虾蛊,梦想三国武将资质,妹妹恋人ova

发送图片:

首先找到具体传递的图片:

<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 / > ) { // 循环判断如果压缩后图片是否大于kb,大于继续压缩
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方法就可以将接收到的图片显示到手机了。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网