繁体   English   中英

在Java中压缩base 64 png图像

[英]compress base 64 png image in java

嗨,我想知道Java中是否有减小图像大小的方法。实际上,我的前端是IOS,它们正在发送Base 64编码数据,并且当我获取编码数据并解码时编码数据并存储在字节数组中。 现在我想在Java和我的方法代码中压缩PNG图片

public String  processFile(String strImageBase64, String strImageName,String donorId)
{       
    FileOutputStream fos =null;
    File savedFile=null;
    try
    {            
        String FileItemRefPath = propsFPCConfig.getProperty("fileCreationReferencePath");
        String imageURLReferncePath = propsFPCConfig.getProperty("imageURLReferncePath");
        File f = new File(FileItemRefPath+"\\"+"productimages"+"\\"+donorId);            
        String strException = "Actual File "+f.getName();            
        if(!f.exists())
        {
            boolean isdirCreationStatus = f.mkdir();                
        }            
        String strDateTobeAppended = new SimpleDateFormat("yyyyMMddhhmm").format(new Date(0));            
        String fileName = strImageName+strDateTobeAppended;            
        savedFile = new File(f.getAbsolutePath()+"\\"+fileName);            
        strException=strException+" savedFile "+savedFile.getName();            
        Base64 decoder = new Base64();            
        byte[] decodedBytes = decoder.decode(strImageBase64);                                    
        if( (decodedBytes != null) && (decodedBytes.length != 0) )
        {
            System.out.println("Decoded bytes length:"+decodedBytes.length);
            fos = new FileOutputStream(savedFile);                                
            System.out.println(new String(decodedBytes) + "\n") ;
            int x=0;
            {
                fos.write(decodedBytes, 0, decodedBytes.length);
            }
            fos.flush();
        }
        //System.out.println(savedFile.getCanonicalPath() +" savedFile.getCanonicalPath()  ");            
        if(fos != null)
        {
            fos.close();
            return savedFile.getAbsolutePath();
        }
        else
        {
            return null;
        }
    }
    catch(Exception e)
    {            
        e.printStackTrace();
    }
    finally
    {
        try
        {
            if( fos!= null)
            {
                fos.close();
            }
            else
            {
                savedFile = null;
            }
        }
        catch (IOException e)
        {                
            e.printStackTrace();
        }
    }        
    return savedFile.getName();
}

并且我正在使用图像名称存储此解码数据,现在我想将此压缩图像存储在另一个网址中

我认为这不值得付出努力。

PNG已经具有很高的压缩率。 很难通过额外的压缩来减小尺寸。

如果您确实要向客户端发送图像或编码为Base64的响应,那么当然可以使用一些方法来提高传输速率:在服务器上启用gzip压缩,以便对HTTP响应进行gzip压缩。 万一原始数据经过Base64编码(这实际上意味着您仅使用每字节8位中的6位),这将减少实际传输字节的数量。 启用gzip压缩对您的服务器代码是透明的,并且对于大多数Web服务器而言只是一个配置开关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM