【Java基础】图片压缩

最近有个需求,需要把图片字节码用Base64 encode之后作为请求报文的一部分调用外部服务方,服务方对图片的大小有要求,不能超过500KB,另一方面,请求报文太大,会出现如下错误:413 请求体过大

先看下现在的图片:

如果是线上的图片,我也写了个获取图片字节码,查看图片大小的小程序

public class GetImgSize {

    public static void main(String[] args) {

        String posUrl = "http://file11info.ppdai.com/4e598e3bd163405d90d8f7b7783d494d.jpg";

        byte[] a = getBy(posUrl);

        System.out.println(a.length);
    }

    private static byte[] getBy(String posUrl){
        try {
            URL url = new URL(posUrl);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            try(InputStream inputStream = url.openStream()){
                int len = 0;
                byte[] buffer = new byte[1024];
                while (-1 != (len = inputStream.read(buffer))){
                    outputStream.write(buffer, 0, len);
                }
            }
            return outputStream.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

压缩函数如下

public class GetImgSize {

    public static void main(String[] args) throws IOException {

        String posUrl = "http://file11info.ppdai.com/4e598e3bd163405d90d8f7b7783d494d.jpg";

        byte[] a = compress(posUrl);

        System.out.println(a.length);

        File output = new File("./duke-compressed-005.jpg");
        OutputStream out = null;
        try {
            out = new FileOutputStream(output);
            out.write(a);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(out != null){
                out.flush();
                out.close();
            }
        }
    }

    private static byte[] compress(String posUrl) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageOutputStream ios = null;
        byte [] data = null;
        ImageWriter writer = null;
        try {
            URL url = new URL(posUrl);
            BufferedImage bufferedImage = ImageIO.read(url);

            writer = ImageIO.getImageWritersByFormatName("jpg").next();

            ios = ImageIO.createImageOutputStream(bos);

            writer.setOutput(ios);

            ImageWriteParam param = writer.getDefaultWriteParam();
            if (param.canWriteCompressed()){
                param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                param.setCompressionQuality(0.5f); //压缩比  不够可以设置0.05
            }

            writer.write(null, new IIOImage(bufferedImage, null, null), param);

            data = bos.toByteArray();

            bos.close();
            ios.flush();
            ios.close();
            writer.dispose();

            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            bos.close();

            if(ios != null){
                ios.close();
            }
            
            if(writer != null){
                writer.dispose();
            }
        }
        return null;
    }
}

 

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...