简体   繁体   English

在Java中压缩base 64 png图像

[英]compress base 64 png image in java

Hi I would like to know if there is any way in Java to reduce the size of an image.Actually My front end is IOS,they are sending Base 64 encode data and when i'm getting the encoded data and i'm decoding the encoded data and storing in byte array. 嗨,我想知道Java中是否有减小图像大小的方法。实际上,我的前端是IOS,它们正在发送Base 64编码数据,并且当我获取编码数据并解码时编码数据并存储在字节数组中。 and now i want to compress the PNG image in java and my method code something like 现在我想在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();
}

and i'm storing this decoded data with imagename,now i want to store this compressed image in anothe url 并且我正在使用图像名称存储此解码数据,现在我想将此压缩图像存储在另一个网址中

I don't think this should be worth the effort. 我认为这不值得付出努力。

PNGs already have a very high level of compression. PNG已经具有很高的压缩率。 It is hard to reduce the size by means of additional compression significantly. 很难通过额外的压缩来减小尺寸。

If you are really sending the image or the response Base64 encoded to the client, of course there are ways to improve transfer rates: Enable gzip compression on your server so that HTTP responses will be gzip compressed. 如果您确实要向客户端发送图像或编码为Base64的响应,那么当然可以使用一些方法来提高传输速率:在服务器上启用gzip压缩,以便对HTTP响应进行gzip压缩。 This reduces the actual number of bytes to transfer quite a bit in case the original data is Base64 encoded (which basically means that you are only using 6 of 8 bits per bytes). 万一原始数据经过Base64编码(这实际上意味着您仅使用每字节8位中的6位),这将减少实际传输字节的数量。 Enabling gzip compression is transparent to your server code and is just a configuration switch away for most webservers. 启用gzip压缩对您的服务器代码是透明的,并且对于大多数Web服务器而言只是一个配置开关。

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

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