1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/**
* 压缩图片
*
* @param bitmap
* 源图片
* @param width
* 想要的宽度
* @param height
* 想要的高度
* @param isAdjust
* 是否自动调整尺寸, true图片就不会拉伸,false严格按照你的尺寸压缩
* @return Bitmap
*/
public
Bitmap reduce(Bitmap bitmap,
int
width,
int
height,
boolean
isAdjust) {
if
(
null
== bitmap) {
return
null
;
}
if
(bitmap.getWidth() < width && bitmap.getHeight() < height && isAdjust) {
return
bitmap;
}
float
sx =
new
BigDecimal(width).divide(
new
BigDecimal(bitmap.getWidth()),
4
, BigDecimal.ROUND_DOWN)
.floatValue();
float
sy =
new
BigDecimal(height).divide(
new
BigDecimal(bitmap.getHeight()),
4
, BigDecimal.ROUND_DOWN)
.floatValue();
if
(isAdjust) {
sx = (sx < sy ? sx : sy);
sy = sx;
}
Matrix matrix =
new
Matrix();
matrix.postScale(sx, sy);
return
Bitmap.createBitmap(bitmap,
0
,
0
, bitmap.getWidth(), bitmap.getHeight(), matrix,
true
);
}
|
1
2
3
4
5
6
|
public
InputStream bitmap2IS(Bitmap bm){
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,
100
, baos);
InputStream sbs =
new
ByteArrayInputStream(baos.toByteArray());
return
sbs;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/**
* 上传文件到服务器
*
* @param url
* 上传路径
*
* @param input
* 文件流
* @return String
* @throws Exception
*/
public
String upload(String url, InputStream input)
throws
Exception {
HttpURLConnection conn = (HttpURLConnection)
new
URL(url).openConnection();
conn.setDoOutput(
true
);
conn.setDoInput(
true
);
conn.setChunkedStreamingMode(
1024
*
1024
);
conn.setRequestMethod(Constant.REQUEST_POST);
conn.setRequestProperty(
"connection"
,
"Keep-Alive"
);
conn.setRequestProperty(
"Charsert"
, HTTP.UTF_8);
conn.setRequestProperty(
"Content-Type"
,
"multipart/form-data;file=img.png"
);
OutputStream output = conn.getOutputStream();
int
i =
0
, total =
0
;
byte
[] buffer =
new
byte
[
1024
];
while
((i = input.read(buffer)) != -
1
) {
output.write(buffer,
0
, i);
}
String response = readFile(conn.getInputStream(), HTTP.UTF_8,
1
,
false
);
input.close();
output.flush();
output.close();
conn.disconnect();
return
response;
}
读取服务器返回的结果
|