android压缩图片方法

来源:互联网 发布:java仪表盘代码 编辑:程序博客网 时间:2024/05/16 19:41

第一步压缩图片:
Java code
?
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, 00, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

第二步,把Bitmap转成InputStream  
Java code
?
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;  
        }

第三步,把InputStream  上传到服务器pc端
Java code
?
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, 1false);
        input.close();
        output.flush();
        output.close();
        conn.disconnect();
        return response;
    }

读取服务器返回的结果
Java code
?
1
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
33
/**
     * 读取输入流中的文本
     
     * @param input
     *            输入流
     * @param encoding
     *            编码格式(默认为UTF-8)
     * @param beginLine
     *            从第几行开始读(默认从第1行开始)
     * @param isLine
     *            读完一行是否写入换行符
     * @return String
     * @throws Exception
     */
    public String readFile(InputStream input, String encoding, int beginLine, boolean isLine) throws Exception {
        String result = "";
        beginLine = (beginLine <= 0 1 : beginLine);
        encoding = (ToolUtil.get().isBlank(encoding) ? HTTP.UTF_8 : encoding);
        InputStreamReader read = new InputStreamReader(input, encoding);
        BufferedReader reader = new BufferedReader(read);
        String line;
        int i = 1;
        while ((line = reader.readLine()) != null) {
            if (i >= beginLine) {
                result += line + (isLine ? "\n" "");
            }
            i++;
        }
        reader.close();
        read.close();
        input.close();
        return result;
    }

0 0